自定义Transform Inspector


# Transform的默认编辑器类名不叫TransformEditor,而是叫TransformInspector。除了这个,其他基本和CustomRectTransformEditor类似

#if UNITY_EDITOR

using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(Transform))]
[CanEditMultipleObjects]
public class CustomTransformEditor : UnityEditor.Editor
{
    UnityEditor.Editor _editorInstance;

    List _transformList = new List();

    private void OnEnable()
    {
        if (null != _editorInstance)
            return;

        var assembly = Assembly.GetAssembly(typeof(UnityEditor.Editor));
        if (null == assembly) return;
        var editorType = assembly.GetType("UnityEditor.TransformInspector");
        if (null == editorType) return;
        if (null != targets)
        {
            _editorInstance = CreateEditor(targets, editorType);
            var methodOnEnable = editorType.GetMethod("OnEnable", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            if (null != methodOnEnable)
                methodOnEnable.Invoke(_editorInstance, null);

            AllTargetsCheck();
        }
    }

    void AllTargetsCheck()
    {
        for (var i = 0; i < targets.Length; ++i)
        {
            _transformList.Add((Transform)targets[i]);
        }
    }

    private void OnDisable()
    {
        if (null != _editorInstance)
        {
            var editorType = _editorInstance.GetType();
            var methodOnDisable = editorType.GetMethod("OnDisable", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            if (null != methodOnDisable)
                methodOnDisable.Invoke(_editorInstance, null);

            DestroyImmediate(_editorInstance);
            _editorInstance = null;

            _transformList.Clear();
        }
    }

    public override void OnInspectorGUI()
    {
        if (null != _editorInstance)
        {
            _editorInstance.OnInspectorGUI();
        }
        else
        {
            base.OnInspectorGUI();
        }

        //add custom editor gui
        EditorGUILayout.LabelField("自定义内容");
        GUILayout.Button("自定义按钮");
    }

}

#endif

【参考】

Extending (instead of replacing) built-in Inspectors - Unity Forum

相关