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


    首先找到的是这个[CustomEditor(typeof(Class), true)],这个东西能够自己绘制在Inspector视图的显示规则,但是!如果这个类被另一个类持有,他就没作用了,

    效果图:
    1.对CustomClass类编辑自定义面板
    对CustomClass类自定义面板
    2. 对MonoTest类编辑自定义面板对MonoTest类自定义面板
    结果。使用 [CustomEditor(typeof(MonoTest), true)]单独对MonoTest类型,进行自定义显示,符合自定义代码的显示布局。使用[CustomEditor(typeof(CustomClass ), true)]对持有类进行自定义显示,没有任何改变,unity默认显示方式。

    代码:

    [Serializable]
    public class MonoTest: MonoBehaviour
    {
        public enum EnumValue
        {
            EnumValue1,
            EnumValue2,
            EnumValue3,
        }
    
        public int intValue;
        public bool boolValue;
        public EnumValue enumValue;
    }
    
    public class CustomClass : MonoBehaviour
    {
        public List<MonoTest> Datas = new List<MonoTest>();
    }
    
    //自定义面板代码
    [CustomEditor(typeof(MonoTest), true)]
    //[CustomEditor(typeof(CustomClass ), true)]
    public class MonoTestEditor : Editor
    {
        private SerializedProperty m_IntValue;
        private SerializedProperty m_BoolValue;
        private SerializedProperty m_EnumValue;
        private void OnEnable()
        {
            m_IntValue = serializedObject.FindProperty("intValue");
            m_BoolValue = serializedObject.FindProperty("boolValue");
            m_EnumValue = serializedObject.FindProperty("enumValue");
        }
    
        public override void OnInspectorGUI()
        {
            //base.OnInspectorGUI();
            //serializedObject.Update();
            EditorGUILayout.BeginHorizontal();
            EditorGUIUtility.labelWidth = 100;
            EditorGUILayout.PropertyField(m_IntValue);
            EditorGUILayout.PropertyField(m_BoolValue);
            EditorGUILayout.PropertyField(m_EnumValue);
            EditorGUILayout.EndHorizontal();
            //serializedObject.ApplyModifiedProperties();
        }
    }
    
    • 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

    看来CustomEditor这个东西,对于嵌套的类没作用,这个时候就需要使用PropertyDrawer ,可以看下一篇文章CustomPropertyDrawer使用

  • 相关阅读:
    计算机网络——层次结构
    vue路由
    el-table双击编辑功能实现
    python基础语法 - 常用模块
    动态规划之线性dp(上)
    Java · 方法的使用 · 方法重载 · 方法递归
    Leetcode101.对称二叉树
    java计算机毕业设计基于springboo+vue的毕业生信息招聘求职平台管理系统
    C++基础篇之引用和其他细碎语法
    JavaEE——No.2 套接字编程(TCP)
  • 原文地址:https://blog.csdn.net/Ling_SevoL_Y/article/details/134061612