Esempio n. 1
0
        private static void OnHierarchyWindowChanged()
        {
            if (HierarchyUtils.IsHierarchyWindowInPrefabMode())
            {
                return;
            }

            if (IsSceneChanged())
            {
                // Create our marker
                _cachedGo   = new GameObject(GameObjectName, typeof(HierarchyNode));
                _cachedNode = _cachedGo.GetComponent <HierarchyNode>();

                // Note: We don't use the DontSaveInEditor flag as this causes OnDestroy/OnDisable() to not be called when the scene unloads
                _cachedGo.hideFlags = HideFlags.NotEditable | HideFlags.HideInHierarchy | HideFlags.HideInInspector;
#if UNITY_5 || UNITY_5_4_OR_NEWER
                _cachedGo.hideFlags |= HideFlags.DontSaveInBuild;
#endif
            }

            if (_cachedNode != null)
            {
                _cachedNode.SetHierarchyDirty();
            }
        }
Esempio n. 2
0
        private void RestoreSelection()
        {
            if (HierarchyUtils.IsHierarchyWindowInPrefabMode())
            {
                return;
            }

            UpdateSceneGUID();
            if (!string.IsNullOrEmpty(_sceneGUID))
            {
                _hierarchyPathList = RestoreHierarchyState(_sceneGUID, true);
                _selectionPathList = RestoreSelectionState(_sceneGUID, true);
                //Debug.Log("hierarchy " + _hierarchyPathList);
                //Debug.Log("selection " + _selectionPathList);
                if (string.IsNullOrEmpty(_selectionPathList))
                {
                    // No selection was restored, so we need to select at least one node so that the hierarchy expansion happens.
                    // So just select one of the items in the hierarchyList
                    string[] paths = null;
                    {
                        // Convert the string list to an array
                        if (!string.IsNullOrEmpty(_hierarchyPathList))
                        {
                            paths = _hierarchyPathList.Split(new char[] { '*' }, System.StringSplitOptions.RemoveEmptyEntries);
                        }

                        // Select the first found expanded object
                        if (paths != null && paths.Length > 0)
                        {
                            foreach (string path in paths)
                            {
                                GameObject go = GameObject.Find(path);
                                if (go != null)
                                {
                                    Selection.activeGameObject = go;
                                    break;
                                }
                            }
                        }
                    }
                }

                // Only force the hierarchy window to update if something has changed
                if (Selection.activeGameObject != null || !string.IsNullOrEmpty(_hierarchyPathList))
                {
                    // Force the hierarchy window to update to the current selection
                    EditorApplication.DirtyHierarchyWindowSorting();

                    // NOTE: We need to use the delayCall because sometimes DirtyHierarchyWindowSorting() doesn't work the first time
                    EditorApplication.delayCall += () =>
                    {
                        // Force the hierarchy window to update to the current selection
                        EditorApplication.DirtyHierarchyWindowSorting();
                    };
                }
            }
        }
        public static string RestoreSelectionState(string sceneId, bool logMissingNodes)
        {
            string result = EditorPrefs.GetString(GetEditorPref("Selection", sceneId), string.Empty);

            if (!HierarchyUtils.SetSelectionFromString(result, logMissingNodes))
            {
                result = string.Empty;
            }
            return(result);
        }
Esempio n. 4
0
        // When the scene closes, this node will get destroyed, at which point we save the scene state information
        private void OnDestroy()
        {
            if (HierarchyUtils.IsHierarchyWindowInPrefabMode())
            {
                return;
            }

            SaveHierarchyState(_hierarchyPathList, _sceneGUID);
            SaveSelectionState(_selectionPathList, _sceneGUID);
        }
        private void CacheHierarchy()
        {
            UpdateSceneGUID();

            if (Selection.activeGameObject != null)
            {
                _selectionPathList = HierarchyUtils.GetSelectionAsString();
            }

            _hierarchyPathList = HierarchyUtils.GetHierarchyAsString();
        }
        public static string RestoreHierarchyState(string sceneId, bool logMissingNodes)
        {
            // If it returns notset then this scene hasn't had any hierarchy state saved before
            // If it returns null/empty, then all hierarchy is collapsed
            string notset = "notset";
            string result = EditorPrefs.GetString(GetEditorPref("Hierarchy", sceneId), notset);

            if (result != notset)
            {
                HierarchyUtils.SetHierarchyFromString(result, logMissingNodes);
            }
            else
            {
                result = string.Empty;
            }
            return(result);
        }
Esempio n. 7
0
 private void OnEditorUpdate()
 {
     // Editor updates are expensive, so don't do them too often as it can slow down the UI
     if (editorUpdateCount > 50 &&
         // We don't want to save hierarchy/selection when the app is playing
         !EditorApplication.isPlaying &&
         !EditorApplication.isPlayingOrWillChangePlaymode &&
         !EditorApplication.isCompiling &&
         !EditorApplication.isUpdating &&
         !HierarchyUtils.IsHierarchyWindowInPrefabMode())
     {
         editorUpdateCount = 0;
         if (HasSelectionChanged() || HasHierarchyChanged())
         {
             CacheHierarchy();
         }
     }
     editorUpdateCount++;
 }
        public static void SetHierarchyFromString(string list, bool logMissingNodes)
        {
            string[] paths = null;
            {
                // Convert the string list to an array
                if (!string.IsNullOrEmpty(list))
                {
                    paths = list.Split(new char[] { '*' }, System.StringSplitOptions.RemoveEmptyEntries);
                }
            }

            // Convert the paths array into an array of objects
            GameObject[] gos = null;
            if (paths != null && paths.Length > 0)
            {
                List <GameObject> goList = new List <GameObject>();
                foreach (string path in paths)
                {
                    // TODO: GameObject.Find doesn't find disabled objects, so we need to handle this
                    GameObject go = GameObject.Find(path);
                    if (go != null)
                    {
                        goList.Add(go);
                    }
                    else if (logMissingNodes)
                    {
                        Debug.LogWarning("[HierarchyRestore] Can't find node with path: '" + path + "'");
                    }
                }
                gos = goList.ToArray();
            }

            if (gos != null && gos.Length > 0)
            {
                HierarchyUtils.SetHierarchyWindowExpandedObjects(gos);
            }
            else
            {
                HierarchyUtils.SetHierarchyWindowExpandedObjects(new GameObject[0]);
            }
        }
Esempio n. 9
0
        public static void SetHierarchyFromString(string list, bool logMissingNodes)
        {
            string[] paths = null;
            {
                // Convert the string list to an array
                if (!string.IsNullOrEmpty(list))
                {
                    paths = list.Split(new char[] { '*' }, System.StringSplitOptions.RemoveEmptyEntries);
                }
            }

            // Convert the paths array into an array of objects
            GameObject[] gos          = null;
            int[]        sceneHandles = null;
            if (paths != null && paths.Length > 0)
            {
                List <GameObject> goList    = new List <GameObject>();
                List <int>        sceneList = null;
                foreach (string path in paths)
                {
                    bool isFound = false;
                    if (path.StartsWith("/"))
                    {
                        // TODO: GameObject.Find doesn't find disabled objects, so we need to handle this
                        GameObject go = GameObject.Find(path);
                        if (go != null)
                        {
                            goList.Add(go);
                            isFound = true;
                        }
                    }
#if RH_UNITY_FEATURE_SCENE_HANDLE
                    else
                    {
                        Scene scene = SceneManager.GetSceneByPath(path);
                        if (scene.IsValid())
                        {
                            if (sceneList == null)
                            {
                                sceneList = new List <int>();
                            }
                            sceneList.Add(scene.handle);
                            isFound = true;
                        }
                    }
#endif
                    if (!isFound && logMissingNodes)
                    {
                        Debug.LogWarning("[HierarchyRestore] Can't find node with path: '" + path + "'");
                    }
                }
                gos = goList.ToArray();
                if (sceneList != null)
                {
                    sceneHandles = sceneList.ToArray();
                }
            }

            if ((gos != null && gos.Length > 0) ||
                (sceneHandles != null && sceneHandles.Length > 0))
            {
                HierarchyUtils.SetHierarchyWindowExpandedObjects(gos, sceneHandles);
            }
            else
            {
                HierarchyUtils.SetHierarchyWindowExpandedObjects(new GameObject[0], sceneHandles);
            }
        }