//...
        void OnGUI()
        {
            GUI.skin.label.richText = true;
            EditorGUILayout.HelpBox("Here you can specify frequently used types for your project and for easier access wherever you need to select a type, like for example when you create a new blackboard variable or using any refelection based actions. Furthermore, it is essential when working with AOT platforms like iOS or WebGL, that you generate an AOT Classes and link.xml files with the relevant button bellow. To add types in the list quicker, you can also Drag&Drop an object, or a Script file in this editor window.\n\nIf you save a preset in your 'Editor Default Resources/" + TypePrefs.SYNC_FILE_NAME + "' it will automatically sync with the list. Useful when working with others on source control.", MessageType.Info);

            if (GUILayout.Button("Add New Type", EditorStyles.miniButton))
            {
                GenericMenu.MenuFunction2 Selected = delegate(object o)
                {
                    if (o is System.Type)
                    {
                        AddType((System.Type)o);
                    }
                    if (o is string)     //namespace
                    {
                        foreach (var type in alltypes)
                        {
                            if (type.Namespace == (string)o)
                            {
                                AddType(type);
                            }
                        }
                    }
                };

                var menu       = new GenericMenu();
                var namespaces = new List <string>();
                menu.AddItem(new GUIContent("Classes/System/Object"), false, Selected, typeof(object));
                foreach (var t in alltypes)
                {
                    var a            = (string.IsNullOrEmpty(t.Namespace) ? "No Namespace/" : t.Namespace.Replace(".", "/") + "/") + t.FriendlyName();
                    var b            = string.IsNullOrEmpty(t.Namespace)? string.Empty : " (" + t.Namespace + ")";
                    var friendlyName = a + b;
                    var category     = "Classes/";
                    if (t.IsValueType)
                    {
                        category = "Structs/";
                    }
                    if (t.IsInterface)
                    {
                        category = "Interfaces/";
                    }
                    if (t.IsEnum)
                    {
                        category = "Enumerations/";
                    }
                    menu.AddItem(new GUIContent(category + friendlyName), typeList.Contains(t), Selected, t);
                    if (t.Namespace != null && !namespaces.Contains(t.Namespace))
                    {
                        namespaces.Add(t.Namespace);
                    }
                }

                menu.AddSeparator("/");
                foreach (var ns in namespaces)
                {
                    var path = "Whole Namespaces/" + ns.Replace(".", "/") + "/Add " + ns;
                    menu.AddItem(new GUIContent(path), false, Selected, ns);
                }

                menu.ShowAsBrowser("Add Preferred Type");
            }


            if (GUILayout.Button("Generate AOTClasses.cs and link.xml Files", EditorStyles.miniButton))
            {
                if (EditorUtility.DisplayDialog("Generate AOT Classes", "A script relevant to AOT compatibility for certain platforms will now be generated.", "OK"))
                {
                    var path = EditorUtility.SaveFilePanelInProject("AOT Classes File", "AOTClasses", "cs", "");
                    if (!string.IsNullOrEmpty(path))
                    {
                        AOTClassesGenerator.GenerateAOTClasses(path, TypePrefs.GetPreferedTypesList(true).ToArray());
                    }
                }

                if (EditorUtility.DisplayDialog("Generate link.xml File", "A file relevant to 'code stripping' for platforms that have code stripping enabled will now be generated.", "OK"))
                {
                    var path = EditorUtility.SaveFilePanelInProject("AOT link.xml", "link", "xml", "");
                    if (!string.IsNullOrEmpty(path))
                    {
                        AOTClassesGenerator.GenerateLinkXML(path, TypePrefs.GetPreferedTypesList().ToArray());
                    }
                }

                AssetDatabase.Refresh();
            }

            GUILayout.BeginHorizontal();

            if (GUILayout.Button("Reset Defaults", EditorStyles.miniButtonLeft))
            {
                if (EditorUtility.DisplayDialog("Reset Preferred Types", "Are you sure?", "Yes", "NO!"))
                {
                    TypePrefs.ResetTypeConfiguration();
                    typeList = TypePrefs.GetPreferedTypesList();
                    Save();
                }
            }

            if (GUILayout.Button("Save Preset", EditorStyles.miniButtonMid))
            {
                var path = EditorUtility.SaveFilePanelInProject("Save Types Preset", "PreferredTypes", "typePrefs", "");
                if (!string.IsNullOrEmpty(path))
                {
                    System.IO.File.WriteAllText(path, JSONSerializer.Serialize(typeof(List <System.Type>), typeList, null, true));
                    AssetDatabase.Refresh();
                }
            }

            if (GUILayout.Button("Load Preset", EditorStyles.miniButtonRight))
            {
                var path = EditorUtility.OpenFilePanel("Load Types Preset", "Assets", "typePrefs");
                if (!string.IsNullOrEmpty(path))
                {
                    var json = System.IO.File.ReadAllText(path);
                    typeList = JSONSerializer.Deserialize <List <System.Type> >(json);
                    Save();
                }
            }

            GUILayout.EndHorizontal();
            GUILayout.Space(5);
            var syncPath = TypePrefs.SyncFilePath();

            EditorGUILayout.HelpBox(syncPath != null ? "List synced with file: " + syncPath.Replace(Application.dataPath, ".../Assets") : "No sync file found in '.../Assets/Editor Default Resources'. Types are currently saved in Unity EditorPrefs only.", MessageType.None);
            GUILayout.Space(5);

            scrollPos = GUILayout.BeginScrollView(scrollPos);
            for (int i = 0; i < typeList.Count; i++)
            {
                if (EditorGUIUtility.isProSkin)
                {
                    GUI.color = Color.black.WithAlpha(i % 2 == 0 ? 0.3f : 0);
                }
                if (!EditorGUIUtility.isProSkin)
                {
                    GUI.color = Color.white.WithAlpha(i % 2 == 0 ? 0.3f : 0);
                }
                GUILayout.BeginHorizontal("box");
                GUI.color = Color.white;
                var type = typeList[i];
                if (type == null)
                {
                    GUILayout.Label("MISSING TYPE", GUILayout.Width(300));
                    GUILayout.Label("---");
                }
                else
                {
                    var name = type.FriendlyName();
                    var icon = TypePrefs.GetTypeIcon(type);
                    GUILayout.Label(icon, GUILayout.Width(16), GUILayout.Height(16));
                    GUILayout.Label(name, GUILayout.Width(300));
                    GUILayout.Label(type.Namespace);
                }
                if (GUILayout.Button("X", GUILayout.Width(18)))
                {
                    RemoveType(type);
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.EndScrollView();

            AcceptDrops();
            Repaint();
        }
Exemple #2
0
        //THE TREE
        void DoTree(Rect treeRect, Event e)
        {
            if (treeGenerationThread != null)
            {
                return;
            }

            if (search != lastSearch)
            {
                lastSearch    = search;
                hoveringIndex = -1;
                if (!string.IsNullOrEmpty(search))
                {
                    //provide null reference thread (thus no aborting) so that results update all the time
                    searchGenerationThread = Threader.StartFunction(null, GenerateSearchResults, (resultNode) =>
                    {
                        currentNode            = resultNode;
                        searchGenerationThread = null;
                        if (current != null)
                        {
                            willRepaint = true;
                        }
                    });
                }
                else
                {
                    currentNode = rootNode;
                }
            }


            GUILayout.BeginArea(treeRect);
            scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false);
            GUILayout.BeginVertical();

            ///----------------------------------------------------------------------------------------------

            var    i                  = 0;
            var    itemAdded          = false;
            string lastSearchCategory = null;
            var    isSearch           = !string.IsNullOrEmpty(search);

            foreach (var childPair in currentNode.children)
            {
                if (isSearch && i >= 200)
                {
                    EditorGUILayout.HelpBox("There are more than 200 results. Please try refine your search input.", MessageType.Info);
                    break;
                }

                var node       = childPair.Value;
                var memberInfo = node.isLeaf ? node.item.userData as MemberInfo : null;
                var isDisabled = node.isLeaf && node.item.func == null && node.item.func2 == null;
                var icon       = node.isLeaf ? node.item.content.image : Icons.folderIcon;
                if (icon == null && memberInfo != null)
                {
                    icon = TypePrefs.GetTypeIcon(memberInfo);
                }

                //when within search, show category on top
                if (isSearch)
                {
                    var searchCategory = lastSearchCategory;
                    if (memberInfo == null || memberInfo is System.Type)
                    {
                        searchCategory = node.parent.fullPath != null?node.parent.fullPath.Split(new char[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries).FirstOrDefault() : null;
                    }
                    else
                    {
                        searchCategory = memberInfo.ReflectedType.FriendlyName();
                    }

                    if (searchCategory != lastSearchCategory)
                    {
                        lastSearchCategory = searchCategory;
                        GUI.color          = EditorGUIUtility.isProSkin ? Color.black : Color.white;
                        GUILayout.BeginHorizontal("box");
                        GUI.color = Color.white;
                        GUILayout.Label(searchCategory, Styles.leftLabel, GUILayout.Height(16));
                        GUILayout.EndHorizontal();
                    }
                }
                //


                if (filterFavorites && !node.isFavorite && !node.HasAnyFavoriteChild())
                {
                    continue;
                }

                if (node.isLeaf && node.item.separator)
                {
                    if (itemAdded)
                    {
                        EditorUtils.Separator();
                    }
                    continue;
                }

                itemAdded = true;

                GUI.color = Color.clear;
                GUILayout.BeginHorizontal("box");
                GUI.color = Color.white;

                //Prefix icon
                GUILayout.Label(icon, GUILayout.Width(22), GUILayout.Height(16));
                GUI.enabled = !isDisabled;

                //Favorite
                if (currentKeyType != null)
                {
                    GUI.color = node.isFavorite ? Color.white : (node.HasAnyFavoriteChild() ? new Color(1, 1, 1, 0.2f) : new Color(0f, 0f, 0f, 0.4f));
                    if (GUILayout.Button(Icons.favoriteIcon, GUIStyle.none, GUILayout.Width(16), GUILayout.Height(16)))
                    {
                        node.ToggleFavorite();
                    }
                    GUI.color = Color.white;
                }

                //Content
                var label    = node.name;
                var hexColor = EditorGUIUtility.isProSkin ? "#B8B8B8" : "#262626";
                hexColor = isDisabled ? "#666666" : hexColor;
                var text = string.Format("<color={0}><size=11>{1}</size></color>", hexColor, (!node.isLeaf ? string.Format("<b>{0}</b>", label) : label));
                GUILayout.Label(text, Styles.leftLabel, GUILayout.Width(0), GUILayout.ExpandWidth(true));
                GUILayout.Label(node.isLeaf ? "●" : "►", Styles.leftLabel, GUILayout.Width(20));
                GUILayout.EndHorizontal();

                var elementRect = GUILayoutUtility.GetLastRect();
                if (e.type == EventType.MouseDown && e.button == 0 && elementRect.Contains(e.mousePosition))
                {
                    e.Use();
                    if (node.isLeaf)
                    {
                        ExecuteItemFunc(node.item);
                        break;
                    }
                    else
                    {
                        currentNode   = node;
                        hoveringIndex = 0;
                        break;
                    }
                }

                if (e.type == EventType.MouseMove && elementRect.Contains(e.mousePosition))
                {
                    hoveringIndex = i;
                }

                if (hoveringIndex == i)
                {
                    GUI.color = hoverColor;
                    GUI.DrawTexture(elementRect, EditorGUIUtility.whiteTexture);
                    GUI.color = Color.white;
                }

                i++;
                GUI.enabled = true;
            }

            if (hoveringIndex != lastHoveringIndex)
            {
                willRepaint       = true;
                lastHoveringIndex = hoveringIndex;
            }

            if (!itemAdded)
            {
                GUILayout.Label("No results to display with current search and filter combination");
            }

            GUILayout.EndVertical();
            EditorGUILayout.EndScrollView();
            GUILayout.EndArea();
        }
Exemple #3
0
        //...
        void OnGUI()
        {
            if (EditorGUIUtility.isProSkin)
            {
                GUI.Box(new Rect(0, 0, Screen.width, Screen.height), string.Empty, Styles.shadowedBackground);
            }

            GUI.skin.label.richText = true;
            EditorGUILayout.HelpBox("Here you can specify frequently used types for your project and for easier access wherever you need to select a type, like for example when you create a new blackboard variable or using any refelection based actions.\nFurthermore, it is essential when working with AOT platforms like iOS or WebGL, that you generate an AOT Classes and link.xml files with the relevant button bellow.\nTo add types in the list quicker, you can also Drag&Drop an object, or a Script file in this editor window.", MessageType.Info);

            if (GUILayout.Button("Add New Type"))
            {
                GenericMenu.MenuFunction2 Selected = delegate(object o){
                    if (o is System.Type)
                    {
                        AddType((System.Type)o);
                    }
                    if (o is string)                      //namespace
                    {
                        foreach (var type in alltypes)
                        {
                            if (type.Namespace == (string)o)
                            {
                                AddType(type);
                            }
                        }
                    }
                };

                var menu       = new GenericMenu();
                var namespaces = new List <string>();
                menu.AddItem(new GUIContent("Classes/System/Object"), false, Selected, typeof(object));
                foreach (var t in alltypes)
                {
                    var friendlyName = (string.IsNullOrEmpty(t.Namespace)? "No Namespace/" : t.Namespace.Replace(".", "/") + "/") + t.FriendlyName();
                    var category     = "Classes/";
                    if (t.IsValueType)
                    {
                        category = "Structs/";
                    }
                    if (t.IsInterface)
                    {
                        category = "Interfaces/";
                    }
                    if (t.IsEnum)
                    {
                        category = "Enumerations/";
                    }
                    menu.AddItem(new GUIContent(category + friendlyName), typeList.Contains(t), Selected, t);
                    if (t.Namespace != null && !namespaces.Contains(t.Namespace))
                    {
                        namespaces.Add(t.Namespace);
                    }
                }

                menu.AddSeparator("/");
                foreach (var ns in namespaces)
                {
                    var path = "Whole Namespaces/" + ns.Replace(".", "/") + "/Add " + ns;
                    menu.AddItem(new GUIContent(path), false, Selected, ns);
                }

                menu.ShowAsBrowser("Add Preferred Type");
            }


            if (GUILayout.Button("Generate AOTClasses.cs and link.xml Files"))
            {
                if (EditorUtility.DisplayDialog("Generate AOT Classes", "A script relevant to AOT compatibility for certain platforms will now be generated.", "OK"))
                {
                    var path = EditorUtility.SaveFilePanelInProject("AOT Classes File", "AOTClasses", "cs", "");
                    if (!string.IsNullOrEmpty(path))
                    {
                        AOTClassesGenerator.GenerateAOTClasses(path, TypePrefs.GetPreferedTypesList(true).ToArray());
                    }
                }

                if (EditorUtility.DisplayDialog("Generate link.xml File", "A file relevant to 'code stripping' for platforms that have code stripping enabled will now be generated.", "OK"))
                {
                    var path = EditorUtility.SaveFilePanelInProject("AOT link.xml", "link", "xml", "");
                    if (!string.IsNullOrEmpty(path))
                    {
                        AOTClassesGenerator.GenerateLinkXML(path, TypePrefs.GetPreferedTypesList().ToArray());
                    }
                }

                AssetDatabase.Refresh();
            }

            GUILayout.BeginHorizontal();

            if (GUILayout.Button("RESET DEFAULTS"))
            {
                if (EditorUtility.DisplayDialog("Reset Preferred Types", "Are you sure?", "Yes", "NO!"))
                {
                    TypePrefs.ResetTypeConfiguration();
                    typeList = TypePrefs.GetPreferedTypesList();
                    Save();
                }
            }

            if (GUILayout.Button("Save Preset"))
            {
                var path = EditorUtility.SaveFilePanelInProject("Save Types Preset", "", "typePrefs", "");
                if (!string.IsNullOrEmpty(path))
                {
                    System.IO.File.WriteAllText(path, JSONSerializer.Serialize(typeof(List <System.Type>), typeList, true));
                    AssetDatabase.Refresh();
                }
            }

            if (GUILayout.Button("Load Preset"))
            {
                var path = EditorUtility.OpenFilePanel("Load Types Preset", "Assets", "typePrefs");
                if (!string.IsNullOrEmpty(path))
                {
                    var json = System.IO.File.ReadAllText(path);
                    typeList = JSONSerializer.Deserialize <List <System.Type> >(json);
                    Save();
                }
            }

            GUILayout.EndHorizontal();
            GUILayout.Space(5);

            scrollPos = GUILayout.BeginScrollView(scrollPos);
            for (int i = 0; i < typeList.Count; i++)
            {
                GUILayout.BeginHorizontal("box");
                var type = typeList[i];
                if (type == null)
                {
                    GUILayout.Label("MISSING TYPE", GUILayout.Width(300));
                    GUILayout.Label("---");
                }
                else
                {
                    var name = type.FriendlyName();
                    var icon = TypePrefs.GetTypeIcon(type);
                    GUILayout.Label(icon, GUILayout.Width(16), GUILayout.Height(16));
                    GUILayout.Label(name, GUILayout.Width(300));
                    GUILayout.Label(type.Namespace);
                }
                if (GUILayout.Button("X", GUILayout.Width(18)))
                {
                    RemoveType(type);
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.EndScrollView();

            AcceptDrops();
            Repaint();
        }
        ///----------------------------------------------------------------------------------------------

        ///Returns a type icon
        public static Texture GetTypeIcon(System.Type type)
        {
            return(TypePrefs.GetTypeIcon(type));
        }
        //THE TREE
        void DoTree(Rect treeRect, Event e)
        {
            GUILayout.BeginArea(treeRect);

            scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false);
            GUILayout.BeginVertical();

            if (search != lastSearch)
            {
                hoveringIndex = 0;
                if (!string.IsNullOrEmpty(search))
                {
                    var results = new List <Node>();
                    foreach (var node in leafNodes)
                    {
                        if (filterFavorites && !node.isFavorite)
                        {
                            continue;
                        }
                        if (StringUtils.SearchMatch(search, node.name, node.category))
                        {
                            results.Add(node);
                        }
                    }
                    var searchRootNode = new Node()
                    {
                        name = "Search Root"
                    };
                    //Remark: scoring is done on category name instead of leaf name
                    searchRootNode.children = results
                                              .OrderBy(r => StringUtils.ScoreSearchMatch(search, r.category, r.category) * (r.isFavorite ? 0.5f : 1))
                                              .ToDictionary(r => r.fullPath, r => r);
                    currentNode = searchRootNode;
                }
                else
                {
                    currentNode = rootNode;
                }
                lastSearch = search;
            }

            ///----------------------------------------------------------------------------------------------

            var    i                  = 0;
            var    itemAdded          = false;
            string lastSearchCategory = null;

            foreach (var childPair in currentNode.children)
            {
                var isSearch = !string.IsNullOrEmpty(search);

                if (isSearch && i >= 200)
                {
                    EditorGUILayout.HelpBox("There are more than 200 results. Please try refine your search input.", MessageType.Info);
                    break;
                }

                var node       = childPair.Value;
                var leafItem   = node.item;
                var memberInfo = leafItem != null ? leafItem.userData as MemberInfo : null;
                var isDisabled = leafItem != null && leafItem.func == null && leafItem.func2 == null;
                var icon       = leafItem != null ? leafItem.content.image : Icons.folderIcon;
                if (icon == null && memberInfo != null)
                {
                    icon = TypePrefs.GetTypeIcon(memberInfo);
                }

                //when within search, show category on top
                if (isSearch)
                {
                    var searchCategory = lastSearchCategory;
                    if (memberInfo == null || memberInfo is System.Type)
                    {
                        searchCategory = node.parent.fullPath != null?node.parent.fullPath.Split(new char[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries).FirstOrDefault() : null;
                    }
                    else
                    {
                        searchCategory = memberInfo.ReflectedType.FriendlyName();
                    }

                    if (searchCategory != lastSearchCategory)
                    {
                        lastSearchCategory = searchCategory;
                        GUI.color          = EditorGUIUtility.isProSkin ? Color.black : Color.white;
                        GUILayout.BeginHorizontal("box");
                        GUI.color = Color.white;
                        GUILayout.Label(searchCategory, Styles.leftLabel, GUILayout.Height(16));
                        GUILayout.EndHorizontal();
                    }
                }
                //


                if (filterFavorites && !node.isFavorite && !node.HasAnyFavoriteChild())
                {
                    continue;
                }

                if (leafItem != null && leafItem.separator)
                {
                    if (itemAdded)
                    {
                        EditorUtils.Separator();
                    }
                    continue;
                }

                itemAdded = true;

                GUI.color = EditorGUIUtility.isProSkin ? Color.white : new Color(0.8f, 0.8f, 0.8f, 1);
                GUILayout.BeginHorizontal("box");

                //Prefix icon
                GUI.color = Color.white;
                GUILayout.Label(icon, GUILayout.Width(32), GUILayout.Height(16));
                GUI.enabled = !isDisabled;

                //Favorite
                if (currentKeyType != null)
                {
                    GUI.color = node.isFavorite ? Color.white : (node.HasAnyFavoriteChild() ? new Color(1, 1, 1, 0.2f) : new Color(0f, 0f, 0f, 0.4f));
                    if (GUILayout.Button(Icons.favoriteIcon, GUIStyle.none, GUILayout.Width(16), GUILayout.Height(16)))
                    {
                        node.ToggleFavorite();
                    }
                    GUI.color = Color.white;
                }

                //Content
                var label = node.name;
                var text  = string.Format("<size=9>{0}</size>", (leafItem == null ? string.Format("<b>{0}</b>", label) : label));
                GUILayout.Box(text, (GUIStyle)"label", GUILayout.Width(0), GUILayout.ExpandWidth(true));
                GUILayout.Label(leafItem != null ? "●" : "►", GUILayout.Width(20));
                GUILayout.EndHorizontal();

                var elementRect = GUILayoutUtility.GetLastRect();
                if (e.type == EventType.MouseDown && e.button == 0 && elementRect.Contains(e.mousePosition))
                {
                    e.Use();
                    if (leafItem != null)
                    {
                        ExecuteItemFunc(leafItem);
                        break;
                    }
                    else
                    {
                        currentNode   = node;
                        hoveringIndex = 0;
                        break;
                    }
                }

                if (e.type == EventType.MouseMove && elementRect.Contains(e.mousePosition))
                {
                    hoveringIndex = i;
                }

                if (hoveringIndex == i)
                {
                    GUI.color = hoverColor;
                    GUI.DrawTexture(elementRect, EditorGUIUtility.whiteTexture);
                    GUI.color = Color.white;
                }

                i++;
                GUI.enabled = true;
            }

            if (hoveringIndex != lastHoveringIndex)
            {
                base.editorWindow.Repaint();
                lastHoveringIndex = hoveringIndex;
            }

            if (!itemAdded)
            {
                GUILayout.Label("No results to display with current search and filter combination");
            }

            GUILayout.EndVertical();
            EditorGUILayout.EndScrollView();
            GUILayout.EndArea();
        }