using UnityEditor; using UnityEngine; using UnityEngine.UIElements; public class FoldWindow : EditorWindow { const string GraphPath = "Assets/FoldGraph.asset"; FoldGraphView graphView; FoldGraph graphAsset; [MenuItem("Tools/yum_food/Fold")] public static void Open() { var window = GetWindow(); window.titleContent = new GUIContent("Fold"); window.Show(); } void OnEnable() { graphAsset = LoadOrCreateGraph(); ConstructGraphView(); Undo.undoRedoPerformed += OnUndoRedo; } void OnDisable() { if (graphView != null) { rootVisualElement.Remove(graphView); graphView.Dispose(); graphView = null; } AssetDatabase.SaveAssets(); Undo.undoRedoPerformed -= OnUndoRedo; } void OnFocus() { // If we lost the graph view for any reason, rebuild it. Otherwise, reload to match the asset on disk. if (graphView == null) { graphAsset = LoadOrCreateGraph(); ConstructGraphView(); } else { graphView.Reload(); } } FoldGraph LoadOrCreateGraph() { var asset = AssetDatabase.LoadAssetAtPath(GraphPath); if (asset == null) { asset = CreateInstance(); AssetDatabase.CreateAsset(asset, GraphPath); AssetDatabase.SaveAssets(); } return asset; } void ConstructGraphView() { graphView = new FoldGraphView(this, graphAsset) { name = "Fold Graph" }; graphView.style.flexGrow = 1f; graphView.style.width = Length.Percent(100); graphView.style.height = Length.Percent(100); rootVisualElement.Add(graphView); } void OnUndoRedo() { graphView?.Reload(); } public void ShowNotification(string message) { if (string.IsNullOrEmpty(message)) return; ShowNotification(new GUIContent(message)); } }