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

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

效果图:

1.对CustomClass类编辑自定义面板

  1. 对MonoTest类编辑自定义面板

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

代码:

csharp 复制代码
[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();
    }
}

看来CustomEditor这个东西,对于嵌套的类没作用,这个时候就需要使用PropertyDrawer

相关推荐
五花肉村长11 小时前
数据结构-队列
c语言·开发语言·数据结构·算法·visualstudio·编辑器
嵌入式小能手12 小时前
开发环境搭建之VScode的安装及使用
vscode·编辑器
Artistation Game12 小时前
九、怪物行为逻辑
游戏·unity·游戏引擎
百里香酚兰12 小时前
【AI学习笔记】基于Unity+DeepSeek开发的一些BUG记录&解决方案
人工智能·学习·unity·大模型·deepseek
妙为12 小时前
unreal engine5制作动作类游戏时,我们使用刀剑等武器攻击怪物或敌方单位时,发现攻击特效、伤害等没有触发
游戏·游戏引擎·虚幻·碰撞预设
dangoxiba15 小时前
[Unity Demo]从零开始制作空洞骑士Hollow Knight第十三集:制作小骑士的接触地刺复活机制以及完善地图的可交互对象
游戏·unity·visualstudio·c#·游戏引擎
电子科技圈16 小时前
IAR全面支持国科环宇AS32X系列RISC-V车规MCU
人工智能·嵌入式硬件·mcu·编辑器
先生沉默先1 天前
使用Materialize制作unity的贴图,Materialize的简单教程,Materialize学习日志
学习·unity·贴图
安冬的码畜日常2 天前
【工欲善其事】巧用 Sublime Text 生成带格式的 HTML 片段
编辑器·html·typora·sublime text·代码片段·snippet
十画_8242 天前
Visual Studio 小技巧记录
unity·visual studio