Unity扩展让枚举视图中变成多选框

本文深入探讨了如何使用自定义属性和枚举类型进行数据绘制,包括属性描述特性、绘制类实现及应用实例,展示了在Unity环境下自定义属性绘制的实践方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如图:

image

定义属性描述特性(因为没有描述的数据,让绘制类去绘制所以为空)

using UnityEngine;
using System.Collections;


public class EnumFlagsAttribute : PropertyAttribute {}

自定义属性绘制类:

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomPropertyDrawer(typeof(EnumFlagsAttribute))]
public class EnumFlagsAttributeDrawer : PropertyDrawer {
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        /*
         * 绘制多值枚举选择框,0 全部不选, -1 全部选中, 其他是枚举之和
         * 枚举值 = 当前下标值 ^ 2
         * 默认[0^2 = 1 , 1 ^2 = 2,  4, 16 , .....]
         */
        property.intValue = EditorGUI.MaskField(position, label, property.intValue
                                                , property.enumNames);

        Debug.Log("图层的值:" + property.intValue);

    }
}

组件:

using UnityEngine;
using System.Collections;


public enum LayerMeskEnum 
{
    Layer1,
    Layer2,
    Layer3,
    Layer4,
}

public class MyCompoment : MonoBehaviour {

    [EnumFlagsAttribute]
    public LayerMeskEnum layer;
}

项目结构:

image

转载于:https://www.cnblogs.com/plateFace/p/4324948.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值