自定义ugui Text Inspector

【需要注意的】

1) Text的默认Insepctor对应的类是UnityEditor.UI.TextEditor, 但是这个类我们无法直接访问, 只能通过反射的方式访问到

2) 使用[CustomEditor(typeof(Text))]并没有办法替换默认的Inspector, 所以只能通过继承Text的方式来实现。

 

 

public class MyText : Text
{  
}

# Editor类

[CustomEditor(typeof(MyText))]
[CanEditMultipleObjects]
public class MyTextEditor : ProxyEditor
{
    
    protected override void GetEditorTypeName(out string typeFullName, out string dllName)
    {
        typeFullName = "UnityEditor.UI.TextEditor";
        dllName = "UnityEditor.UI";
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        EditorGUILayout.LabelField("=====这边是扩展内容");
    }
}

#endif

public abstract class ProxyEditor : UnityEditor.Editor
{
    private Type _editorType;
    private UnityEditor.Editor _editorInstance;

    protected abstract void GetEditorTypeName(out string typeFullName, out string dllName);
    
    private void OnEnable()
    {
        if (null != _editorInstance) 
            return;

        if (null == _editorType)
        {
            //var typeFullName = "UnityEditor.UI.TextEditor";
            //var dllName = "UnityEditor.UI";
            GetEditorTypeName(out var typeFullName, out var dllName);
            if (string.IsNullOrEmpty(dllName))
                _editorType = Type.GetType(typeFullName);
            else
                _editorType = Type.GetType($"{typeFullName},{dllName}");
        }
        if (null == _editorType) return;

        if (null != targets)
            _editorInstance = CreateEditor(targets, _editorType);
    }

    private void OnDisable()
    {
        if (null != _editorInstance)
        {
            DestroyImmediate(_editorInstance);
            _editorInstance = null;
        }
    }

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

 

posted @ 2022-04-02 23:16  yanghui01  阅读(122)  评论(0编辑  收藏  举报