void Unfocus()
    {
        // Add to search history
        SceneSearchHistory.AddToHistory(this, new SceneSearchHistory.SearchHistory(objectLookup, filter, searchAllScenes, searchResults.Count));

        // Remove focus if cleared
        objectLookup = "";
        GUI.FocusControl(null);
    }
Esempio n. 2
0
    void Unfocus()
    {
        // Add to search history
        SceneSearchHistory.AddToHistory(this, new SceneSearchHistory.SearchHistory(sceneLookup, searchByPath, searchCount));

        // Remove focus if cleared
        sceneLookup = "";
        GUI.FocusControl(null);
    }
    void OnGUI()
    {
        if (window == null)
        {
            window = GetWindow(typeof(SceneFinder)); PreOpenScenes();
        }

        GUILayout.BeginHorizontal(GUI.skin.FindStyle("Toolbar"));
        GUI.SetNextControlName("SearchField");
        objectLookup = GUILayout.TextField(objectLookup, GUI.skin.FindStyle("ToolbarSeachTextField"), GUILayout.Width(window.position.width - (string.IsNullOrEmpty(objectLookup) ? 21f : 37f)));

        //search clear button
        if (!string.IsNullOrEmpty(objectLookup))
        {
            if (GUILayout.Button("", GUI.skin.FindStyle("ToolbarSeachCancelButton")))
            {
                Unfocus();
            }
        }

        GUIStyle style = new GUIStyle();

        if (GUILayout.Button("", EditorStyles.toolbarDropDown, GUILayout.Width(15f)))
        {
            // create custom dropdown menu on button click
            GenericMenu menu = new GenericMenu();
            menu.AddItem(new GUIContent("Objects And Components"), filter == SceneSearchFilter.ObjectsAndComponents, OnSearchFilterChanged, SceneSearchFilter.ObjectsAndComponents);
            menu.AddItem(new GUIContent("Objects Only"), filter == SceneSearchFilter.ObjectsOnly, OnSearchFilterChanged, SceneSearchFilter.ObjectsOnly);
            menu.AddItem(new GUIContent("Components Only"), filter == SceneSearchFilter.ComponentsOnly, OnSearchFilterChanged, SceneSearchFilter.ComponentsOnly);
            menu.AddItem(new GUIContent("By Tag Name"), filter == SceneSearchFilter.ByTag, OnSearchFilterChanged, SceneSearchFilter.ByTag);
            menu.AddItem(new GUIContent("By Layer Name"), filter == SceneSearchFilter.ByLayer, OnSearchFilterChanged, SceneSearchFilter.ByLayer);
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Search All Scenes"), searchAllScenes, OnSearchFilterChanged, !searchAllScenes);
            menu.AddItem(new GUIContent("Open Search History"), false, OnSearchHistoryOpened, null);
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Unload All Opened Scenes", "Unloads any scenes opened by this tool"), false, UnloadAllScenes, null);

            menu.ShowAsContext();
        }
        GUILayout.EndHorizontal();
        if (searchResults.Count > 0)
        {
            GUILayout.Label(searchResults.Count + ((searchResults.Count == 1) ? " Result Found" : " Results Found"), EditorStyles.boldLabel);
        }
        GUILayout.Label("Active Scene: " + GetSceneName(EditorSceneManager.GetActiveScene().name), EditorStyles.boldLabel);
        scrollPos = GUILayout.BeginScrollView(scrollPos);

        //generate search results (find objects/components)
        if (!string.IsNullOrEmpty(objectLookup))
        {
            if (searchResults.Count > 0)
            {
                searchResults.Clear();
                searchResults = new List <GameObject>();
            }

            List <GameObject> sceneObjs = new List <GameObject>();

            if (searchAllScenes)
            {
                var      scenesGUIDs = AssetDatabase.FindAssets("t:Scene");
                string[] scenesPaths = scenesGUIDs.Select(AssetDatabase.GUIDToAssetPath).ToArray();
                for (int i = 0; i < scenesPaths.Length; i++)
                {
                    sceneObjs.AddRange(EditorSceneManager.GetSceneByPath(scenesPaths[i]).GetRootGameObjects());
                }
            }
            else
            {
                sceneObjs.AddRange(EditorSceneManager.GetActiveScene().GetRootGameObjects());
            }

            for (int i = 0; i < sceneObjs.Count; i++)
            {
                //BY OBJECT OR COMPONENT
                if (filter == SceneSearchFilter.ObjectsAndComponents)
                {
                    if (sceneObjs[i].name.ToLower().Contains(objectLookup.ToLower()) && !searchResults.Contains(sceneObjs[i]))
                    {
                        searchResults.Add(sceneObjs[i]);
                    }

                    Component[] objComponents = sceneObjs[i].GetComponents <Component>();
                    foreach (Component component in objComponents)
                    {
                        if (component.GetType().Name.ToLower().Contains(objectLookup.ToLower()) && !searchResults.Contains(sceneObjs[i]))
                        {
                            searchResults.Add(sceneObjs[i]); break;
                        }
                    }

                    //RECCURSIVE SEARCH (CHILD OBJECTS)
                    SearchChildObjects(sceneObjs[i].transform);
                }

                //BY OBJECT NAME
                else if (filter == SceneSearchFilter.ObjectsOnly)
                {
                    if (sceneObjs[i].name.ToLower().Contains(objectLookup.ToLower()))
                    {
                        searchResults.Add(sceneObjs[i]);
                    }
                }

                //BY OBJECT COMPONENT
                else if (filter == SceneSearchFilter.ComponentsOnly)
                {
                    Component[] objComponents = sceneObjs[i].GetComponents <Component>();
                    foreach (Component component in objComponents)
                    {
                        if (component.GetType().Name.ToLower().Contains(objectLookup.ToLower()))
                        {
                            searchResults.Add(sceneObjs[i]); break;
                        }
                    }
                }

                //BY OBJECT TAG
                else if (filter == SceneSearchFilter.ByTag)
                {
                    if (sceneObjs[i].tag.ToLower().Contains(objectLookup.ToLower()))
                    {
                        searchResults.Add(sceneObjs[i]);
                    }
                }

                //BY OBJECT LAYER
                else if (filter == SceneSearchFilter.ByLayer)
                {
                    if (LayerMask.LayerToName(sceneObjs[i].layer).ToLower().Contains(objectLookup.ToLower()))
                    {
                        searchResults.Add(sceneObjs[i]);
                    }
                }

                //RECCURSIVE SEARCH (CHILD OBJECTS)
                SearchChildObjects(sceneObjs[i].transform);
            }

            //DISPLAY SEARCH RESULTS (FILTER-BASED)
            for (int i = 0; i < searchResults.Count; i++)
            {
                //GUILayout.Label(searchResults[i].name);
                if (fold.Count - 1 < i)
                {
                    fold.Add(false);
                }
                fold[i] = EditorGUILayout.InspectorTitlebar(fold[i], searchResults[i]);

                if (fold[i])
                {
                    GameObject obj            = searchResults[i];
                    int        componentIndex = 1;

                    GUILayout.BeginHorizontal();
                    GUILayout.Label("Tag", GUILayout.Width(30f));
                    obj.tag = EditorGUILayout.TagField("", obj.tag, GUILayout.Width(window.position.width / 2.5f));
                    GUILayout.Label("Layer", GUILayout.Width(40f));
                    obj.layer = EditorGUILayout.LayerField("", obj.layer, GUILayout.Width(window.position.width / 2.75f));
                    GUILayout.EndHorizontal();
                    EditorGUILayout.Space();

                    string scenePath = obj.scene.path;
                    GUILayout.Label("Scene: " + GetSceneName(obj.scene.name));
                    if (!string.IsNullOrEmpty(scenePath))
                    {
                        GUILayout.Label("(" + obj.scene.path + ")");
                    }
                    if (obj.scene != EditorSceneManager.GetActiveScene())
                    {
                        EditorGUILayout.HelpBox("This object is in another scene", MessageType.Info);
                    }

                    EditorGUILayout.Space();
                    GUILayout.Label("Components:");

                    //list all components on the object, BOLD the ones that match search
                    foreach (Behaviour component in obj.GetComponents <Behaviour>())
                    {
                        string    componentName = component.GetType().Name;
                        Texture2D componentIcon = (Texture2D)EditorGUIUtility.ObjectContent(null, component.GetType()).image;
                        if (componentIcon == null)
                        {
                            componentIcon = GetDefaultScriptIcon();
                        }
                        style           = EditorStyles.toggle;
                        style.fontStyle = componentName.ToLower().Contains(objectLookup.ToLower()) ? FontStyle.Bold : FontStyle.Normal;
                        GUILayout.BeginHorizontal();
                        GUILayout.Label(componentIcon, GUILayout.Width(20f), GUILayout.Height(20f));
                        component.enabled = GUILayout.Toggle(component.enabled, componentName, style);
                        GUILayout.EndHorizontal();

                        GUILayout.Label("Component index: " + componentIndex++);
                        List <string> hierarchy = new List <string>();
                        GetObjectHierarchy(obj.transform, ref hierarchy);
                        GUILayout.Label("Hierarchy Order:");
                        for (int e = 0; e < hierarchy.Count; e++)
                        {
                            GUILayout.Label(AddSpace(e * 2) + "↳ " + hierarchy[e]);
                        }
                        GUILayout.Label("Child Index: " + obj.transform.GetSiblingIndex());
                        EditorGUILayout.Space();
                    }

                    if (GUILayout.Button("Find Object"))
                    {
                        EditorGUIUtility.PingObject(obj);
                    }
                    if (obj.scene != EditorSceneManager.GetActiveScene())
                    {
                        if (GUILayout.Button("Load Scene (" + obj.scene.name + ")"))
                        {
                            fold[i] = false; SwitchScenes(obj);
                        }
                    }

                    //if (obj.scene != EditorSceneManager.GetActiveScene()) { if (GUILayout.Button("Load Scene (" + obj.scene.name + ")")) { sceneObjName = obj.name; EditorSceneManager.activeSceneChangedInEditMode += EditorSceneManager_activeSceneChanged; fold[i] = false; window.autoRepaintOnSceneChange = true; UnloadAllScenes(null); SceneSwitcher.SwitchScenes(obj.scene.path); } }
                }
            }
            EditorGUILayout.Space();
        }

        GUILayout.EndScrollView();

        // Add to search history
        if (!string.IsNullOrEmpty(objectLookup) && GUI.GetNameOfFocusedControl() != "SearchField" && lastSearchInstance.ToLower() != objectLookup.ToLower())
        {
            SceneSearchHistory.AddToHistory(this, new SceneSearchHistory.SearchHistory(objectLookup, filter, searchAllScenes, searchResults.Count));
            lastSearchInstance = objectLookup;
            GUI.SetNextControlName("SearchField");
            GUI.FocusControl(null);
        }
    }
 void OnSearchHistoryOpened(object obj)
 {
     SceneSearchHistory.Init(this);
 }
Esempio n. 5
0
    void OnGUI()
    {
        if (window == null)
        {
            window = GetWindow(typeof(SceneSwitcher));
        }

        GUILayout.Label("Search for Scene");
        GUILayout.BeginHorizontal(GUI.skin.FindStyle("Toolbar"));
        GUI.SetNextControlName("SearchField");
        sceneLookup = GUILayout.TextField(sceneLookup, GUI.skin.FindStyle("ToolbarSeachTextField"), GUILayout.Width(window.position.width - (string.IsNullOrEmpty(sceneLookup) ? 21f : 37f)));

        //search clear button
        if (!string.IsNullOrEmpty(sceneLookup))
        {
            if (GUILayout.Button("", GUI.skin.FindStyle("ToolbarSeachCancelButton")))
            {
                Unfocus();
            }
        }

        if (GUILayout.Button("", EditorStyles.toolbarDropDown, GUILayout.Width(15f)))
        {
            // create custom dropdown menu on button click
            GenericMenu menu = new GenericMenu();
            menu.AddItem(new GUIContent("Search By Path"), searchByPath == true, OnSearchFilterChanged, !searchByPath);
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Open Search History"), false, OnSearchHistoryOpened, null);

            menu.ShowAsContext();
        }
        GUILayout.EndHorizontal();

        EditorGUILayout.Space();
        GUILayout.Label("Active Scene: " + EditorSceneManager.GetActiveScene().name, EditorStyles.boldLabel);
        EditorGUILayout.Space();

        scrollPos = GUILayout.BeginScrollView(scrollPos);
        if (string.IsNullOrEmpty(sceneLookup)) //list all scenes
        {
            GUIStyle style = EditorStyles.foldout;
            style.fontSize = 15;

            #region Scenes in Build
            List <string> disabledScenesInBuild = new List <string>();
            buildFold = EditorGUILayout.Foldout(buildFold, "Scenes In Build", true);

            if (buildFold)
            {
                //List all ACTIVE scenes in Build Settings
                foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
                {
                    if (!scene.enabled)
                    {
                        disabledScenesInBuild.Add(scene.path);
                    }
                    else
                    {
                        string scenePath = scene.path;
                        string sceneName = scenePath.Substring(scenePath.LastIndexOf("/") + 1).Replace(".unity", string.Empty);

                        EditorGUILayout.Space();
                        if (GUILayout.Button(sceneName))
                        {
                            SwitchScenes(scenePath); Unfocus();
                        }
                        GUILayout.TextField("(" + scenePath + ")", EditorStyles.textField);
                    }
                }

                //List all DISABLED scenes in Build Settings
                if (disabledScenesInBuild.Count > 0)
                {
                    GUILayout.Label("[DISABLED SCENES IN BUILD]");

                    for (int i = 0; i < disabledScenesInBuild.Count; i++)
                    {
                        string scenePath = disabledScenesInBuild[i];
                        string sceneName = scenePath.Substring(scenePath.LastIndexOf("/") + 1).Replace(".unity", string.Empty);

                        EditorGUILayout.Space();
                        if (GUILayout.Button(sceneName))
                        {
                            EditorSceneManager.OpenScene(scenePath); Unfocus();
                        }
                        GUILayout.TextField("(" + scenePath + ")");
                    }
                }
            }
            #endregion

            EditorGUILayout.Space();
            EditorGUILayout.Space();

            #region Scenes in Project
            string[] allScenePaths = AssetDatabase.FindAssets("t:scene");
            projectFold = EditorGUILayout.Foldout(projectFold, "Scenes In Project", true);

            if (projectFold)
            {
                for (int i = 0; i < allScenePaths.Length; i++)
                {
                    string scenePath = AssetDatabase.GUIDToAssetPath(allScenePaths[i]);
                    string sceneName = scenePath.Substring(scenePath.LastIndexOf("/") + 1).Replace(".unity", string.Empty);

                    EditorGUILayout.Space();
                    if (GUILayout.Button(sceneName))
                    {
                        SwitchScenes(scenePath);
                    }
                    GUILayout.TextField("(" + scenePath + ")");
                }
            }
            #endregion
        }
        else //list scenes matching search results
        {
            string[] allScenes = AssetDatabase.FindAssets("t:scene");
            searchCount = 0;
            for (int i = 0; i < allScenes.Length; i++)
            {
                string scenePath = AssetDatabase.GUIDToAssetPath(allScenes[i]);
                string sceneName = scenePath.Substring(scenePath.LastIndexOf("/") + 1).Replace(".unity", string.Empty);
                if (sceneName.ToLower().Contains(sceneLookup.ToLower()) || (searchByPath && scenePath.ToLower().Contains(sceneLookup.ToLower())))
                {
                    EditorGUILayout.Space();
                    if (GUILayout.Button(sceneName))
                    {
                        SwitchScenes(scenePath); Unfocus();
                    }
                    GUILayout.Label("(" + scenePath + ")");
                    searchCount++;
                }
            }
        }
        GUILayout.EndScrollView();

        // Add to search history
        if (!string.IsNullOrEmpty(sceneLookup) && GUI.GetNameOfFocusedControl() != "SearchField" && lastSearchInstance.ToLower() != sceneLookup.ToLower())
        {
            SceneSearchHistory.AddToHistory(this, new SceneSearchHistory.SearchHistory(sceneLookup, searchByPath, searchCount));
            lastSearchInstance = sceneLookup;
            GUI.SetNextControlName("SearchField");
            GUI.FocusControl("SearchField");
        }
    }