public static object FlagsEnumEditor(object enumValue, FieldInfo field, object instance)
        {
            Type type  = enumValue.GetType();
            int  value = (int)enumValue;

            string[]      strings = Enum.GetNames(type);
            StringBuilder builder = new StringBuilder();

            foreach (var s in strings)
            {
                object o         = Enum.Parse(type, s);
                int    currValue = (int)o;
                bool   selected  = (currValue & value) > 0;
                if (selected)
                {
                    builder.Append(s);
                    builder.Append("|");
                }
            }
            if (GUILayout.Button(builder.ToString()))
            {
                PopCustomWindow popCustomWindow = PopCustomWindow.ShowPopWindow();
                popCustomWindow.DrawGUI = () =>
                {
                    foreach (var s in strings)
                    {
                        object o         = Enum.Parse(type, s);
                        int    currValue = (int)o;
                        bool   selected  = (currValue & value) > 0;
                        GUILayout.BeginHorizontal();
                        GUILayout.Label(s);
                        bool endSelect = EditorGUILayout.Toggle(selected);
                        GUILayout.EndHorizontal();
                        if (selected != endSelect)
                        {
                            if (endSelect)
                            {
                                value += currValue;
                            }
                            else
                            {
                                value -= currValue;
                            }
                        }
                    }
                    field.SetValue(instance, value);
                };
            }
            return(value);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 排序列表
        /// </summary>
        /// <param name="list">The list.</param>
        /// <returns>List&lt;T&gt;.</returns>
        private List <T> Sort(List <T> list)
        {
            FieldInfo sortFieldInfo = null;

            if (this.sortField.IsNOTNullOrEmpty())
            {
                sortFieldInfo = this.dataType.GetField(this.sortField);
            }

            bool click = false;

            if (sortFieldInfo != null)
            {
                click = GUILayout.Button("Sort:" + this.dataType.GetField(this.sortField).GetBestName(), EditorStyles.toolbarButton);
            }
            else
            {
                click = GUILayout.Button("S", EditorStyles.toolbarButton, GUILayout.Width(20));
            }
            if (click)
            {
                var customWindow = PopCustomWindow.ShowPopWindow();
                customWindow.DrawGUI = () =>
                {
                    GUILayout.BeginVertical();
                    GUILayout.BeginHorizontal("flow overlay box");
                    GUITool.Button("排序", Color.clear);
                    GUILayout.EndHorizontal();
                    var fieldInfos = this.dataType.GetFields(BindingFlags.Instance | BindingFlags.Public);
                    if (GUILayout.Button("默认"))
                    {
                        this.sortField = string.Empty;
                        this.SaveState();
                        customWindow.CloseWindow();
                        this.SetDirty();
                    }
                    foreach (var fieldInfo in fieldInfos)
                    {
                        if (fieldInfo.FieldType == typeof(int) || fieldInfo.FieldType == typeof(string) ||
                            fieldInfo.FieldType == typeof(uint) || fieldInfo.FieldType == typeof(float))
                        {
                            if (GUILayout.Button(fieldInfo.Name))
                            {
                                this.sortField = fieldInfo.Name;
                                this.SaveState();
                                customWindow.CloseWindow();
                                this.SetDirty();
                            }
                        }
                    }
                    GUILayout.EndVertical();
                };
            }
            if (research)
            {
                if (this.sortField.IsNOTNullOrEmpty())
                {
                    list.Sort(
                        (l, r) =>
                    {
                        var fieldInfo = this.dataType.GetField(this.sortField, BindingFlags.Instance | BindingFlags.Public);
                        var lvalue    = fieldInfo.GetValue(l);
                        var rvalue    = fieldInfo.GetValue(r);
                        if (fieldInfo.FieldType == typeof(string))
                        {
                            return(StringComparer.CurrentCulture.Compare(lvalue, rvalue));
                        }
                        if (fieldInfo.FieldType == typeof(uint))
                        {
                            if ((uint)(lvalue) > (uint)(rvalue))
                            {
                                return(1);
                            }
                            if ((uint)(lvalue) < (uint)(rvalue))
                            {
                                return(-1);
                            }
                            return(0);
                        }
                        if (fieldInfo.FieldType == typeof(int))
                        {
                            if ((int)(lvalue) > (int)(rvalue))
                            {
                                return(1);
                            }
                            if ((int)(lvalue) < (int)(rvalue))
                            {
                                return(-1);
                            }
                            return(0);
                        }
                        if (fieldInfo.FieldType == typeof(float))
                        {
                            if ((float)(lvalue) > (float)(rvalue))
                            {
                                return(1);
                            }
                            if ((float)(lvalue) < (float)(rvalue))
                            {
                                return(-1);
                            }
                            return(0);
                        }
                        return(0);
                    });
                }
            }

            return(list);
        }