From 8e0bd964ad9664e9586f4ddb1f7d18dc10bb570a Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Mon, 1 Jul 2019 09:54:51 +0200 Subject: [PATCH] Fixed #107 - OnValidate OnValidate is now called on rename as well. Removed outdated OnValidate fix. It probably never worked anyway. --- Scripts/Editor/NodeEditorGUI.cs | 11 ----------- Scripts/Editor/NodeEditorReflection.cs | 9 +++++++++ Scripts/Editor/RenamePopup.cs | 2 ++ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index a2908b6..6304754 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -282,12 +282,6 @@ namespace XNodeEditor { selectionCache = new List(Selection.objects); } - System.Reflection.MethodInfo onValidate = null; - if (Selection.activeObject != null && Selection.activeObject is XNode.Node) { - onValidate = Selection.activeObject.GetType().GetMethod("OnValidate"); - if (onValidate != null) EditorGUI.BeginChangeCheck(); - } - BeginZoomed(); Vector2 mousePos = Event.current.mousePosition; @@ -420,11 +414,6 @@ namespace XNodeEditor { if (e.type != EventType.Layout && currentActivity == NodeActivity.DragGrid) Selection.objects = preSelection.ToArray(); EndZoomed(); - - //If a change in is detected in the selected node, call OnValidate method. - //This is done through reflection because OnValidate is only relevant in editor, - //and thus, the code should not be included in build. - if (onValidate != null && EditorGUI.EndChangeCheck()) onValidate.Invoke(Selection.activeObject, null); } private bool ShouldBeCulled(XNode.Node node) { diff --git a/Scripts/Editor/NodeEditorReflection.cs b/Scripts/Editor/NodeEditorReflection.cs index 6b8b121..76ec656 100644 --- a/Scripts/Editor/NodeEditorReflection.cs +++ b/Scripts/Editor/NodeEditorReflection.cs @@ -103,6 +103,15 @@ namespace XNodeEditor { } } + /// Call OnValidate on target + public static void TriggerOnValidate(UnityEngine.Object target) { + System.Reflection.MethodInfo onValidate = null; + if (target != null) { + onValidate = target.GetType().GetMethod("OnValidate", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + if (onValidate != null) onValidate.Invoke(target, null); + } + } + public static KeyValuePair[] GetContextMenuMethods(object obj) { Type type = obj.GetType(); MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); diff --git a/Scripts/Editor/RenamePopup.cs b/Scripts/Editor/RenamePopup.cs index 85d229f..4903525 100644 --- a/Scripts/Editor/RenamePopup.cs +++ b/Scripts/Editor/RenamePopup.cs @@ -51,6 +51,7 @@ namespace XNodeEditor { target.name = NodeEditorUtilities.NodeDefaultName(target.GetType()); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target)); Close(); + NodeEditorWindow.TriggerOnValidate(target); } } // Rename asset to input text @@ -59,6 +60,7 @@ namespace XNodeEditor { target.name = input; AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target)); Close(); + NodeEditorWindow.TriggerOnValidate(target); } } }