Exemple #1
0
        public static Enum EnumPopupEx(string label, Enum value, ref bool changed, GUIStyle style, params GUILayoutOption[] options)
        {
            //通过反射获取到枚举列表
            FieldInfo[] fields = value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public);
            string[]    popup  = new string[fields.Length];
            //获取对应Comment属性,生成菜单列表
            int selected = 0;

            for (int i = 0; i < fields.Length; i++)
            {
                CommentAttribute attr = fields[i].GetCustomAttributes(typeof(CommentAttribute), false).FirstOrDefault() as CommentAttribute;
                popup[i] = attr != null ? attr.Comment : fields[i].Name;
                if (fields[i].GetValue(null).Equals(value))
                {
                    selected = i;
                }
            }
            //弹出菜单
            selected = Popup(label, selected, popup, ref changed, options);
            if (fields.Length > 0)
            {
                value = (Enum)fields[selected].GetValue(null);
            }
            return(value);
        }
Exemple #2
0
        public static int EnumMaskToggle(string label, Enum value, ref bool changed, params GUILayoutOption[] options)
        {
            int maskValue    = (int)(object)value;
            int newMaskValue = 0;

            EditorGUILayout.LabelField(label);
            EditorGUI.indentLevel++;
            FieldInfo[] fields = value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public);
            for (int i = 0; i < fields.Length; i++)
            {
                CommentAttribute attr = fields[i].GetCustomAttributes(typeof(CommentAttribute), false).FirstOrDefault() as CommentAttribute;
                string           text = attr != null ? attr.Comment : fields[i].Name;
                int  itemValue        = (int)fields[i].GetValue(null);
                bool toggle           = (itemValue & maskValue) != 0;
                toggle = Toggle(text, toggle, ref changed);
                if (toggle)
                {
                    newMaskValue |= itemValue;
                }
            }
            EditorGUI.indentLevel--;
            return(newMaskValue);
        }
Exemple #3
0
        ///unity的那个有个bug,如果自己包含值为0的这个选项,菜单里就会出现两个none。第二个none的值会映射第一个选项
        public static Enum EnumMaskFieldEx(string label, Enum value, ref bool changed, GUIStyle style,
                                           params GUILayoutOption[] layoutOptions)
        {
            int maskValue = (int)(object)value;
            int newMaskValue;
            List <EnumMaskItem> itemList = new List <EnumMaskItem>();

            FieldInfo[] fields = value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public);
            //获取列表
            for (int i = 0; i < fields.Length; i++)
            {
                CommentAttribute attr = fields[i].GetCustomAttributes(typeof(CommentAttribute), false).FirstOrDefault() as CommentAttribute;
                string           text = attr != null ? attr.Comment : fields[i].Name;
                int itemValue         = (int)fields[i].GetValue(null);
                //跳过0和-1
                if (itemValue == 0 || itemValue == -1)
                {
                    continue;
                }
                itemList.Add(new EnumMaskItem()
                {
                    Value      = itemValue,
                    Text       = text,
                    IsSelected = (itemValue & maskValue) != 0,
                });
            }
            //生成列表的选项
            string[] options = new string[itemList.Count];
            for (int i = 0; i < itemList.Count; i++)
            {
                options[i] = itemList[i].Text;
            }
            //生成列表选项的索引和选项项
            int itemSelectMask = 0;

            if (maskValue == 0 || maskValue == -1)
            {
                itemSelectMask = maskValue;
            }
            else
            {
                for (int i = 0; i < itemList.Count; i++)
                {
                    if (itemList[i].IsSelected)
                    {
                        itemSelectMask |= 1 << i;//unity的返回的1是第一个,0是全不选
                    }
                }
            }
            //显示选项
            var selects = EditorGUILayout.MaskField(label, itemSelectMask, options, style, layoutOptions);

            if (selects != itemSelectMask)
            {
                changed = true;
                //从选项中获取枚举值mask
                if (selects == 0 || selects == -1)
                {
                    newMaskValue = selects;
                }
                else
                {
                    newMaskValue = 0;
                    for (int i = 0; i < itemList.Count; i++)
                    {
                        if ((selects & (1 << i)) != 0)
                        {
                            newMaskValue |= itemList[i].Value;
                        }
                    }
                }
            }
            else
            {
                newMaskValue = maskValue;
            }
            return((Enum)Enum.ToObject(value.GetType(), newMaskValue));
        }