A game about forced loneliness, made by TACStudios
1using System.Collections;
2using System.Linq;
3
4namespace Unity.VisualScripting
5{
6 /// <summary>
7 /// Counts all items in a collection or enumeration.
8 /// </summary>
9 [UnitCategory("Collections")]
10 public sealed class CountItems : Unit
11 {
12 /// <summary>
13 /// The collection.
14 /// </summary>
15 [DoNotSerialize]
16 [PortLabelHidden]
17 public ValueInput collection { get; private set; }
18
19 /// <summary>
20 /// The number of items contained in the collection.
21 /// </summary>
22 [DoNotSerialize]
23 [PortLabelHidden]
24 public ValueOutput count { get; private set; }
25
26 protected override void Definition()
27 {
28 collection = ValueInput<IEnumerable>(nameof(collection));
29 count = ValueOutput(nameof(count), Count);
30
31 Requirement(collection, count);
32 }
33
34 public int Count(Flow flow)
35 {
36 var enumerable = flow.GetValue<IEnumerable>(collection);
37
38 if (enumerable is ICollection)
39 {
40 return ((ICollection)enumerable).Count;
41 }
42 else
43 {
44 return enumerable.Cast<object>().Count();
45 }
46 }
47 }
48}