From 5436b74313bcc19177421ba8a8a439eab172582e Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Wed, 8 May 2019 14:56:02 +0200 Subject: [PATCH] NodeEditorBase derive from Editor This gives us lifetime stuff like OnEnable, OnDisable, Reset etc in custom editors --- Scripts/Editor/NodeEditorBase.cs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/Scripts/Editor/NodeEditorBase.cs b/Scripts/Editor/NodeEditorBase.cs index ab463e6..7846d81 100644 --- a/Scripts/Editor/NodeEditorBase.cs +++ b/Scripts/Editor/NodeEditorBase.cs @@ -10,13 +10,13 @@ namespace XNodeEditor.Internal { /// Editor Type. Should be the type of the deriving script itself (eg. NodeEditor) /// Attribute Type. The attribute used to connect with the runtime type (eg. CustomNodeEditorAttribute) /// Runtime Type. The ScriptableObject this can be an editor for (eg. Node) - public abstract class NodeEditorBase where A : Attribute, NodeEditorBase.INodeEditorAttrib where T : NodeEditorBase where K : ScriptableObject { + public abstract class NodeEditorBase : Editor where A : Attribute, NodeEditorBase.INodeEditorAttrib where T : NodeEditorBase where K : ScriptableObject { /// Custom editors defined with [CustomNodeEditor] private static Dictionary editorTypes; private static Dictionary editors = new Dictionary(); public NodeEditorWindow window; - public K target; - public SerializedObject serializedObject; + public new K target { get { return _target == base.target ? _target : _target = (K) base.target; } set { base.target = value; } } + private K _target; public static T GetEditor(K target, NodeEditorWindow window) { if (target == null) return null; @@ -24,16 +24,12 @@ namespace XNodeEditor.Internal { if (!editors.TryGetValue(target, out editor)) { Type type = target.GetType(); Type editorType = GetEditorType(type); - editor = Activator.CreateInstance(editorType) as T; - editor.target = target; - editor.serializedObject = new SerializedObject(target); + editor = (T) Editor.CreateEditor(target, editorType); editor.window = window; - editor.OnCreate(); 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.serializedObject == null) editor.serializedObject = new SerializedObject(target); return editor; } @@ -60,9 +56,6 @@ namespace XNodeEditor.Internal { } } - /// Called on creation, after references have been set - public virtual void OnCreate() { } - public interface INodeEditorAttrib { Type GetInspectedType(); }