mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 09:16:01 +08:00
Begun reworking editor caching
This commit is contained in:
parent
5a88ddbda6
commit
794d1aefc7
@ -2,36 +2,42 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using UnityEngine;
|
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
namespace XNodeEditor.Internal {
|
namespace XNodeEditor.Internal {
|
||||||
/// <summary> Handles caching of custom editor classes and their target types. Accessible with GetEditor(Type type) </summary>
|
/// <summary> Handles caching of custom editor classes and their target types. Accessible with GetEditor(Type type) </summary>
|
||||||
public class NodeEditorBase<T, A, K> where A : Attribute, NodeEditorBase<T, A, K>.INodeEditorAttrib where T : NodeEditorBase<T, A, K> where K : ScriptableObject {
|
public class NodeEditorBase<T, A, K> where A : Attribute, NodeEditorBase<T, A, K>.INodeEditorAttrib where T : NodeEditorBase<T, A, K> where K : ScriptableObject {
|
||||||
/// <summary> Custom editors defined with [CustomNodeEditor] </summary>
|
/// <summary> Custom editors defined with [CustomNodeEditor] </summary>
|
||||||
private static Dictionary<Type, T> editors;
|
private static Dictionary<Type, T> editorTemplates;
|
||||||
|
private static Dictionary<K, T> editors = new Dictionary<K, T>();
|
||||||
public K target;
|
public K target;
|
||||||
public SerializedObject serializedObject;
|
public SerializedObject serializedObject;
|
||||||
|
|
||||||
public static T GetEditor(K target) {
|
public static T GetEditor(K target) {
|
||||||
if (target == null) return null;
|
if (target == null) return null;
|
||||||
|
if (!editors.ContainsKey(target)) {
|
||||||
Type type = target.GetType();
|
Type type = target.GetType();
|
||||||
T editor = GetEditor(type);
|
T editor = GetEditor(type);
|
||||||
editor.target = target;
|
editors.Add(target, Activator.CreateInstance(editor.GetType()) as T);
|
||||||
editor.serializedObject = new SerializedObject(target);
|
editors[target].target = target;
|
||||||
|
editors[target].serializedObject = new SerializedObject(target);
|
||||||
return editor;
|
return editor;
|
||||||
}
|
}
|
||||||
|
return editors[target];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private static T GetEditor(Type type) {
|
private static T GetEditor(Type type) {
|
||||||
if (type == null) return null;
|
if (type == null) return null;
|
||||||
if (editors == null) CacheCustomEditors();
|
if (editorTemplates == null) CacheCustomEditors();
|
||||||
if (editors.ContainsKey(type)) return editors[type];
|
if (editorTemplates.ContainsKey(type)) return editorTemplates[type];
|
||||||
//If type isn't found, try base type
|
//If type isn't found, try base type
|
||||||
return GetEditor(type.BaseType);
|
return GetEditor(type.BaseType);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void CacheCustomEditors() {
|
private static void CacheCustomEditors() {
|
||||||
editors = new Dictionary<Type, T>();
|
editorTemplates = new Dictionary<Type, T>();
|
||||||
|
|
||||||
//Get all classes deriving from NodeEditor via reflection
|
//Get all classes deriving from NodeEditor via reflection
|
||||||
Type[] nodeEditors = XNodeEditor.NodeEditorWindow.GetDerivedTypes(typeof(T));
|
Type[] nodeEditors = XNodeEditor.NodeEditorWindow.GetDerivedTypes(typeof(T));
|
||||||
@ -40,7 +46,7 @@ namespace XNodeEditor.Internal {
|
|||||||
if (attribs == null || attribs.Length == 0) continue;
|
if (attribs == null || attribs.Length == 0) continue;
|
||||||
if (nodeEditors[i].IsAbstract) continue;
|
if (nodeEditors[i].IsAbstract) continue;
|
||||||
A attrib = attribs[0] as A;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user