From 2b9d965c1e0a6c9a5cbaee230fd338292d389110 Mon Sep 17 00:00:00 2001 From: Bruno Mikoski Date: Fri, 4 Dec 2020 13:16:29 +0000 Subject: [PATCH] add: search for inactive game objects --- Scripts/Editor/HierarchyKeeper.cs | 62 +++++++++++++++++++++++++++---- package.json | 2 +- 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/Scripts/Editor/HierarchyKeeper.cs b/Scripts/Editor/HierarchyKeeper.cs index 96ab6eb..598ac1c 100644 --- a/Scripts/Editor/HierarchyKeeper.cs +++ b/Scripts/Editor/HierarchyKeeper.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Reflection; using UnityEditor; using UnityEditor.SceneManagement; @@ -43,6 +44,9 @@ private static EditorWindow hierarchyWindow } private static HierarchyData cachedHierarchyData; + private static Dictionary sceneItemsCache = new Dictionary(); + private static Dictionary pathToGameObjectsCache = new Dictionary(); + private static HierarchyData hierarchyData { get @@ -68,7 +72,7 @@ private static void OnSceneUnloaded(Scene scene) private static void OnSceneLoaded(Scene scene, LoadSceneMode arg1) { - RestoreExpandedData(scene.path); + RestoreExpandedData(scene); } private static void OnSceneClosing(Scene scene, bool removingscene) @@ -78,10 +82,10 @@ private static void OnSceneClosing(Scene scene, bool removingscene) private static void OnSceneOpened(Scene scene, OpenSceneMode mode) { - RestoreExpandedData(scene.path); + RestoreExpandedData(scene); } - private static void RestoreExpandedData(string scenePath) + private static void RestoreExpandedData(Scene scene) { if (hierarchyWindow == null) return; @@ -89,7 +93,7 @@ private static void RestoreExpandedData(string scenePath) if (!HierarchyKeeperTools.IsActive()) return; - if (!hierarchyData.TryGetSceneData(scenePath, out SceneHierarchyData sceneHierarchyData)) + if (!hierarchyData.TryGetSceneData(scene.path, out SceneHierarchyData sceneHierarchyData)) return; object sceneHierarchy = SceneHierarchyWindowType.GetProperty(SceneHierarchyPropertyName).GetValue(hierarchyWindow); @@ -105,16 +109,58 @@ private static void RestoreExpandedData(string scenePath) for (int i = 0; i < sceneHierarchyData.itemsPath.Count; i++) { string expandedItemPath = sceneHierarchyData.itemsPath[i]; - GameObject path = GameObject.Find(expandedItemPath); - if (path == null) - continue; + GameObject gameObjectAtPath = GameObject.Find(expandedItemPath); + if (gameObjectAtPath == null) + { + if (!TryToFindBySceneRootObjects(scene, expandedItemPath, out gameObjectAtPath)) + continue; + } + + setExpandedMethod.Invoke(sceneHierarchy, new object[] {gameObjectAtPath.GetInstanceID(), true}); + } + } + + private static bool TryToFindBySceneRootObjects(Scene scene, string targetItemPath, out GameObject resultGameObject) + { + if (pathToGameObjectsCache.TryGetValue(targetItemPath, out resultGameObject)) + { + if (resultGameObject != null) + return true; - setExpandedMethod.Invoke(sceneHierarchy, new object[] {path.GetInstanceID(), true}); + pathToGameObjectsCache.Remove(targetItemPath); + } + + GameObject[] objects = scene.GetRootGameObjects(); + for (int i = 0; i < objects.Length; i++) + { + GameObject rootGameObject = objects[i]; + Transform[] allChild = rootGameObject.GetComponentsInChildren(true); + for (int j = 0; j < allChild.Length; j++) + { + Transform transform = allChild[j]; + if (!sceneItemsCache.TryGetValue(transform, out string itemPath)) + { + itemPath = transform.GetPath(); + sceneItemsCache.Add(transform, itemPath); + } + + if (itemPath.Equals(targetItemPath, StringComparison.OrdinalIgnoreCase)) + { + resultGameObject = transform.gameObject; + pathToGameObjectsCache.Add(targetItemPath, resultGameObject); + return true; + } + } } + + return false; } private static void StoreExpandedData(string targetScenePath) { + if (EditorApplication.isPlayingOrWillChangePlaymode) + return; + if (hierarchyWindow == null) return; diff --git a/package.json b/package.json index 4fb31ba..fa495bf 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.brunomikoski.scenehierarchykeeper", "displayName": "Scene Hierarchy Keeper", - "version": "0.0.1", + "version": "0.0.2", "unity": "2018.4", "description": "A tool that keeps your scene hierarchy between editor/play time, and opening and close scene", "keywords": [