Skip to content

Commit 2ee0e42

Browse files
committed
Faster editor caching and less error prone
1 parent fb61577 commit 2ee0e42

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

Scripts/Editor/NodeEditorBase.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace XNodeEditor.Internal {
99
/// <summary> Handles caching of custom editor classes and their target types. Accessible with GetEditor(Type type) </summary>
1010
public class NodeEditorBase<T, A, K> where A : Attribute, NodeEditorBase<T, A, K>.INodeEditorAttrib where T : NodeEditorBase<T, A, K> where K : ScriptableObject {
1111
/// <summary> Custom editors defined with [CustomNodeEditor] </summary>
12-
private static Dictionary<Type, T> editorTemplates;
12+
private static Dictionary<Type, Type> editorTypes;
1313
private static Dictionary<K, T> editors = new Dictionary<K, T>();
1414
public K target;
1515
public SerializedObject serializedObject;
@@ -18,33 +18,36 @@ public static T GetEditor(K target) {
1818
if (target == null) return null;
1919
if (!editors.ContainsKey(target)) {
2020
Type type = target.GetType();
21-
T editor = GetEditor(type);
22-
editors.Add(target, Activator.CreateInstance(editor.GetType()) as T);
21+
Type editorType = GetEditorType(type);
22+
editors.Add(target, Activator.CreateInstance(editorType) as T);
2323
editors[target].target = target;
2424
editors[target].serializedObject = new SerializedObject(target);
2525
}
26-
return editors[target];
26+
T editor = editors[target];
27+
if (editor.target == null) editor.target = target;
28+
if (editor.serializedObject == null) editor.serializedObject = new SerializedObject(target);
29+
return editor;
2730
}
2831

29-
private static T GetEditor(Type type) {
32+
private static Type GetEditorType(Type type) {
3033
if (type == null) return null;
31-
if (editorTemplates == null) CacheCustomEditors();
32-
if (editorTemplates.ContainsKey(type)) return editorTemplates[type];
34+
if (editorTypes == null) CacheCustomEditors();
35+
if (editorTypes.ContainsKey(type)) return editorTypes[type];
3336
//If type isn't found, try base type
34-
return GetEditor(type.BaseType);
37+
return GetEditorType(type.BaseType);
3538
}
3639

3740
private static void CacheCustomEditors() {
38-
editorTemplates = new Dictionary<Type, T>();
41+
editorTypes = new Dictionary<Type, Type>();
3942

4043
//Get all classes deriving from NodeEditor via reflection
4144
Type[] nodeEditors = XNodeEditor.NodeEditorWindow.GetDerivedTypes(typeof(T));
4245
for (int i = 0; i < nodeEditors.Length; i++) {
46+
if (nodeEditors[i].IsAbstract) continue;
4347
var attribs = nodeEditors[i].GetCustomAttributes(typeof(A), false);
4448
if (attribs == null || attribs.Length == 0) continue;
45-
if (nodeEditors[i].IsAbstract) continue;
4649
A attrib = attribs[0] as A;
47-
editorTemplates.Add(attrib.GetInspectedType(), Activator.CreateInstance(nodeEditors[i]) as T);
50+
editorTypes.Add(attrib.GetInspectedType(), nodeEditors[i]);
4851
}
4952
}
5053

0 commit comments

Comments
 (0)