首页 > 其他分享 >EditorGUI.EnumFlagsField实现多选枚举

EditorGUI.EnumFlagsField实现多选枚举

时间:2024-04-12 22:23:05浏览次数:21  
标签:Enum MyFontStyleMask EditorGUI public 枚举 EnumFlagsField position FontStyleFlags

效果

枚举

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)
    {
        position = EditorGUI.PrefixLabel(position, label);
        var fieldType = fieldInfo.FieldType;
        if (!typeof(Enum).IsAssignableFrom(fieldType))
        {
            EditorGUI.LabelField(position, $"not Enum Type: {fieldType.Name}");
        }
        else
        {
            var obj = Enum.ToObject(fieldType, sp.intValue); //整数值强转为枚举类型
            Enum e = EditorGUI.EnumFlagsField(position, (Enum)obj);
            sp.intValue = Convert.ToInt32(e); //枚举强转为整数
        }
    }

}

#endif

测试代码

public class MyEnumMaskTest : MonoBehaviour
{
    [MyEnumMask]
    public int m_Value;

    [MyEnumMask]
    public MyFontStyleMask m_FontStyleFlags;

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

}

 

参考

Unity中的位操作(从LayerMask说起) | indienova 独立游戏

 

标签:Enum,MyFontStyleMask,EditorGUI,public,枚举,EnumFlagsField,position,FontStyleFlags
From: https://www.cnblogs.com/sailJs/p/18132244

相关文章

  • EditorGUI.MaskField实现多选枚举
    效果枚举publicenumMyFontStyleMask{Bold=1,Italic=1<<1,Outline=1<<2,}标签类usingUnityEngine;publicclassMyEnumMaskAttribute:PropertyAttribute{}PropertyDrawer#ifUNITY_EDITORusingSystem;usingUnityEd......
  • C语言10-指针(多级指针、空指针、野指针),自定义数据类型(枚举enum、结构体struct)
    第12章指针pointer12.6多级指针指向指针的指针称为多级指针eg:int*ptr1=&num; int**ptr2=&ptr1; int***ptr3=&ptr2;12.7空指针应用场景:1.暂时不确定指向的指针,可以在定义的时候先赋值为NULL2.有些指针函数,如果内部出现异常无法成功实现功能,可以返回NUL......
  • C++算法题解 - 递归实现排列型枚举 - 递归法 (图文) (递归搜索树)
    题目:递归实现排列型枚举把1∼n这n个整数排成一行后随机打乱顺序,输出所有可能的次序。输入格式一个整数n。输出格式按照从小到大的顺序输出所有方案,每行1个。首先,同一行相邻两个数用一个空格隔开。其次,对于两个不同的行,对应下标的数一一比较,字典序较小的排在前面。数据......
  • 枚举Enumeration
    枚举它的对象(具体值)是固定的时(如:性别、季节等)不会更多,普通类不能体现它们是固定的若干个对象。枚举(enumeration,简写enum)是一组常量的集合;可以理解为特殊的类,里面仅包含一组有限的特定对象。自定义实现枚举构造器私有化,防止直接实例化对象去掉set方法,防止属性被修改;保证对象......
  • 泛型类型参数约束2 - 枚举作为约束类型
    先复习下枚举的相关基础知识:枚举类型(EnumType)说明枚举只有一种成员:命名的整型常量的集合枚举是值类型使用枚举有效地防止用户提供无效值,使代码更加清晰定义枚举:注意:​枚举成员不可以使用修饰符​每个枚举成员底层都是一个常量值​默认情况下,枚举成员的类型是int......
  • 数据结构复习-01enum枚举类型
    enum枚举类型语法:enum Nanme{name1=number1,name2=number2,};举例:enumDay{mon=1;tue=2;};enumDayday=mon;printf("dayis%d",day);输出:注意事项:1.若枚举类型中的首个元素未定义则默认为0 2.枚举类型的非首元素的默认值为......
  • 排列型枚举(全排列)
    0.简介在排列型枚举中,我们从给定的元素集合中选择出若干个元素的所有可能排列,这些排列考虑了元素的顺序.1.代码模板#include<bits/stdc++.h>usingnamespacestd;intn;intorder[20];boolchosen[20];//x代表当前选择位voidDFS(intx){ //选满了 if(x==n+1......
  • 【C语言】:枚举和联合体
    这里写自定义目录标题1、枚举1.1枚举类型的声明1.2枚举类型的优点1.3枚举类型的使用2、联合体(共用体)2.1联合体类型的声明2.2联合体的特点2.3联合体大小的计算1、枚举1.1枚举类型的声明枚举顾名思义就是⼀⼀列举,把可能的取值⼀⼀列举。⼀周的星期⼀到星期⽇......
  • 小红不想做莫比乌斯反演杜教筛求因子和的前缀和(枚举)--牛客周赛 Round 39-E
    #include<bits/stdc++.h>usingnamespacestd;#defineintlonglong#defineendl'\n'#defineinf1e18constintmod=1e9+7;constintN=2e5+5;intn,m,p,x;voidsolve(){ cin>>n>>m>>p>>x; intans=0; for(inti=1;i&......
  • 接口和枚举在方法中的区别
    枚举类型较传统定义常量的方式,除了具有参数类型检测的优势之外,还具有其他方面的优势。用户可以将一个枚举类型看作是一个类,它继承于java.lang.Enum类,当定义一个枚举类型时,每一个枚举类型成员都可以看作是枚举类型的一个实例,这些枚举类型成员都默认被final、public、static......