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

相关推荐
esmap20 分钟前
ESMAP 智慧消防解决方案:以数字孪生技术构建全域感知消防体系,赋能消防安全管理智能化升级
人工智能·物联网·3d·编辑器·智慧城市
大明者省2 小时前
激活函数选型速查表(核心规则)
编辑器
GitHubDaily2 小时前
Windows 欠了 30 年的那个原生编辑器,终于回来了。
编辑器
蓝丶曦月4 小时前
MacM系列芯片安装 最新版本Notepad--(平替Windows系统的Notepad++)详细教程
编辑器·notepad++·mac
我的offer在哪里4 小时前
示例 Unity 项目结构(Playable Game Template)
unity·游戏引擎
jun_bai5 小时前
VSCode使用
ide·vscode·编辑器
淡海水7 小时前
【节点】[Branch节点]原理解析与实际应用
unity·游戏引擎·shadergraph·图形·branch
在路上看风景7 小时前
4.6 显存和缓存
unity
CaracalTiger8 小时前
OpenClaw-VSCode:在 VS Code 中通过 WebSocket 远程管理 OpenClaw 网关的完整方案
运维·ide·人工智能·vscode·websocket·开源·编辑器
Zik----9 小时前
简单的Unity漫游场景搭建
unity·游戏引擎