BaseGraphWindow.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System.Linq;
  2. using System;
  3. using System.Xml.Linq;
  4. using UnityEngine;
  5. using UnityEditor;
  6. using UnityEditor.Callbacks;
  7. using UnityEngine.UIElements;
  8. using UnityEngine.SceneManagement;
  9. using UnityEditor.SceneManagement;
  10. namespace GraphProcessor
  11. {
  12. [System.Serializable]
  13. public abstract class BaseGraphWindow : EditorWindow
  14. {
  15. protected VisualElement rootView;
  16. protected BaseGraphView graphView;
  17. [SerializeField]
  18. protected BaseGraph graph;
  19. readonly string graphWindowStyle = "GraphProcessorStyles/BaseGraphView";
  20. public bool isGraphLoaded
  21. {
  22. get { return graphView != null && graphView.graph != null; }
  23. }
  24. bool reloadWorkaround = false;
  25. public event Action< BaseGraph > graphLoaded;
  26. public event Action< BaseGraph > graphUnloaded;
  27. /// <summary>
  28. /// Called by Unity when the window is enabled / opened
  29. /// </summary>
  30. protected virtual void OnEnable()
  31. {
  32. InitializeRootView();
  33. if (graph != null)
  34. LoadGraph();
  35. else
  36. reloadWorkaround = true;
  37. }
  38. protected virtual void Update()
  39. {
  40. // Workaround for the Refresh option of the editor window:
  41. // When Refresh is clicked, OnEnable is called before the serialized data in the
  42. // editor window is deserialized, causing the graph view to not be loaded
  43. if (reloadWorkaround && graph != null)
  44. {
  45. LoadGraph();
  46. reloadWorkaround = false;
  47. }
  48. }
  49. void LoadGraph()
  50. {
  51. // We wait for the graph to be initialized
  52. if (graph.isEnabled)
  53. InitializeGraph(graph);
  54. else
  55. graph.onEnabled += () => InitializeGraph(graph);
  56. }
  57. /// <summary>
  58. /// Called by Unity when the window is disabled (happens on domain reload)
  59. /// </summary>
  60. protected virtual void OnDisable()
  61. {
  62. if (graph != null && graphView != null)
  63. graphView.SaveGraphToDisk();
  64. }
  65. /// <summary>
  66. /// Called by Unity when the window is closed
  67. /// </summary>
  68. protected virtual void OnDestroy() { }
  69. void InitializeRootView()
  70. {
  71. rootView = base.rootVisualElement;
  72. rootView.name = "graphRootView";
  73. rootView.styleSheets.Add(Resources.Load<StyleSheet>(graphWindowStyle));
  74. }
  75. public void InitializeGraph(BaseGraph graph)
  76. {
  77. if (this.graph != null && graph != this.graph)
  78. {
  79. // Save the graph to the disk
  80. EditorUtility.SetDirty(this.graph);
  81. AssetDatabase.SaveAssets();
  82. // Unload the graph
  83. graphUnloaded?.Invoke(this.graph);
  84. }
  85. graphLoaded?.Invoke(graph);
  86. this.graph = graph;
  87. if (graphView != null)
  88. rootView.Remove(graphView);
  89. //Initialize will provide the BaseGraphView
  90. InitializeWindow(graph);
  91. graphView = rootView.Children().FirstOrDefault(e => e is BaseGraphView) as BaseGraphView;
  92. if (graphView == null)
  93. {
  94. Debug.LogError("GraphView has not been added to the BaseGraph root view !");
  95. return ;
  96. }
  97. graphView.Initialize(graph);
  98. InitializeGraphView(graphView);
  99. // TOOD: onSceneLinked...
  100. if (graph.IsLinkedToScene())
  101. LinkGraphWindowToScene(graph.GetLinkedScene());
  102. else
  103. graph.onSceneLinked += LinkGraphWindowToScene;
  104. }
  105. void LinkGraphWindowToScene(Scene scene)
  106. {
  107. EditorSceneManager.sceneClosed += CloseWindowWhenSceneIsClosed;
  108. void CloseWindowWhenSceneIsClosed(Scene closedScene)
  109. {
  110. if (scene == closedScene)
  111. {
  112. Close();
  113. EditorSceneManager.sceneClosed -= CloseWindowWhenSceneIsClosed;
  114. }
  115. }
  116. }
  117. public virtual void OnGraphDeleted()
  118. {
  119. if (graph != null && graphView != null)
  120. rootView.Remove(graphView);
  121. graphView = null;
  122. }
  123. protected abstract void InitializeWindow(BaseGraph graph);
  124. protected virtual void InitializeGraphView(BaseGraphView view) {}
  125. }
  126. }