Unity graph tool solution based on different implementation now focused on Unity.Experimental.Graphview
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.8 KiB

using System;
using TNodeCore.Editor;
using TNodeCore.Editor.EditorPersistence;
using TNodeCore.Editor.NodeGraphView;
using TNodeCore.Models;
using TNodeGraphViewImpl.Editor.Cache;
using TNodeGraphViewImpl.Editor.NodeGraphView;
3 years ago
using UnityEditor;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UIElements;
namespace TNodeGraphViewImpl.Editor{
3 years ago
// public class SelectGraphWindow : EditorWindow{
// public EditorWindow parent;
// public Type graphType;
// public static void ShowWindow<T> (GraphEditor<T> parent) where T:GraphData{
// var window = GetWindow<SelectGraphWindow>();
// window.graphType = typeof(T);
// window.Show();
// window.parent = parent;
// }
// private void OnGUI(){
//
// if(GUILayout.Button("Create An Graph")){
// //Add a save file dialog to save the graph
// //Create the graph
// var graphAsset = ScriptableObject.CreateInstance(graphType);
// var path = EditorUtility.SaveFilePanel("Save Graph", "", "", "asset");
// //Save the graph
// AssetDatabase.CreateAsset(graphAsset, path);
// AssetDatabase.SaveAssets();
// AssetDatabase.Refresh();
// //Load the graph
// var graph = AssetDatabase.LoadAssetAtPath<ScriptableObject>(path) as GraphData;
// var graphEditor = parent as IGraphEditor;
// if (graphEditor.GetGraphView() != null){
// graphEditor.GetGraphView().SetGraphData(graph);
// Debug.Log(graph);
// }
// }
// //Drag and drop a graph asset to load it
// if(Event.current.type == EventType.DragUpdated){
// DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
// Event.current.Use();
// }
// }
// }
public abstract class GraphEditor<T> : EditorWindow,IGraphEditor where T:GraphData{
protected BaseDataGraphView<T> GraphView;
[SerializeField]
3 years ago
private VisualTreeAsset mVisualTreeAsset = default;
//Persist editor data ,such as node position,node size ,etc ,in this script object
[FormerlySerializedAs("nodeEditorData")] public GraphEditorData graphEditorData;
private bool _windowShowed=false;
public void CreateGUI(){
3 years ago
// Each editor window contains a root VisualElement object
VisualElement root = rootVisualElement;
3 years ago
// Instantiate UXML
VisualElement labelFromUXML = mVisualTreeAsset.Instantiate();
root.Add(labelFromUXML);
3 years ago
BuildGraphView();
DefineGraphEditorActions();
GraphView.Owner = this;
3 years ago
OnCreate();
}
public void Update(){
if (GraphView == null) return;
if (GraphView.Data != null) return;
if (_windowShowed==false){
_windowShowed = true;
}
}
public void SetupNonRuntime(T graphData){
GraphView.Data = graphData;
GraphView.IsRuntimeGraph = false;
}
3 years ago
private void BuildGraphView(){
GraphView = NodeEditorExtensions.CreateViewComponentFromBaseType<BaseDataGraphView<T>>();
rootVisualElement.Add(GraphView);
GraphView.StretchToParentSize();
}
3 years ago
private void DefineGraphEditorActions(){
//Register a event when user press ctrl + s
rootVisualElement.RegisterCallback<KeyUpEvent>((evt) => {
if (evt.keyCode == KeyCode.S && evt.ctrlKey)
{
Save();
}
});
}
private void Save(){
//if no graph is loaded ,create a file save dialogue
if (GraphView.Data == null)
3 years ago
{
string path = EditorUtility.SaveFilePanel("Save Graph", "", "", "asset");
if (path.Length != 0){
//Create a new asset file with type of GraphDataType
3 years ago
T asset = ScriptableObject.CreateInstance<T>();
AssetDatabase.CreateAsset(asset, path);
AssetDatabase.SaveAssets();
3 years ago
}
}
else{
GraphView.SaveWithEditorData(graphEditorData);
AssetDatabase.Refresh();
}
3 years ago
}
protected virtual void OnCreate(){
}
public void SetGraphView(IBaseDataGraphView graphView){
GraphView = graphView as BaseDataGraphView<T>;
}
public IBaseDataGraphView GetGraphView(){
return GraphView;
}
3 years ago
}
}