1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-21 01:36:03 +08:00

NodeEditorBase derive from Editor

This gives us lifetime stuff like OnEnable, OnDisable, Reset etc in custom editors
This commit is contained in:
Thor Brigsted 2019-05-08 14:56:02 +02:00
parent 9e7e7c4c9b
commit 5436b74313

View File

@ -10,13 +10,13 @@ namespace XNodeEditor.Internal {
/// <typeparam name="T">Editor Type. Should be the type of the deriving script itself (eg. NodeEditor) </typeparam> /// <typeparam name="T">Editor Type. Should be the type of the deriving script itself (eg. NodeEditor) </typeparam>
/// <typeparam name="A">Attribute Type. The attribute used to connect with the runtime type (eg. CustomNodeEditorAttribute) </typeparam> /// <typeparam name="A">Attribute Type. The attribute used to connect with the runtime type (eg. CustomNodeEditorAttribute) </typeparam>
/// <typeparam name="K">Runtime Type. The ScriptableObject this can be an editor for (eg. Node) </typeparam> /// <typeparam name="K">Runtime Type. The ScriptableObject this can be an editor for (eg. Node) </typeparam>
public abstract class NodeEditorBase<T, A, K> where A : Attribute, NodeEditorBase<T, A, K>.INodeEditorAttrib where T : NodeEditorBase<T, A, K> where K : ScriptableObject { public abstract class NodeEditorBase<T, A, K> : Editor 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, Type> editorTypes; private static Dictionary<Type, Type> editorTypes;
private static Dictionary<K, T> editors = new Dictionary<K, T>(); private static Dictionary<K, T> editors = new Dictionary<K, T>();
public NodeEditorWindow window; public NodeEditorWindow window;
public K target; public new K target { get { return _target == base.target ? _target : _target = (K) base.target; } set { base.target = value; } }
public SerializedObject serializedObject; private K _target;
public static T GetEditor(K target, NodeEditorWindow window) { public static T GetEditor(K target, NodeEditorWindow window) {
if (target == null) return null; if (target == null) return null;
@ -24,16 +24,12 @@ namespace XNodeEditor.Internal {
if (!editors.TryGetValue(target, out editor)) { if (!editors.TryGetValue(target, out editor)) {
Type type = target.GetType(); Type type = target.GetType();
Type editorType = GetEditorType(type); Type editorType = GetEditorType(type);
editor = Activator.CreateInstance(editorType) as T; editor = (T) Editor.CreateEditor(target, editorType);
editor.target = target;
editor.serializedObject = new SerializedObject(target);
editor.window = window; editor.window = window;
editor.OnCreate();
editors.Add(target, editor); editors.Add(target, editor);
} }
if (editor.target == null) editor.target = target; if (editor.target == null) editor.Initialize(new UnityEngine.Object[] { target });
if (editor.window != window) editor.window = window; if (editor.window != window) editor.window = window;
if (editor.serializedObject == null) editor.serializedObject = new SerializedObject(target);
return editor; return editor;
} }
@ -60,9 +56,6 @@ namespace XNodeEditor.Internal {
} }
} }
/// <summary> Called on creation, after references have been set </summary>
public virtual void OnCreate() { }
public interface INodeEditorAttrib { public interface INodeEditorAttrib {
Type GetInspectedType(); Type GetInspectedType();
} }