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();
    }
}

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值