No Description
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.

FirstItem.cs 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 FirstItem : Unit
  10. {
  11. /// <summary>
  12. /// The collection.
  13. /// </summary>
  14. [DoNotSerialize]
  15. [PortLabelHidden]
  16. public ValueInput collection { get; private set; }
  17. /// <summary>
  18. /// The first item of the collection.
  19. /// </summary>
  20. [DoNotSerialize]
  21. [PortLabelHidden]
  22. public ValueOutput firstItem { get; private set; }
  23. protected override void Definition()
  24. {
  25. collection = ValueInput<IEnumerable>(nameof(collection));
  26. firstItem = ValueOutput(nameof(firstItem), First);
  27. Requirement(collection, firstItem);
  28. }
  29. public object First(Flow flow)
  30. {
  31. var enumerable = flow.GetValue<IEnumerable>(collection);
  32. if (enumerable is IList)
  33. {
  34. return ((IList)enumerable)[0];
  35. }
  36. else
  37. {
  38. return enumerable.Cast<object>().First();
  39. }
  40. }
  41. }
  42. }