Singularity/Library/PackageCache/com.unity.visualscripting@1.../Runtime/VisualScripting.Flow/Framework/Collections/CountItems.cs

49 lines
1.2 KiB
C#
Raw Permalink Normal View History

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