CustomPortsNode.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using GraphProcessor;
  5. using System.Linq;
  6. [System.Serializable, NodeMenuItem("Custom/MultiPorts")]
  7. public class CustomPortsNode : BaseNode
  8. {
  9. [Input]
  10. public List< float > inputs;
  11. [Output]
  12. public List< float > outputs; // TODO: custom function for this one
  13. List< object > values = new List< object >();
  14. public override string name => "CustomPorts";
  15. public override string layoutStyle => "TestType";
  16. // We keep the max port count so it doesn't cause binding issues
  17. [SerializeField, HideInInspector]
  18. int portCount = 1;
  19. protected override void Process()
  20. {
  21. // do things with values
  22. }
  23. [CustomPortBehavior(nameof(inputs))]
  24. IEnumerable< PortData > ListPortBehavior(List< SerializableEdge > edges)
  25. {
  26. portCount = Mathf.Max(portCount, edges.Count + 1);
  27. for (int i = 0; i < portCount; i++)
  28. {
  29. yield return new PortData {
  30. displayName = "In " + i,
  31. displayType = typeof(float),
  32. identifier = i.ToString(), // Must be unique
  33. };
  34. }
  35. }
  36. // This function will be called once per port created from the `inputs` custom port function
  37. // will in parameter the list of the edges connected to this port
  38. [CustomPortInput(nameof(inputs), typeof(float))]
  39. void PullInputs(List< SerializableEdge > inputEdges)
  40. {
  41. values.AddRange(inputEdges.Select(e => e.passThroughBuffer).ToList());
  42. }
  43. [CustomPortOutput(nameof(outputs), typeof(float))]
  44. void PushOutputs(List< SerializableEdge > connectedEdges)
  45. {
  46. // Values length is supposed to match connected edges length
  47. for (int i = 0; i < connectedEdges.Count; i++)
  48. connectedEdges[i].passThroughBuffer = values[Mathf.Min(i, values.Count - 1)];
  49. // once the outputs are pushed, we don't need the inputs data anymore
  50. values.Clear();
  51. }
  52. }