第三章自定义检视面板_PropertyDrawer_内置的PropertyDrawer(7/9)

3.2 PropertyDrawer

​ PropertyDrawer​​ 是Unity编辑器扩展的核心工具,用于​​自定义序列化属性在Inspector中的显示方式​​。它通过重写属性的绘制逻辑,让属性显示可以自定义

3.2.1 内置的PropertyDrawer

Unity 内置了多种 PropertyDrawer,用于优化常用数据类型的 Inspector 显示效果。

Unity Inspector 属性 & PropertyDrawer 表

属性/类型对应 PropertyDrawer功能描述使用示例适用类型UI 效果
RangeAttributeRangeDrawer将数值显示为滑动条[Range(0, 100)] public float health;float, int带最小/最大值的滑动条
HeaderAttributeHeaderDrawer添加分组标题[Header("Player Settings")] public string name;任意灰色背景的分组标题
SpaceAttributeSpaceDrawer添加垂直间距[Space(20)] public float bonus;任意指定高度的空白区域
TooltipAttributeTooltipDrawer添加悬停提示[Tooltip("Health points")] public int hp;任意鼠标悬停时显示提示文本
ColorUsageAttributeColorUsageDrawer高级颜色选择器[ColorUsage(true)] public Color effectColor;Color支持 HDR / Alpha 的颜色选择器
EnumEnumDrawer枚举下拉菜单public PlayerClass playerClass;所有枚举下拉选择框
LayerAttributeLayerFieldDrawer层级选择器[Layer] public int targetLayer;intUnity 层级下拉菜单
TagAttributeTagFieldDrawer标签选择器[Tag] public string targetTag;stringUnity 标签下拉菜单
GradientGradientDrawer渐变编辑器public Gradient damageColor;Gradient渐变编辑面板
AnimationCurveCurveDrawer曲线编辑器public AnimationCurve moveCurve;AnimationCurve曲线编辑面板
MultilineAttributeMultilineDrawer多行文本输入[Multiline] public string description;string多行文本区域
TextAreaAttributeTextAreaDrawer可滚动文本区域[TextArea(3, 10)] public string bio;string带滚动条的多行文本框
DelayedAttributeDelayedDrawer延迟更新字段[Delayed] public float value;int, float, string输入后需确认或失去焦点才更新
MinAttributeMinDrawer最小值限制[Min(0)] public int score;int, float输入值低于最小值时自动修正
ContextMenuItemContextMenuItemDrawer右键菜单选项[ContextMenuItem("Reset", "ResetValue")] public int value;任意字段右键显示自定义菜单
SerializeReferenceSerializeReferenceDrawer多态序列化[SerializeReference] public IWeapon weapon;接口 / 基类显示具体类型选择下拉框
Vector FieldsVectorDrawer向量字段public Vector3 position;Vector2, Vector3, Vector4紧凑的多个浮点数字段
RectRectDrawer矩形编辑器public Rect area;RectX / Y / 宽 / 高 四个字段
BoundsBoundsDrawer边界编辑器public Bounds area;Bounds中心点 + 尺寸编辑字段
QuaternionQuaternionDrawer四元数编辑器public Quaternion rotation;Quaternion欧拉角显示 (X / Y / Z)
FixedBufferFixedBufferDrawer固定缓冲区public fixed float buffer[10];fixed buffer数组式字段显示

使用提示:所有特性均需在 using UnityEngine; 命名空间下使用,部分特性(如 SerializeReference)需配合 System.Serializable 接口或基类使用。

举个栗子,代码如下

using UnityEngine;

public class PropertyDrawerExample : MonoBehaviour
{
    //内置PropertyDrawer 示例
    // 带范围限制的浮点数字段
    [Range(4f, 10f)] // Inspector 显示滑块控制(4-10范围)
    [SerializeField] private float radius = 5f;

    // 带最小值限制的浮点数字段
    [Min(4f)] // 自动限制输入最小值(可手动输入大于4的值)
    [SerializeField] private float radius2 = 5f;

    // 多行文本输入(固定高度)
    [Multiline(5), SerializeField] // 显示5行高度的文本框(带滚动条)
    private string text = "Hello World.\r\nToday is a good day.";

    // 无限高度的文本区域
    [TextArea, SerializeField] // 可自由拖拽调整高度的文本区
    private string text2 = "Hello World.\r\nToday is a good day.\r\nThis is a TextArea with no line limit.";

    // 高级颜色选择器
    [ColorUsage(true, true), SerializeField] // 带HDR和Alpha控制的颜色选择器
    private Color color = Color.white;

    // 渐变编辑器(标准设置)
    [GradientUsage(false, ColorSpace.Gamma), SerializeField]
    private Gradient gradient; // 非HDR,Gamma色彩空间的渐变编辑器
}

效果图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值