Skip to content

Commit

Permalink
Merge pull request #2 from brunomikoski/feature/add-support-for-inact…
Browse files Browse the repository at this point in the history
…ive-find

add: search for inactive game objects
  • Loading branch information
brunomikoski authored Dec 4, 2020
2 parents 4d24a3c + 2b9d965 commit 51a8e3c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
62 changes: 54 additions & 8 deletions Scripts/Editor/HierarchyKeeper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEditor.SceneManagement;
Expand Down Expand Up @@ -43,6 +44,9 @@ private static EditorWindow hierarchyWindow
}

private static HierarchyData cachedHierarchyData;
private static Dictionary<Transform, string> sceneItemsCache = new Dictionary<Transform, string>();
private static Dictionary<string, GameObject> pathToGameObjectsCache = new Dictionary<string, GameObject>();

private static HierarchyData hierarchyData
{
get
Expand All @@ -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)
Expand All @@ -78,18 +82,18 @@ 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;

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);
Expand All @@ -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<Transform>(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;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down

0 comments on commit 51a8e3c

Please sign in to comment.