using System.Collections;
using System.Linq;
namespace Unity.VisualScripting
{
///
/// Counts all items in a collection or enumeration.
///
[UnitCategory("Collections")]
public sealed class CountItems : Unit
{
///
/// The collection.
///
[DoNotSerialize]
[PortLabelHidden]
public ValueInput collection { get; private set; }
///
/// The number of items contained in the collection.
///
[DoNotSerialize]
[PortLabelHidden]
public ValueOutput count { get; private set; }
protected override void Definition()
{
collection = ValueInput(nameof(collection));
count = ValueOutput(nameof(count), Count);
Requirement(collection, count);
}
public int Count(Flow flow)
{
var enumerable = flow.GetValue(collection);
if (enumerable is ICollection)
{
return ((ICollection)enumerable).Count;
}
else
{
return enumerable.Cast