GraphInspector.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditor.UIElements;
  4. using UnityEngine.UIElements;
  5. using System;
  6. using System.Reflection;
  7. namespace GraphProcessor
  8. {
  9. public class GraphInspector : Editor
  10. {
  11. protected VisualElement root;
  12. protected BaseGraph graph;
  13. protected ExposedParameterFieldFactory exposedParameterFactory;
  14. VisualElement parameterContainer;
  15. protected virtual void OnEnable()
  16. {
  17. graph = target as BaseGraph;
  18. graph.onExposedParameterListChanged += UpdateExposedParameters;
  19. graph.onExposedParameterModified += UpdateExposedParameters;
  20. if (exposedParameterFactory == null)
  21. exposedParameterFactory = new ExposedParameterFieldFactory(graph);
  22. }
  23. protected virtual void OnDisable()
  24. {
  25. graph.onExposedParameterListChanged -= UpdateExposedParameters;
  26. graph.onExposedParameterModified -= UpdateExposedParameters;
  27. exposedParameterFactory?.Dispose(); // Graphs that created in GraphBehaviour sometimes gives null ref.
  28. exposedParameterFactory = null;
  29. }
  30. public sealed override VisualElement CreateInspectorGUI()
  31. {
  32. root = new VisualElement();
  33. CreateInspector();
  34. return root;
  35. }
  36. protected virtual void CreateInspector()
  37. {
  38. parameterContainer = new VisualElement{
  39. name = "ExposedParameters"
  40. };
  41. FillExposedParameters(parameterContainer);
  42. root.Add(parameterContainer);
  43. }
  44. protected void FillExposedParameters(VisualElement parameterContainer)
  45. {
  46. if (graph.exposedParameters.Count != 0)
  47. parameterContainer.Add(new Label("Exposed Parameters:"));
  48. foreach (var param in graph.exposedParameters)
  49. {
  50. if (param.settings.isHidden)
  51. continue;
  52. var field = exposedParameterFactory.GetParameterValueField(param, (newValue) => {
  53. param.value = newValue;
  54. serializedObject.ApplyModifiedProperties();
  55. graph.NotifyExposedParameterValueChanged(param);
  56. });
  57. parameterContainer.Add(field);
  58. }
  59. }
  60. void UpdateExposedParameters(ExposedParameter param) => UpdateExposedParameters();
  61. void UpdateExposedParameters()
  62. {
  63. parameterContainer.Clear();
  64. FillExposedParameters(parameterContainer);
  65. }
  66. // Don't use ImGUI
  67. public sealed override void OnInspectorGUI() {}
  68. }
  69. }