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

相关推荐
mxwin3 小时前
Unity Shader 半透明物体为什么不能写入深度缓冲?
unity·游戏引擎·shader
晚枫歌F4 小时前
三层时间轮的实现
网络·unity·游戏引擎
望眼欲穿的程序猿5 小时前
苹果系统使用VsCode开发QT
ide·vscode·编辑器
咸鱼永不翻身5 小时前
Lua脚本事件检查工具
unity·lua·工具
其实防守也摸鱼7 小时前
带你了解与配置phpmyadmin
笔记·安全·网络安全·pdf·编辑器·工具·调试
leo__5207 小时前
单载波中继系统资源分配算法MATLAB仿真程序
算法·matlab·unity
Rsun045517 小时前
Oracle中常用语法
编辑器
努力长头发的程序猿8 小时前
Unity使用ScriptableObject序列化资源
unity·游戏引擎
mxwin9 小时前
Unity Shader 手写基于 PBR 的 URP Lit Shader 核心光照计算
unity·游戏引擎·shader
小贺儿开发9 小时前
Unity3D 智能云端数字标牌系统
unity·阿里云·人机交互·视频·oss·广告·互动