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.

Sum.cs 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace Unity.VisualScripting
  4. {
  5. [UnitOrder(303)]
  6. [TypeIcon(typeof(Add<>))]
  7. public abstract class Sum<T> : MultiInputUnit<T>
  8. {
  9. /// <summary>
  10. /// The sum.
  11. /// </summary>
  12. [DoNotSerialize]
  13. [PortLabelHidden]
  14. public ValueOutput sum { get; private set; }
  15. protected override void Definition()
  16. {
  17. if (this is IDefaultValue<T> defaultValueUnit)
  18. {
  19. var mi = new List<ValueInput>();
  20. multiInputs = mi.AsReadOnly();
  21. for (var i = 0; i < inputCount; i++)
  22. {
  23. if (i == 0)
  24. {
  25. mi.Add(ValueInput<T>(i.ToString()));
  26. continue;
  27. }
  28. mi.Add(ValueInput(i.ToString(), defaultValueUnit.defaultValue));
  29. }
  30. }
  31. else
  32. {
  33. base.Definition();
  34. }
  35. sum = ValueOutput(nameof(sum), Operation).Predictable();
  36. foreach (var multiInput in multiInputs)
  37. {
  38. Requirement(multiInput, sum);
  39. }
  40. }
  41. public abstract T Operation(T a, T b);
  42. public abstract T Operation(IEnumerable<T> values);
  43. public T Operation(Flow flow)
  44. {
  45. if (inputCount == 2)
  46. {
  47. return Operation(flow.GetValue<T>(multiInputs[0]), flow.GetValue<T>(multiInputs[1]));
  48. }
  49. else
  50. {
  51. return Operation(multiInputs.Select(flow.GetValue<T>));
  52. }
  53. }
  54. }
  55. }