暫無描述
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.

CountItems.cs 1.2KB

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