コード例 #1
0
        public static void ShowAsPopup(Rect buttonPosition, TreeItem[] rootItems, TreeItem selectedItem, string search = "", SearchChangedEventHandler onSearchChanged = null)
        {
            SearchablePopup searchable = new SearchablePopup();

            searchable.m_Items        = new List <TreeItem>(rootItems);
            searchable.m_SelectedItem = selectedItem;
            searchable.m_SearchString = search;
            if (onSearchChanged != null)
            {
                searchable.SearchChanged += onSearchChanged;
            }
            searchable.ApplyFilter();


            if (Event.current != null)
            {
                // If the event is not null, then we're inside OnGUI so can use the default Unity popup
                SearchablePopupContent content = new SearchablePopupContent(buttonPosition, searchable);
                searchable.SetEditorWindow(() => content.editorWindow);
                PopupWindow.Show(buttonPosition, content);
            }
            else
            {
                // Otherwise, the OnGUI version will throw an exception so use our own Editor window
                SearchablePopupEditorWindow.ShowAsPopup(buttonPosition, searchable);
            }
        }
コード例 #2
0
        public static void ShowAsPopup(Rect buttonPosition, GUIContent[] labels, int selectedIndex, SelectedIndexEventHandler onSelection, string search = "", SearchChangedEventHandler onSearchChanged = null)
        {
            List <TreeItem> roots                 = new List <TreeItem>();
            TreeItem        selectedItem          = null;
            Dictionary <string, TreeItem> itemMap = new Dictionary <string, TreeItem>();
            List <TreeItem> items                 = new List <TreeItem>();

            //Create all the actual leaf items
            for (int i = 0; i < labels.Length; i++)
            {
                GUIContent label = labels[i];
                string     text  = label.text;

                if (!string.IsNullOrEmpty(text))
                {
                    string[] bits = text.Split(new char[] { '/' });

                    GUIContent leafContent = new GUIContent(bits[bits.Length - 1], label.image, label.tooltip);

                    // We store the index into the labels array in userData
                    TreeItem item = new TreeItem(leafContent, text, (int)i, userSelectedItem => {
                        if (onSelection != null)
                        {
                            onSelection((int)userSelectedItem.UserData);
                        }
                    });

                    if (!itemMap.ContainsKey(text))
                    {
                        items.Add(item);
                        if (!itemMap.ContainsKey(text))
                        {
                            itemMap.Add(text, item);
                        }
                        if (i == selectedIndex)
                        {
                            selectedItem = item;
                        }
                    }
                    else
                    {
                        Debug.LogErrorFormat("Duplicate Key \"{0}\" in label \"{1}\"", text, label);
                    }
                }
            }

            // Create any intermediate parent items
            foreach (TreeItem item in items)
            {
                int index = (int)item.UserData;

                string originalText = labels[index].text;
                if (!string.IsNullOrEmpty(originalText))
                {
                    string[] bits   = originalText.Split(new char[] { '/' });
                    TreeItem prev   = null;
                    TreeItem parent = null;
                    for (int i = 1; i < bits.Length; i++)
                    {
                        prev = parent;
                        string key = string.Join("/", bits.Take(i).ToArray());
                        if (!itemMap.TryGetValue(key, out parent))
                        {
                            parent = new TreeItem(new GUIContent(bits[i - 1]), null);
                            if (prev != null)
                            {
                                prev.AddChild(parent);
                            }
                            else
                            {
                                roots.Add(parent);
                            }
                            itemMap.Add(key, parent);
                        }
                    }
                    // Add the actual item to the parent (if we even found a parent)
                    if (parent != null)
                    {
                        parent.AddChild(item);
                    }
                    else
                    {
                        roots.Add(item);
                    }
                }
            }

            ShowAsPopup(buttonPosition, roots.ToArray(), selectedItem, search, onSearchChanged);
        }