ParameterNode.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using GraphProcessor;
  5. using System.Linq;
  6. using System;
  7. namespace GraphProcessor
  8. {
  9. [System.Serializable]
  10. public class ParameterNode : BaseNode
  11. {
  12. [Input]
  13. public object input;
  14. [Output]
  15. public object output;
  16. public override string name => "Parameter";
  17. // We serialize the GUID of the exposed parameter in the graph so we can retrieve the true ExposedParameter from the graph
  18. [SerializeField, HideInInspector]
  19. public string parameterGUID;
  20. public ExposedParameter parameter { get; private set; }
  21. public event Action onParameterChanged;
  22. public ParameterAccessor accessor;
  23. protected override void Enable()
  24. {
  25. // load the parameter
  26. LoadExposedParameter();
  27. graph.onExposedParameterModified += OnParamChanged;
  28. if (onParameterChanged != null)
  29. onParameterChanged?.Invoke();
  30. }
  31. void LoadExposedParameter()
  32. {
  33. parameter = graph.GetExposedParameterFromGUID(parameterGUID);
  34. if (parameter == null)
  35. {
  36. Debug.Log("Property \"" + parameterGUID + "\" Can't be found !");
  37. // Delete this node as the property can't be found
  38. graph.RemoveNode(this);
  39. return;
  40. }
  41. output = parameter.value;
  42. }
  43. void OnParamChanged(ExposedParameter modifiedParam)
  44. {
  45. if (parameter == modifiedParam)
  46. {
  47. onParameterChanged?.Invoke();
  48. }
  49. }
  50. [CustomPortBehavior(nameof(output))]
  51. IEnumerable<PortData> GetOutputPort(List<SerializableEdge> edges)
  52. {
  53. if (accessor == ParameterAccessor.Get)
  54. {
  55. yield return new PortData
  56. {
  57. identifier = "output",
  58. displayName = "Value",
  59. displayType = (parameter == null) ? typeof(object) : parameter.GetValueType(),
  60. acceptMultipleEdges = true
  61. };
  62. }
  63. }
  64. [CustomPortBehavior(nameof(input))]
  65. IEnumerable<PortData> GetInputPort(List<SerializableEdge> edges)
  66. {
  67. if (accessor == ParameterAccessor.Set)
  68. {
  69. yield return new PortData
  70. {
  71. identifier = "input",
  72. displayName = "Value",
  73. displayType = (parameter == null) ? typeof(object) : parameter.GetValueType(),
  74. };
  75. }
  76. }
  77. protected override void Process()
  78. {
  79. #if UNITY_EDITOR // In the editor, an undo/redo can change the parameter instance in the graph, in this case the field in this class will point to the wrong parameter
  80. parameter = graph.GetExposedParameterFromGUID(parameterGUID);
  81. #endif
  82. ClearMessages();
  83. if (parameter == null)
  84. {
  85. AddMessage($"Parameter not found: {parameterGUID}", NodeMessageType.Error);
  86. return;
  87. }
  88. if (accessor == ParameterAccessor.Get)
  89. output = parameter.value;
  90. else
  91. graph.UpdateExposedParameter(parameter.guid, input);
  92. }
  93. }
  94. public enum ParameterAccessor
  95. {
  96. Get,
  97. Set
  98. }
  99. }