效果
枚举
public enum MyFontStyleMask { Bold = 1, Italic = 1 << 1, Outline = 1 << 2, }
标签类
using UnityEngine; public class MyEnumMaskAttribute : PropertyAttribute { }
Property Drawer
#if UNITY_EDITOR using System; using UnityEditor; using UnityEngine; [CustomPropertyDrawer(typeof(MyEnumMaskAttribute))] public class MyEnumMaskPropertyDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty sp, GUIContent label) {if (!typeof(Enum).IsAssignableFrom(fieldInfo.FieldType)) { position = EditorGUI.PrefixLabel(position, label); EditorGUI.LabelField(position, $"not Enum Type: {fieldInfo.FieldType.Name}"); } else { sp.intValue = EditorGUI.MaskField(position, label, sp.intValue, sp.enumNames); } } } #endif
测试代码
public class MyEnumMaskTest : MonoBehaviour { [MyEnumMask] public MyFontStyleMask m_FontStyleFlags; [MyEnumMask] public int m_Value; void Start() { Debug.Log($"{m_FontStyleFlags}, {Convert.ToString((int)m_FontStyleFlags, 2).PadLeft(32, '0')}"); Debug.Log($"Bold: { 0 != (m_FontStyleFlags & MyFontStyleMask.Bold)}"); Debug.Log($"Italic: {0 != (m_FontStyleFlags & MyFontStyleMask.Italic)}"); Debug.Log($"Outline: {0 != (m_FontStyleFlags & MyFontStyleMask.Outline)}"); } }
注意,不是枚举,EditorGUI.MaskField也能实现位操作,比如像下面这样
var displayOpts = new string[] { "1", "1<<1", "1<<2" }; sp.intValue = EditorGUI.MaskField(position, label, sp.intValue, displayOpts);
参考
Unity 编辑器扩展十 多选枚举 - 简书 (jianshu.com)
标签:MaskField,MyFontStyleMask,EditorGUI,public,枚举,position,FontStyleFlags From: https://www.cnblogs.com/sailJs/p/18127799