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 LastItem : 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 last item of the collection.
21 /// </summary>
22 [DoNotSerialize]
23 [PortLabelHidden]
24 public ValueOutput lastItem { get; private set; }
25
26 protected override void Definition()
27 {
28 collection = ValueInput<IEnumerable>(nameof(collection));
29 lastItem = ValueOutput(nameof(lastItem), First);
30
31 Requirement(collection, lastItem);
32 }
33
34 public object First(Flow flow)
35 {
36 var enumerable = flow.GetValue<IEnumerable>(collection);
37
38 if (enumerable is IList)
39 {
40 var list = (IList)enumerable;
41
42 return list[list.Count - 1];
43 }
44 else
45 {
46 return enumerable.Cast<object>().Last();
47 }
48 }
49 }
50}