A game about forced loneliness, made by TACStudios
1using System.Collections; 2using System.Linq; 3 4namespace Unity.VisualScripting 5{ 6 /// <summary> 7 /// Returns the first item in a collection or enumeration. 8 /// </summary> 9 [UnitCategory("Collections")] 10 public sealed class FirstItem : 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 first item of the collection. 21 /// </summary> 22 [DoNotSerialize] 23 [PortLabelHidden] 24 public ValueOutput firstItem { get; private set; } 25 26 protected override void Definition() 27 { 28 collection = ValueInput<IEnumerable>(nameof(collection)); 29 firstItem = ValueOutput(nameof(firstItem), First); 30 31 Requirement(collection, firstItem); 32 } 33 34 public object First(Flow flow) 35 { 36 var enumerable = flow.GetValue<IEnumerable>(collection); 37 38 if (enumerable is IList) 39 { 40 return ((IList)enumerable)[0]; 41 } 42 else 43 { 44 return enumerable.Cast<object>().First(); 45 } 46 } 47 } 48}