2025-05-17 12:34:37 +01:00

75 lines
2.2 KiB
C#

using System;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
namespace AssetGraph.Core
{
public class AssetGraphEditorWindow : EditorWindow
{
[SerializeField] private AssetGraphData currentGraph;
[SerializeField] private SerializedObject serializedObject;
[SerializeField] private AssetGraphViewWindow currentView;
public AssetGraphData CurrentGraph => currentGraph;
public static void Open(AssetGraphData asset)
{
var existingWindows = Resources.FindObjectsOfTypeAll<AssetGraphEditorWindow>();
foreach (var w in existingWindows)
{
if (w.CurrentGraph == asset)
{
w.Focus(); // 👁 focus the OG window
return;
}
}
var window = CreateWindow<AssetGraphEditorWindow>(typeof(SceneView));
window.titleContent = new GUIContent($"{asset.name}",
EditorGUIUtility.ObjectContent(null, typeof(AssetGraphData)).image);
window.Load(asset);
window.Focus();
}
void OnEnable()
{
if(currentGraph != null)
DrawGraph();
}
private void OnGUI()
{
if (currentGraph != null)
{
if(EditorUtility.IsDirty(currentGraph))
this.hasUnsavedChanges = true;
else
this.hasUnsavedChanges = false;
}
}
public void Load(AssetGraphData asset)
{
currentGraph = asset;
DrawGraph();
}
public void DrawGraph()
{
serializedObject = new SerializedObject(currentGraph);
currentView = new AssetGraphViewWindow(serializedObject, this);
currentView.graphViewChanged += OnChange;
rootVisualElement.style.flexGrow = 1;
rootVisualElement.Add(currentView);
}
private GraphViewChange OnChange(GraphViewChange graphviewchange)
{
EditorUtility.SetDirty(currentGraph);
return graphviewchange;
}
}
}