From 794d1aefc7046ee55a690a6c7dd0902fc0ec40c7 Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Wed, 27 Jun 2018 01:01:07 +0200 Subject: [PATCH 1/3] Begun reworking editor caching --- Scripts/Editor/NodeEditorBase.cs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/Scripts/Editor/NodeEditorBase.cs b/Scripts/Editor/NodeEditorBase.cs index 14ef5ce..6153326 100644 --- a/Scripts/Editor/NodeEditorBase.cs +++ b/Scripts/Editor/NodeEditorBase.cs @@ -2,36 +2,42 @@ using System.Collections; using System.Collections.Generic; using System.Reflection; -using UnityEngine; using UnityEditor; +using UnityEngine; namespace XNodeEditor.Internal { /// Handles caching of custom editor classes and their target types. Accessible with GetEditor(Type type) - public class NodeEditorBase where A : Attribute, NodeEditorBase.INodeEditorAttrib where T : NodeEditorBase where K : ScriptableObject { + public class NodeEditorBase where A : Attribute, NodeEditorBase.INodeEditorAttrib where T : NodeEditorBase where K : ScriptableObject { /// Custom editors defined with [CustomNodeEditor] - private static Dictionary editors; + private static Dictionary editorTemplates; + private static Dictionary editors = new Dictionary(); public K target; public SerializedObject serializedObject; public static T GetEditor(K target) { if (target == null) return null; - Type type = target.GetType(); - T editor = GetEditor(type); - editor.target = target; - editor.serializedObject = new SerializedObject(target); - return editor; + if (!editors.ContainsKey(target)) { + Type type = target.GetType(); + T editor = GetEditor(type); + editors.Add(target, Activator.CreateInstance(editor.GetType()) as T); + editors[target].target = target; + editors[target].serializedObject = new SerializedObject(target); + return editor; + } + return editors[target]; + } private static T GetEditor(Type type) { if (type == null) return null; - if (editors == null) CacheCustomEditors(); - if (editors.ContainsKey(type)) return editors[type]; + if (editorTemplates == null) CacheCustomEditors(); + if (editorTemplates.ContainsKey(type)) return editorTemplates[type]; //If type isn't found, try base type return GetEditor(type.BaseType); } private static void CacheCustomEditors() { - editors = new Dictionary(); + editorTemplates = new Dictionary(); //Get all classes deriving from NodeEditor via reflection Type[] nodeEditors = XNodeEditor.NodeEditorWindow.GetDerivedTypes(typeof(T)); @@ -40,7 +46,7 @@ namespace XNodeEditor.Internal { if (attribs == null || attribs.Length == 0) continue; if (nodeEditors[i].IsAbstract) continue; A attrib = attribs[0] as A; - editors.Add(attrib.GetInspectedType(), Activator.CreateInstance(nodeEditors[i]) as T); + editorTemplates.Add(attrib.GetInspectedType(), Activator.CreateInstance(nodeEditors[i]) as T); } } From fb61577713d29e6ddd7339ae84870bd60e12104c Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Fri, 29 Jun 2018 00:17:49 +0200 Subject: [PATCH 2/3] Fixed critical bug --- Scripts/Editor/NodeEditorBase.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Scripts/Editor/NodeEditorBase.cs b/Scripts/Editor/NodeEditorBase.cs index 6153326..a327b43 100644 --- a/Scripts/Editor/NodeEditorBase.cs +++ b/Scripts/Editor/NodeEditorBase.cs @@ -22,10 +22,8 @@ namespace XNodeEditor.Internal { editors.Add(target, Activator.CreateInstance(editor.GetType()) as T); editors[target].target = target; editors[target].serializedObject = new SerializedObject(target); - return editor; } return editors[target]; - } private static T GetEditor(Type type) { From 2ee0e426687d722b8a4bebd4109fe4f3b677463c Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Sat, 30 Jun 2018 16:16:11 +0200 Subject: [PATCH 3/3] Faster editor caching and less error prone --- Scripts/Editor/NodeEditorBase.cs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Scripts/Editor/NodeEditorBase.cs b/Scripts/Editor/NodeEditorBase.cs index a327b43..8ab610e 100644 --- a/Scripts/Editor/NodeEditorBase.cs +++ b/Scripts/Editor/NodeEditorBase.cs @@ -9,7 +9,7 @@ namespace XNodeEditor.Internal { /// Handles caching of custom editor classes and their target types. Accessible with GetEditor(Type type) public class NodeEditorBase where A : Attribute, NodeEditorBase.INodeEditorAttrib where T : NodeEditorBase where K : ScriptableObject { /// Custom editors defined with [CustomNodeEditor] - private static Dictionary editorTemplates; + private static Dictionary editorTypes; private static Dictionary editors = new Dictionary(); public K target; public SerializedObject serializedObject; @@ -18,33 +18,36 @@ namespace XNodeEditor.Internal { if (target == null) return null; if (!editors.ContainsKey(target)) { Type type = target.GetType(); - T editor = GetEditor(type); - editors.Add(target, Activator.CreateInstance(editor.GetType()) as T); + Type editorType = GetEditorType(type); + editors.Add(target, Activator.CreateInstance(editorType) as T); editors[target].target = target; editors[target].serializedObject = new SerializedObject(target); } - return editors[target]; + T editor = editors[target]; + if (editor.target == null) editor.target = target; + if (editor.serializedObject == null) editor.serializedObject = new SerializedObject(target); + return editor; } - private static T GetEditor(Type type) { + private static Type GetEditorType(Type type) { if (type == null) return null; - if (editorTemplates == null) CacheCustomEditors(); - if (editorTemplates.ContainsKey(type)) return editorTemplates[type]; + if (editorTypes == null) CacheCustomEditors(); + if (editorTypes.ContainsKey(type)) return editorTypes[type]; //If type isn't found, try base type - return GetEditor(type.BaseType); + return GetEditorType(type.BaseType); } private static void CacheCustomEditors() { - editorTemplates = new Dictionary(); + editorTypes = new Dictionary(); //Get all classes deriving from NodeEditor via reflection Type[] nodeEditors = XNodeEditor.NodeEditorWindow.GetDerivedTypes(typeof(T)); for (int i = 0; i < nodeEditors.Length; i++) { + if (nodeEditors[i].IsAbstract) continue; var attribs = nodeEditors[i].GetCustomAttributes(typeof(A), false); if (attribs == null || attribs.Length == 0) continue; - if (nodeEditors[i].IsAbstract) continue; A attrib = attribs[0] as A; - editorTemplates.Add(attrib.GetInspectedType(), Activator.CreateInstance(nodeEditors[i]) as T); + editorTypes.Add(attrib.GetInspectedType(), nodeEditors[i]); } }