From 59951a2fd536b2cc3840a4828e6e0955ba511eca Mon Sep 17 00:00:00 2001 From: Kristian PD Date: Wed, 28 Mar 2018 15:04:42 -0400 Subject: [PATCH] fix(SharedMethods): find components in loading scenes Change finding components to look in scenes that are loading or loaded. --- .../Scripts/Utilities/VRTK_SharedMethods.cs | 65 ++++++++++++------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/Assets/VRTK/Source/Scripts/Utilities/VRTK_SharedMethods.cs b/Assets/VRTK/Source/Scripts/Utilities/VRTK_SharedMethods.cs index f3a7418e0..4727feb14 100644 --- a/Assets/VRTK/Source/Scripts/Utilities/VRTK_SharedMethods.cs +++ b/Assets/VRTK/Source/Scripts/Utilities/VRTK_SharedMethods.cs @@ -262,11 +262,11 @@ public static GameObject FindEvenInactiveGameObject(string gameObjectName = n { if (string.IsNullOrEmpty(gameObjectName)) { - T foundComponent = FindEvenInactiveComponentsInLoadedScenes(searchAllScenes, true).FirstOrDefault(); + T foundComponent = FindEvenInactiveComponentsInValidScenes(searchAllScenes, true).FirstOrDefault(); return foundComponent == null ? null : foundComponent.gameObject; } - return FindEvenInactiveComponentsInLoadedScenes(searchAllScenes) + return FindEvenInactiveComponentsInValidScenes(searchAllScenes) .Select(component => { Transform transform = component.gameObject.transform.Find(gameObjectName); @@ -287,7 +287,7 @@ public static GameObject FindEvenInactiveGameObject(string gameObjectName = n /// All the found components. If no component is found an empty array is returned. public static T[] FindEvenInactiveComponents(bool searchAllScenes = false) where T : Component { - IEnumerable results = FindEvenInactiveComponentsInLoadedScenes(searchAllScenes); + IEnumerable results = FindEvenInactiveComponentsInValidScenes(searchAllScenes); return results.ToArray(); } @@ -303,7 +303,7 @@ public static T[] FindEvenInactiveComponents(bool searchAllScenes = false) wh /// The found component. If no component is found `null` is returned. public static T FindEvenInactiveComponent(bool searchAllScenes = false) where T : Component { - return FindEvenInactiveComponentsInLoadedScenes(searchAllScenes, true).FirstOrDefault(); + return FindEvenInactiveComponentsInValidScenes(searchAllScenes, true).FirstOrDefault(); } /// @@ -781,33 +781,52 @@ public static BuildTargetGroup[] GetValidBuildTargetGroups() /// If true, will search all loaded scenes, otherwise just the active scene. /// If true, will stop searching objects as soon as a match is found. /// - private static IEnumerable FindEvenInactiveComponentsInLoadedScenes(bool searchAllScenes, bool stopOnMatch = false) where T : Component + private static IEnumerable FindEvenInactiveComponentsInValidScenes(bool searchAllScenes, bool stopOnMatch = false) where T : Component { - List results = new List(); + IEnumerable results; + if (searchAllScenes) + { + List allSceneResults = new List(); + for (int sceneIndex = 0; sceneIndex < SceneManager.sceneCount; sceneIndex++) + { + allSceneResults.AddRange(FindEventInactiveComponentsInScene(SceneManager.GetSceneAt(sceneIndex), stopOnMatch)); + } + results = allSceneResults; + } + else + { + results = FindEventInactiveComponentsInScene(SceneManager.GetActiveScene(), stopOnMatch); + } + + return results; + } - for (int sceneIndex = 0; sceneIndex < SceneManager.sceneCount; sceneIndex++) + /// + /// The FIndEvenInactiveComponentsInScene method searches the specified scene for components matching the type supplied. + /// + /// The scene to search. This scene must be valid, either loaded or loading. + /// If true, will stop searching objects as soon as a match is found. + /// + private static IEnumerable FindEventInactiveComponentsInScene(Scene scene, bool stopOnMatch = false) + { + List results = new List(); + foreach (GameObject rootObject in scene.GetRootGameObjects()) { - Scene scene = SceneManager.GetSceneAt(sceneIndex); - if (scene.isLoaded && (searchAllScenes || scene == SceneManager.GetActiveScene())) + if (stopOnMatch) { - foreach (GameObject rootObject in scene.GetRootGameObjects()) + T foundComponent = rootObject.GetComponentInChildren(true); + if (foundComponent != null) { - if (stopOnMatch) - { - T foundComponent = rootObject.GetComponentInChildren(true); - if (foundComponent != null) - { - results.Add(foundComponent); - return results; - } - } - else - { - results.AddRange(rootObject.GetComponentsInChildren(true)); - } + results.Add(foundComponent); + return results; } } + else + { + results.AddRange(rootObject.GetComponentsInChildren(true)); + } } + return results; } }