summaryrefslogtreecommitdiff
path: root/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/InspectorExt.cs
blob: 8ec839df5d35708226753a8145e97dc72aefe6ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Reflection;
using UnityEngine;
using UnityEditor;

public abstract class InspectorExt : Editor
{
    protected Editor defaultEditor;

    protected abstract string defaultEditorName { get; }

    bool s_MoreInfo = true;
    static readonly GUIContent more = EditorGUIUtility.TrTextContent("More Information", null, (Texture2D)null);

    public virtual void OnEnable()
    {
        defaultEditor = Editor.CreateEditor(targets, Type.GetType(defaultEditorName));
        if(defaultEditor == null)
            Debug.LogError("No such editor class \""+ defaultEditorName + "\"");
        TryInvokeDefaultMethod("OnEnable");
    }

    public virtual void OnDisable()
    {
        //When OnDisable is called, the default editor we created should be destroyed to avoid memory leakage.
        //Also, make sure to call any required methods like OnDisable
        TryInvokeDefaultMethod("OnDisable");
        DestroyImmediate(defaultEditor);

        s_MoreInfo = true;
    }

    public override void  OnInspectorGUI()
    {
        if (defaultEditor)
            defaultEditor.OnInspectorGUI();
    }

    public virtual void OnSceneGUI ()
    {
        TryInvokeDefaultMethod("OnSceneGUI");
    }

    protected bool BeginMore()
    {
        s_MoreInfo = EditorGUILayout.BeginFoldoutHeaderGroup(s_MoreInfo, more, null, null, null);
        return s_MoreInfo;
    }

    protected void EndMore()
    {
        EditorGUILayout.EndFoldoutHeaderGroup();
    }

    protected void TryInvokeDefaultMethod(string method, BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
    {
        MethodInfo disableMethod = defaultEditor.GetType().GetMethod(method, flag);
        if (disableMethod != null)
        {
            disableMethod.Invoke(defaultEditor, null);
        }
    }

}