Esempio n. 1
0
        public override object Show(string name, object value, Type t, FieldInfo fieldInfo, object instance, bool withName = true)
        {
            string textField = "";

            if (withName)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label(name, GUILayout.Width(146));
            }
            var dateTime = value is DateTime ? (DateTime)value : new DateTime();

            if (GUILayout.Button(dateTime.ToString("yyyy-MM-dd HH:mm:ss")))
            {
                string str             = dateTime.ToString("yyyy-MM-dd HH:mm:ss");
                var    popCustomWindow = new PopCustomWindow();
                popCustomWindow.DrawGUI = () =>
                {
                    str = EditorGUILayout.TextField(str, GUILayout.Width(200));
                    try
                    {
                        value = Convert.ToDateTime(str);
                        fieldInfo.SetValue(instance, value);
                    }
                    catch (Exception e)
                    {
                        Debug.Log(e);
                    }
                };
                popCustomWindow.PopWindow();
            }

            if (withName)
            {
                GUILayout.EndHorizontal();
            }

//            if (withName)
//            {
//                textField = EditorGUILayout.TextField(name,value.ToString());
//            }
//            else
//            {
//                textField = EditorGUILayout.TextField(value.ToString());
//            }
//            try
//            {
//                value = Convert.ToDateTime(textField);
//            }
//            catch (Exception e)
//            {
//
//            }


            return(value);
        }
        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);
        }
        /// <summary>
        /// 筛选列表
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns>List&lt;T&gt;.</returns>
        private List <T> Filter(List <T> source)
        {
            if (GUILayout.Button("F", EditorStyles.toolbarButton, GUILayout.Width(20)))
            {
                PopCustomWindow customWindow = new PopCustomWindow();
                customWindow.DrawGUI = () =>
                {
                    GUILayout.BeginHorizontal("flow overlay box");
                    GUITool.Button("筛选", Color.clear);
                    GUILayout.EndHorizontal();

                    GUILayout.BeginVertical();
                    foreach (var searchFilter in filters)
                    {
                        var show = searchFilter.Show();
                        if (show)
                        {
                            this.SetDirty();
                        }
                    }
                    GUILayout.EndVertical();
                };
                customWindow.PopWindow();
            }

            if (research)
            {
                var list = new List <T>();

                //检索数据,必须满足所有的筛选器
                foreach (var data in source)
                {
                    bool enable = this.filters.All(filter => !filter.IsActive() || filter.RunFilter(data));
                    if (enable)
                    {
                        list.Add(data);
                    }
                }
                return(list);
            }
            return(source);
        }
        /// <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);
        }
        private void OpenSelectWindow(FieldInfo fieldInfo, object instance, string[] imgFolderPaths)
        {
            List <Texture2D> texture2Ds   = new List <Texture2D>();
            List <Texture2D> texture2Dss  = new List <Texture2D>();
            List <Texture2D> texture2Dsss = new List <Texture2D>();
            var findAssets = AssetDatabase.FindAssets("", imgFolderPaths);
            Dictionary <string, string>    loaded            = new Dictionary <string, string>();
            Dictionary <Texture2D, string> loadedTextureName = new Dictionary <Texture2D, string>();
            Dictionary <Texture2D, string> loadedTexturePath = new Dictionary <Texture2D, string>();

            foreach (var asset in findAssets)
            {
                var guidToAssetPath = AssetDatabase.GUIDToAssetPath(asset);
                if (!loaded.ContainsKey(guidToAssetPath))
                {
                    var loadAssetAtPath = AssetDatabase.LoadAssetAtPath <Texture2D>(guidToAssetPath);
                    var pathSplit       = guidToAssetPath.Split('/');
                    var imgName         = pathSplit[pathSplit.Length - 1];

                    loaded[guidToAssetPath] = imgName;
                    if (loadAssetAtPath != null)
                    {
                        loadedTextureName[loadAssetAtPath] = imgName;
                        loadedTexturePath[loadAssetAtPath] = guidToAssetPath;
                        texture2Ds.Add(loadAssetAtPath);
                    }
                }
            }

            var popCustomWindow = new PopCustomWindow();

            popCustomWindow.DefaultSize = new Vector2(600, 400);

            popCustomWindow.DrawGUI = () =>
            {
                texture2Dsss = searchBar2.Draw(texture2Ds, item => item.name);
                if (searchBar2.SearchContent.IsNOTNullOrEmpty())
                {
                    texture2Dss = texture2Dsss;
                }
                else
                {
                    texture2Dss = texture2Ds;
                }
                string selectPath = null;
                int    i          = 0;
                while (i < texture2Dss.Count)
                {
                    GUILayout.BeginHorizontal();
                    for (int lineCount = 0; lineCount < 5 && i < texture2Dss.Count; lineCount++)
                    {
                        bool select    = false;
                        var  texture2D = texture2Dss[i];
                        GUILayout.BeginVertical();
                        select = GUILayout.Button(new GUIContent(texture2D), GUILayout.Width(100), GUILayout.Height(100));
                        GUITool.Button(loadedTextureName[texture2D], Color.clear, GUILayout.MaxWidth(100));
                        GUILayout.EndVertical();
                        i++;

                        if (select)
                        {
                            selectPath = loadedTexturePath[texture2D];
                        }
                    }
                    GUILayout.EndHorizontal();
                }

                if (selectPath.IsNOTNullOrEmpty())
                {
                    var assetDatabasePathToFieldValue = this.AssetDatabasePathToFieldValue(selectPath);
//                        Debug.Log(assetDatabasePathToFieldValue);
                    fieldInfo.SetValue(instance, this.AssetDatabasePathToFieldValue(selectPath));
                    popCustomWindow.CloseWindow();
                }
            };
            popCustomWindow.PopWindow();
        }