From afab16c3c02ee488464273213f404a6de4a3d465 Mon Sep 17 00:00:00 2001 From: neko1990 Date: Sat, 9 Dec 2017 10:40:23 +0800 Subject: [PATCH] record graph modification , leave the save action to user. replace UnityEditor.AssetDatabase.SaveAssets with UnityEditor.Undo.RecordObject and UnityEditor.EditorUtility.SetDirty. --- Scripts/NodeGraph.cs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Scripts/NodeGraph.cs b/Scripts/NodeGraph.cs index f71642b..1f4b419 100644 --- a/Scripts/NodeGraph.cs +++ b/Scripts/NodeGraph.cs @@ -18,46 +18,55 @@ namespace XNode { /// Add a node to the graph by type public virtual Node AddNode(Type type) { +#if UNITY_EDITOR + UnityEditor.Undo.RecordObject(this, "Add Node " + type.ToString()); +#endif Node node = ScriptableObject.CreateInstance(type) as Node; + nodes.Add(node); + node.graph = this; #if UNITY_EDITOR if (!Application.isPlaying) { UnityEditor.AssetDatabase.AddObjectToAsset(node, this); - UnityEditor.AssetDatabase.SaveAssets(); node.name = UnityEditor.ObjectNames.NicifyVariableName(node.name); } + UnityEditor.EditorUtility.SetDirty(this); #endif - nodes.Add(node); - node.graph = this; return node; } /// Creates a copy of the original node in the graph public virtual Node CopyNode(Node original) { +#if UNITY_EDITOR + UnityEditor.Undo.RecordObject(this, "Copy Node"); +#endif Node node = ScriptableObject.Instantiate(original); node.ClearConnections(); + nodes.Add(node); + node.graph = this; #if UNITY_EDITOR if (!Application.isPlaying) { UnityEditor.AssetDatabase.AddObjectToAsset(node, this); - UnityEditor.AssetDatabase.SaveAssets(); node.name = UnityEditor.ObjectNames.NicifyVariableName(node.name); } + UnityEditor.EditorUtility.SetDirty(this); #endif - nodes.Add(node); - node.graph = this; return node; } /// Safely remove a node and all its connections /// public void RemoveNode(Node node) { +#if UNITY_EDITOR + UnityEditor.Undo.RecordObject(this, "Remove Node"); +#endif node.ClearConnections(); + nodes.Remove(node); #if UNITY_EDITOR if (!Application.isPlaying) { DestroyImmediate(node, true); - UnityEditor.AssetDatabase.SaveAssets(); } + UnityEditor.EditorUtility.SetDirty(this); #endif - nodes.Remove(node); } /// Remove all nodes and connections from the graph