Unity编辑器扩展 自定义脚本属性面板--基础篇

开发中,如果使用插件会发现插件的组件,在属性面板上的设计非常方便,看着很高大上,他们是怎么做到的呢

基础

  • 在Editor文件夹下,创建我们的属性面板编辑脚本
  • using UnityEditor的命名空间
  • 继承Editor

测试脚本

using UnityEngine;

public class ComponentInspector:MonoBehaviour {
       public bool PropertyBool;
       public TestEnum PropertyEnum;
       public int PropertyValue;
}
public enum TestEnum {

        Test1 = 0,
        Test2 = 1

}
   

Editor脚本

using UnityEngine;
using UnityEditor;

//ComponentInspector:目标脚本
[CustomEditor(typeof(ComponentInspector))]
public class ComponentInspectorEditor : Editor{
    
     private SerializedObject @object;
     
     private SerializedProperty m_PropertyBool;
     private SerializedProperty m_PropertyEnum;
     private SerializedProperty m_PropertyValue;

     private void OnEnable(){
            @object = new SerializedObject(target);
            //寻找对应的属性
            m_PropertyBool = @object.FindProperty("PropertyBool");
            m_PropertyEnum= @object.FindProperty("PropertyEnum");
            m_PropertyValue= @object.FindProperty("PropertyValue");
     }

     public override void OnInspectorGUI(){
            @object.Update();
            SerializedProperty property = @object.GetIterator();
            while (property.NextVisible(true))
            {
                using (new EditorGUI.DisabledScope("m_Script" == property.propertyPath))
                {
                    EditorGUILayout.PropertyField(property, true);
                    break;
                }
            }
            //开始设计属性面板
            EditorGUILayout.PropertyField(m_PropertyBool);
            if(m_PropertyBool .boolValue){
                    EditorGUILayout.PropertyField(m_PropertyEnum);
                    if(m_PropertyEnum.enumValueIndex == 0){
                           EditorGUILayout.PropertyField(m_PropertyValue);
                    }
            }
            
            @object.ApplyModifiedProperties();
     }
}

简单看一下效果吧

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贪小心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值