Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LastItem.cs 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Collections;
  2. using System.Linq;
  3. namespace Unity.VisualScripting
  4. {
  5. /// <summary>
  6. /// Returns the first item in a collection or enumeration.
  7. /// </summary>
  8. [UnitCategory("Collections")]
  9. public sealed class LastItem : Unit
  10. {
  11. /// <summary>
  12. /// The collection.
  13. /// </summary>
  14. [DoNotSerialize]
  15. [PortLabelHidden]
  16. public ValueInput collection { get; private set; }
  17. /// <summary>
  18. /// The last item of the collection.
  19. /// </summary>
  20. [DoNotSerialize]
  21. [PortLabelHidden]
  22. public ValueOutput lastItem { get; private set; }
  23. protected override void Definition()
  24. {
  25. collection = ValueInput<IEnumerable>(nameof(collection));
  26. lastItem = ValueOutput(nameof(lastItem), First);
  27. Requirement(collection, lastItem);
  28. }
  29. public object First(Flow flow)
  30. {
  31. var enumerable = flow.GetValue<IEnumerable>(collection);
  32. if (enumerable is IList)
  33. {
  34. var list = (IList)enumerable;
  35. return list[list.Count - 1];
  36. }
  37. else
  38. {
  39. return enumerable.Cast<object>().Last();
  40. }
  41. }
  42. }
  43. }