diff --git a/Scripts/Editor/NodeEditor.cs b/Scripts/Editor/NodeEditor.cs index b237a21..03a8ee0 100644 --- a/Scripts/Editor/NodeEditor.cs +++ b/Scripts/Editor/NodeEditor.cs @@ -129,8 +129,32 @@ namespace XNodeEditor { public void Rename(string newName) { if (newName == null || newName.Trim() == "") newName = NodeEditorUtilities.NodeDefaultName(target.GetType()); target.name = newName; - AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target)); + TriggerRenameNotifications(target); } + + /// + /// Triggers OnRename on all nodes in the graph after a node's name changes, + /// reimporting asset files for all subsequently renamed nodes. + /// + public static void TriggerRenameNotifications(XNode.Node renamedNode) { + List graphNodes = renamedNode.graph.nodes; + string[] oldNodeNames = new string[graphNodes.Count]; + // Notify all nodes, allow them to further change names accordingly + for (int i = 0; i < graphNodes.Count; i++) { + XNode.Node node = graphNodes[i]; + oldNodeNames[i] = node.name; + node.OnRename(renamedNode); + } + + // After all user-driven modifications have been resolved, update all assets whose node names have changed + // (And always reimport the node which got renamed in the first place) + for (int i = 0; i < graphNodes.Count; i++) { + if (graphNodes[i] == renamedNode || graphNodes[i].name != oldNodeNames[i]) { + AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(graphNodes[i])); + } + } + } + [AttributeUsage(AttributeTargets.Class)] public class CustomNodeEditorAttribute : Attribute, diff --git a/Scripts/Editor/RenamePopup.cs b/Scripts/Editor/RenamePopup.cs index 564374e..72d76fa 100644 --- a/Scripts/Editor/RenamePopup.cs +++ b/Scripts/Editor/RenamePopup.cs @@ -49,7 +49,7 @@ namespace XNodeEditor { if (input == null || input.Trim() == "") { if (GUILayout.Button("Revert to default") || (e.isKey && e.keyCode == KeyCode.Return)) { target.name = NodeEditorUtilities.NodeDefaultName(target.GetType()); - AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target)); + NodeEditor.TriggerRenameNotifications((XNode.Node)target); Close(); target.TriggerOnValidate(); } @@ -58,7 +58,7 @@ namespace XNodeEditor { else { if (GUILayout.Button("Apply") || (e.isKey && e.keyCode == KeyCode.Return)) { target.name = input; - AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target)); + NodeEditor.TriggerRenameNotifications((XNode.Node)target); Close(); target.TriggerOnValidate(); } diff --git a/Scripts/Node.cs b/Scripts/Node.cs index a07679a..6398c1e 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -254,6 +254,9 @@ namespace XNode { /// Called after a connection is removed from this port /// Output or Input public virtual void OnRemoveConnection(NodePort port) { } + + /// Called after a node's name has changed. + public virtual void OnRename(Node node) { } /// Disconnect everything from this node public void ClearConnections() {