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

时间:2024-07-21 23:07:14

如图:

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

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

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

项目结构:

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