Unity编辑器扩展——自定义Inspector面板

一:Editor类

Editor类和EditorWindow类都继承自同一个基类:ScriptableObject,他们都可以针对某种脚本类来进行操作
Editor类只能对指定脚本进行扩展,Inspector面板的显示,或变量在Scene视图中的可视化

使用到的类:
——GUILayout:绘制各种2D控件,比如按钮、文本、可折叠列表,以及对它们的分组和排版。这些控件既可以绘制在Editor类里,也可以绘制在EditorWindow里
——EditorGUILayout:与GUILayout类似,但是提供更多的预定义控件
——GUIUtility:绘制自定义的2D控件,比如进度条、对话框、打开文件
——EditorGUIUtility:绘制自定义的2D控件,比如进度条、对话框、打开文件

重写OnInspectorGUI方法进行绘制


二:案例

例如一个正常继承MonoBehavIoUr的脚本Test,定义了一个float类型的公有变量move_speed和一个float类型的公有变量rotation_speed,以及一个bool类型的isMove


如上图所示,Inspector面板中显示定义的三个参数,但是这样赋值时容易产生混淆,所以现在我们有这样一个需求,当isMove勾选上则面板上显示move_speed变量,当isMove未勾选上则面板上显示rotation_speed变量

新建一个脚本(一般命名为需要被扩展的脚本名+Editor,例如Test脚本的Inspector扩展类命名为TestEditor),脚本需要继承自Editor,我们知道自定义的Window窗口需要在OnGUI中绘制,而自定义的Inspector面板需要在OnInspectorGUI中绘制


using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
    public override void OnInspectorGUI()
    {
        Test t = (Test)target;

        t.isMove = EditorGUILayout.Toggle("是否为移动", t.isMove);
        if (t.isMove)
        {
            EditorGUILayout.FloatField("移动速度", t.move_speed);
        }
        else
        {
            EditorGUILayout.FloatField("旋转速度", t.rotation_speed);
        }
    }
}

三:EditorGUILayout.PropertyField

快速实现字段在编辑器中的显示与编辑,只能显示字段不能显示属性,private和protected需要添加序列化标识​​​​​​​​​​​​​​

​​​​​​​

using UnityEngine;
using System;
using UnityEditor;
using UnityEngine.Events;

public class TestNew : MonoBehavIoUr
{
    public enum ETestType
    {
        Type1,
        Type2,
        Type3,
    }

    public float m_Value1;
    [Serializefield]
    private int m_Value2;

    [Serializable]
    public class TestEvent1 : UnityEvent { }
    [Serializefield]
    private TestEvent1 m_Action1 = new TestEvent1();
    public TestEvent1 testAction1
    {
        get { return m_Action1; }
        set { m_Action1 = value; }
    }

    [Serializable]
    public class TestEvent2 : UnityEvent<int, string> { }
    [Serializefield]
    private TestEvent2 m_Action2 = new TestEvent2();
    public TestEvent2 testAction2
    {
        get { return m_Action2; }
        set { m_Action2 = value; }
    }

    [Serializefield]
    ETestType m_TestType;
}

[CustomEditor(typeof(TestNew))]
public class TestNewEditor : Editor
{
    SerializedProperty m_Value1;
    SerializedProperty m_Value2;
    SerializedProperty m_Action1;
    SerializedProperty m_Action2;
    SerializedProperty m_TestType;
    GUIContent content;

    private void OnEnable()
    {
        m_Value1 = serializedobject.FindProperty("m_Value1");
        m_Value2 = serializedobject.FindProperty("m_Value2");
        m_Action1 = serializedobject.FindProperty("m_Action1");
        m_Action2 = serializedobject.FindProperty("m_Action2");
        m_TestType = serializedobject.FindProperty("m_TestType");
        content = EditorGUIUtility.TrTextContent("测试按钮", "tooltip", "PlayButton");
    }

    public override void OnInspectorGUI()
    {
        serializedobject.Update();

        EditorGUILayout.PropertyField(m_Value1);
        EditorGUILayout.PropertyField(m_Value2);
        EditorGUILayout.PropertyField(m_Action1);
        EditorGUILayout.PropertyField(m_Action2);
        EditorGUILayout.PropertyField(m_TestType);
        if ((TestNew.ETestType)m_TestType.intValue == TestNew.ETestType.Type1)
        {

        }
        if (GUILayout.Button(content))
        {
            Debug.Log("click test button");
        }

        serializedobject.ApplyModifiedProperties();
    }
}

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...