diff --git a/Examples/NewNodeGraph.asset b/Examples/NewNodeGraph.asset new file mode 100644 index 0000000..62d3ed1 Binary files /dev/null and b/Examples/NewNodeGraph.asset differ diff --git a/Examples/NewNodeGraph.asset.meta b/Examples/NewNodeGraph.asset.meta new file mode 100644 index 0000000..fc8326b --- /dev/null +++ b/Examples/NewNodeGraph.asset.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 1452552e49747df45b25f74d307c25b6 +timeCreated: 1506021986 +licenseType: Free +NativeFormatImporter: + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/Editor/NodeEditorToolbar.cs b/Scripts/Editor/NodeEditorToolbar.cs index c1ae734..c540bf0 100644 --- a/Scripts/Editor/NodeEditorToolbar.cs +++ b/Scripts/Editor/NodeEditorToolbar.cs @@ -32,12 +32,11 @@ public partial class NodeEditorWindow { public void FileContextMenu() { GenericMenu contextMenu = new GenericMenu(); - contextMenu.AddItem(new GUIContent("Create New"), false, null); - contextMenu.AddItem(new GUIContent("Load"), false, Load); + contextMenu.AddItem(new GUIContent("Create New"), false, New); contextMenu.AddSeparator(""); contextMenu.AddItem(new GUIContent("Save"), false, Save); - contextMenu.AddItem(new GUIContent("Save As"), false, null); + contextMenu.AddItem(new GUIContent("Save As"), false, SaveAs); contextMenu.DropDown(new Rect(5f, 17f, 0f, 0f)); } diff --git a/Scripts/Editor/NodeEditorWindow.cs b/Scripts/Editor/NodeEditorWindow.cs index f3fb17a..2423d8d 100644 --- a/Scripts/Editor/NodeEditorWindow.cs +++ b/Scripts/Editor/NodeEditorWindow.cs @@ -2,39 +2,61 @@ using System.Collections.Generic; using UnityEngine; using UnityEditor; -using System.IO; +using System.IO; +using UnityEditor.Callbacks; [InitializeOnLoad] -public partial class NodeEditorWindow : EditorWindow { - - string saved; - +public partial class NodeEditorWindow : EditorWindow { public Dictionary portConnectionPoints { get { return _portConnectionPoints; } } private Dictionary _portConnectionPoints = new Dictionary(); private Dictionary portRects = new Dictionary(); + public NodeGraphAsset graphAsset; public NodeGraph graph { get { return _graph != null ? _graph : _graph = new NodeGraph(); } } - public NodeGraph _graph; + public NodeGraph _graph; public Vector2 panOffset { get { return _panOffset; } set { _panOffset = value; Repaint(); } } - private Vector2 _panOffset; + private Vector2 _panOffset; public float zoom { get { return _zoom; } set { _zoom = Mathf.Clamp(value, 1f, 5f); Repaint(); } } private float _zoom = 1; partial void OnEnable(); [MenuItem("Window/UNEC")] - static void Init() { + static NodeEditorWindow Init() { NodeEditorWindow w = CreateInstance(); w.titleContent = new GUIContent("UNEC"); w.wantsMouseMove = true; w.Show(); + return w; } public void Save() { - saved = graph.Serialize(); + if (graphAsset == null) SaveAs(); + else if (AssetDatabase.Contains(graphAsset)) { + graphAsset.json = graph.Serialize(); + EditorUtility.SetDirty(graphAsset); + AssetDatabase.SaveAssets(); + } + else SaveAs(); } - public void Load() { - _graph = NodeGraph.Deserialize(saved); + public void SaveAs() { + string path = EditorUtility.SaveFilePanelInProject("Save NodeGraph", "NewNodeGraph", "asset", ""); + if (string.IsNullOrEmpty(path)) return; + else { + NodeGraphAsset existingGraphAsset = AssetDatabase.LoadAssetAtPath(path); + if (existingGraphAsset != null) graphAsset = existingGraphAsset; + else { + graphAsset = new NodeGraphAsset(); + AssetDatabase.CreateAsset(graphAsset, path); + } + graphAsset.json = graph.Serialize(); + EditorUtility.SetDirty(graphAsset); + AssetDatabase.SaveAssets(); + } + } + + public void New() { + _graph = new NodeGraph(); } private void DraggableWindow(int windowID) { @@ -60,4 +82,15 @@ public partial class NodeEditorWindow : EditorWindow { selectedNode = node; } + [OnOpenAsset(0)] + public static bool OnOpen(int instanceID, int line) { + NodeGraphAsset nodeGraphAsset = EditorUtility.InstanceIDToObject(instanceID) as NodeGraphAsset; + if (nodeGraphAsset != null) { + NodeEditorWindow w = Init(); + w.graphAsset = nodeGraphAsset; + w._graph = nodeGraphAsset.nodeGraph; + return true; + } + return false; + } } \ No newline at end of file diff --git a/Scripts/NodeGraph.cs b/Scripts/NodeGraph.cs index 073df97..863ad36 100644 --- a/Scripts/NodeGraph.cs +++ b/Scripts/NodeGraph.cs @@ -87,7 +87,7 @@ public class NodeGraph { } private class NodeTyper { - public string nodeType; + public string nodeType = "Node"; } } diff --git a/Scripts/NodeGraphAsset.cs b/Scripts/NodeGraphAsset.cs new file mode 100644 index 0000000..d9b2906 --- /dev/null +++ b/Scripts/NodeGraphAsset.cs @@ -0,0 +1,13 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using System; + +[CreateAssetMenu(fileName = "NewNodeGraph", menuName = "Node Graph")] +public class NodeGraphAsset : ScriptableObject { + public string json { get { return _json; } set { _json = value; _nodeGraph = null; } } + [SerializeField] private string _json; + + public NodeGraph nodeGraph { get { return _nodeGraph != null ? _nodeGraph : _nodeGraph = NodeGraph.Deserialize(json); } } + [NonSerialized] private NodeGraph _nodeGraph = null; +} diff --git a/Scripts/NodeGraphAsset.cs.meta b/Scripts/NodeGraphAsset.cs.meta new file mode 100644 index 0000000..718d0eb --- /dev/null +++ b/Scripts/NodeGraphAsset.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 803fd86571b50524f807cf1e86777b1a +timeCreated: 1506020816 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: