using System.Collections.Generic; using System.Linq; namespace Unity.VisualScripting { [UnitOrder(303)] [TypeIcon(typeof(Add<>))] public abstract class Sum : MultiInputUnit { /// /// The sum. /// [DoNotSerialize] [PortLabelHidden] public ValueOutput sum { get; private set; } protected override void Definition() { if (this is IDefaultValue defaultValueUnit) { var mi = new List(); multiInputs = mi.AsReadOnly(); for (var i = 0; i < inputCount; i++) { if (i == 0) { mi.Add(ValueInput(i.ToString())); continue; } mi.Add(ValueInput(i.ToString(), defaultValueUnit.defaultValue)); } } else { base.Definition(); } sum = ValueOutput(nameof(sum), Operation).Predictable(); foreach (var multiInput in multiInputs) { Requirement(multiInput, sum); } } public abstract T Operation(T a, T b); public abstract T Operation(IEnumerable values); public T Operation(Flow flow) { if (inputCount == 2) { return Operation(flow.GetValue(multiInputs[0]), flow.GetValue(multiInputs[1])); } else { return Operation(multiInputs.Select(flow.GetValue)); } } } }