示例#1
0
        private void OnGUI()
        {
            GUI.skin.label.richText     = true;
            GUI.skin.button.richText    = true;
            GUI.skin.box.richText       = true;
            GUI.skin.textArea.richText  = true;
            GUI.skin.textField.richText = true;
            GUI.skin.toggle.richText    = true;
            GUI.skin.window.richText    = true;

            if (AutoAdjustSize)
            {
                GUILayout.BeginVertical(GUITool.GetAreaGUIStyle(new Color(0.3f, 0.3f, 0.3f)), GUILayout.Width(10), GUILayout.Height(10));
                DrawOnGUI();
                GUILayout.EndVertical();
                Rect lastRect = GUILayoutUtility.GetLastRect();

                if (lastRect.size.x > 5 && lastRect.size.y > 5)
                {
                    this.maxSize = lastRect.size;
                    this.minSize = lastRect.size;
                }
            }
            else
            {
                this.ScrollViewPosition = GUILayout.BeginScrollView(this.ScrollViewPosition, GUITool.GetAreaGUIStyle(new Color(0.3f, 0.3f, 0.3f)));
                DrawOnGUI();
                GUILayout.EndScrollView();
            }
        }
        public static IList ListEditor(string prefix, IList list, Type listType)
        {
            if (list == null)
            {
                list = ReflecTool.Instantiate(listType) as IList;
            }
            var argType = listType.IsArray ? listType.GetElementType() : listType.GetGenericArguments()[0];

            //register foldout
            if (!registeredEditorFoldouts.ContainsKey(list))
            {
                registeredEditorFoldouts[list] = false;
            }

            GUILayout.BeginVertical();

            var foldout = registeredEditorFoldouts[list];

            foldout = EditorGUILayout.Foldout(foldout, prefix);
            registeredEditorFoldouts[list] = foldout;

            if (!foldout)
            {
                GUILayout.EndVertical();
                return(list);
            }

            if (list.Equals(null))
            {
                GUILayout.Label("Null List");
                GUILayout.EndVertical();
                return(list);
            }
            GUILayout.BeginHorizontal();
            GUILayout.Space(20);
            GUILayout.BeginVertical();
            if (GUILayout.Button("Add Element"))
            {
                if (listType.IsArray)
                {
                    list = ResizeArray((Array)list, list.Count + 1);
                    registeredEditorFoldouts[list] = true;
                }
                else
                {
                    object newElement = null;
                    if (argType.IsValueType)
                    {
                        newElement = Activator.CreateInstance(argType);
                    }
                    if (!argType.IsInterface && !argType.IsAbstract && AssemblyTool.FindTypesInCurrentDomainWhereExtend(argType).Count == 1)
                    {
                        try
                        {
                            newElement = Activator.CreateInstance(argType);
                        }
                        catch (Exception)
                        {
                        }
                    }
                    list.Add(newElement);
                }
                GUI.FocusControl("");
            }

            //            EditorGUI.indentLevel++;

            for (var i = 0; i < list.Count; i++)
            {
                GUILayout.BeginVertical(GUITool.GetAreaGUIStyle(new Color(Color.grey.r, Color.grey.g, Color.grey.b, 0.3f)));
                GUILayout.BeginHorizontal();
                GUILayout.BeginVertical();
                ShowObject(list[i]);
                GUILayout.EndVertical();
                //                list[i] = GenericField("Element " + i, list[i], argType, null, null, false);
                if (GUILayout.Button("X", GUILayout.Width(18)))
                {
                    if (listType.IsArray)
                    {
                        list = ResizeArray((Array)list, list.Count - 1);
                        registeredEditorFoldouts[list] = true;
                    }
                    else
                    {
                        list.RemoveAt(i);
                    }
                    GUI.FocusControl("");
                }
                GUILayout.EndHorizontal();
                GUITool.Line(2);
                GUILayout.EndVertical();
            }
            GUILayout.EndVertical();
            GUILayout.EndHorizontal();
            //            EditorGUI.indentLevel--;
            Separator();

            GUILayout.EndVertical();
            return(list);
        }
        /// <summary>
        /// The draw on gui.
        /// </summary>
        protected override void DrawOnGUI()
        {
            if (this.Items.Count == 0)
            {
                return;
            }
            var levelName = this.menuNameStack[this.menuNameStack.Count - 1];
            var levelPath = this.menuNameStack.Joint("/");

            List <PopMenuWindowItem> items = this.Items[levelPath];

            if (this.HasSearchBar)
            {
                if (this.searchBar == null)
                {
                    this.searchBar = new GUISearchBar <PopMenuWindowItem>();
                }

                var searchItems = this.searchBar.Draw(this.ItemList, item => item.GetItemName());
                if (this.searchBar.SearchContent.IsNOTNullOrEmpty())
                {
                    items = searchItems;
                    if (this.MenuName.IsNOTNullOrEmpty())
                    {
                        levelName = SearchLevelName;
                    }
                }
            }
            if (levelName.IsNOTNullOrEmpty())
            {
                GUILayout.BeginHorizontal(GUITool.GetAreaGUIStyle(new Color(0, 0, 0, 0.2f)));
                if (this.menuNameStack.Count >= 2 && levelName != SearchLevelName)
                {
                    if (GUITool.Button("◀", Color.clear, GUILayout.Width(30)) || GUITool.Button(levelName, Color.clear) || GUITool.Button(" ", Color.clear, GUILayout.Width(30)))
                    {
                        this.menuNameStack.RemoveAt(this.menuNameStack.Count - 1);
                    }
                }
                else
                {
                    GUITool.Button(levelName, Color.clear);
                }
                GUILayout.EndHorizontal();
            }

            if (!this.AutoAdjustSize)
            {
                this.scroll = GUILayout.BeginScrollView(this.scroll);
            }

            if (this.AutoSortItem)
            {
                items.Sort(
                    (l, r) =>
                {
                    if (l is PopMenuWindowItemJump)
                    {
                        return(1);
                    }
                    if (r is PopMenuWindowItemJump)
                    {
                        return(-1);
                    }
                    return(StringComparer.CurrentCulture.Compare(l.GetItemName(), r.GetItemName()));
                });
            }

            foreach (var popMenuWindowItem in items)
            {
                if (popMenuWindowItem.Show())
                {
                    popMenuWindowItem.Select();
                }
            }
            if (!this.AutoAdjustSize)
            {
                GUILayout.EndScrollView();
            }
            if (Event.current.type == EventType.KeyUp && !Event.current.alt && !Event.current.command && Event.current.shift && !Event.current.control)
            {
                if (this.searchBar == null || string.IsNullOrEmpty(this.searchBar.SearchContent))
                {
                    if (Event.current.keyCode == KeyCode.Backspace)
                    {
                    }
                    else if (Event.current.keyCode == KeyCode.Escape)
                    {
                        if (this.menuNameStack.Count >= 2 && levelName != SearchLevelName)
                        {
                            this.menuNameStack.RemoveAt(this.menuNameStack.Count - 1);
                            Event.current.Use();
                        }
                    }
                    else
                    {
                        foreach (var menuWindowItem in items)
                        {
                            if (menuWindowItem.ShortcutKey == Event.current.keyCode)
                            {
                                menuWindowItem.Select();
                                Event.current.Use();
                                break;
                            }
                        }
//                        var popMenuWindowItem = items[UnityEngine.Random.Range(0, items.Count - 1)];
//                        popMenuWindowItem.Select();
                    }
                    //                this.PageTo();
                }
            }
        }