Skip to content

Commit 59951a2

Browse files
committed
fix(SharedMethods): find components in loading scenes
Change finding components to look in scenes that are loading or loaded.
1 parent 16fd9a1 commit 59951a2

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

Assets/VRTK/Source/Scripts/Utilities/VRTK_SharedMethods.cs

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,11 @@ public static GameObject FindEvenInactiveGameObject<T>(string gameObjectName = n
262262
{
263263
if (string.IsNullOrEmpty(gameObjectName))
264264
{
265-
T foundComponent = FindEvenInactiveComponentsInLoadedScenes<T>(searchAllScenes, true).FirstOrDefault();
265+
T foundComponent = FindEvenInactiveComponentsInValidScenes<T>(searchAllScenes, true).FirstOrDefault();
266266
return foundComponent == null ? null : foundComponent.gameObject;
267267
}
268268

269-
return FindEvenInactiveComponentsInLoadedScenes<T>(searchAllScenes)
269+
return FindEvenInactiveComponentsInValidScenes<T>(searchAllScenes)
270270
.Select(component =>
271271
{
272272
Transform transform = component.gameObject.transform.Find(gameObjectName);
@@ -287,7 +287,7 @@ public static GameObject FindEvenInactiveGameObject<T>(string gameObjectName = n
287287
/// <returns>All the found components. If no component is found an empty array is returned.</returns>
288288
public static T[] FindEvenInactiveComponents<T>(bool searchAllScenes = false) where T : Component
289289
{
290-
IEnumerable<T> results = FindEvenInactiveComponentsInLoadedScenes<T>(searchAllScenes);
290+
IEnumerable<T> results = FindEvenInactiveComponentsInValidScenes<T>(searchAllScenes);
291291
return results.ToArray();
292292
}
293293

@@ -303,7 +303,7 @@ public static T[] FindEvenInactiveComponents<T>(bool searchAllScenes = false) wh
303303
/// <returns>The found component. If no component is found `null` is returned.</returns>
304304
public static T FindEvenInactiveComponent<T>(bool searchAllScenes = false) where T : Component
305305
{
306-
return FindEvenInactiveComponentsInLoadedScenes<T>(searchAllScenes, true).FirstOrDefault();
306+
return FindEvenInactiveComponentsInValidScenes<T>(searchAllScenes, true).FirstOrDefault();
307307
}
308308

309309
/// <summary>
@@ -781,33 +781,52 @@ public static BuildTargetGroup[] GetValidBuildTargetGroups()
781781
/// <param name="searchAllScenes">If true, will search all loaded scenes, otherwise just the active scene.</param>
782782
/// <param name="stopOnMatch">If true, will stop searching objects as soon as a match is found.</param>
783783
/// <returns></returns>
784-
private static IEnumerable<T> FindEvenInactiveComponentsInLoadedScenes<T>(bool searchAllScenes, bool stopOnMatch = false) where T : Component
784+
private static IEnumerable<T> FindEvenInactiveComponentsInValidScenes<T>(bool searchAllScenes, bool stopOnMatch = false) where T : Component
785785
{
786-
List<T> results = new List<T>();
786+
IEnumerable<T> results;
787+
if (searchAllScenes)
788+
{
789+
List<T> allSceneResults = new List<T>();
790+
for (int sceneIndex = 0; sceneIndex < SceneManager.sceneCount; sceneIndex++)
791+
{
792+
allSceneResults.AddRange(FindEventInactiveComponentsInScene<T>(SceneManager.GetSceneAt(sceneIndex), stopOnMatch));
793+
}
794+
results = allSceneResults;
795+
}
796+
else
797+
{
798+
results = FindEventInactiveComponentsInScene<T>(SceneManager.GetActiveScene(), stopOnMatch);
799+
}
800+
801+
return results;
802+
}
787803

788-
for (int sceneIndex = 0; sceneIndex < SceneManager.sceneCount; sceneIndex++)
804+
/// <summary>
805+
/// The FIndEvenInactiveComponentsInScene method searches the specified scene for components matching the type supplied.
806+
/// </summary>
807+
/// <param name="scene">The scene to search. This scene must be valid, either loaded or loading.</param>
808+
/// <param name="stopOnMatch">If true, will stop searching objects as soon as a match is found.</param>
809+
/// <returns></returns>
810+
private static IEnumerable<T> FindEventInactiveComponentsInScene<T>(Scene scene, bool stopOnMatch = false)
811+
{
812+
List<T> results = new List<T>();
813+
foreach (GameObject rootObject in scene.GetRootGameObjects())
789814
{
790-
Scene scene = SceneManager.GetSceneAt(sceneIndex);
791-
if (scene.isLoaded && (searchAllScenes || scene == SceneManager.GetActiveScene()))
815+
if (stopOnMatch)
792816
{
793-
foreach (GameObject rootObject in scene.GetRootGameObjects())
817+
T foundComponent = rootObject.GetComponentInChildren<T>(true);
818+
if (foundComponent != null)
794819
{
795-
if (stopOnMatch)
796-
{
797-
T foundComponent = rootObject.GetComponentInChildren<T>(true);
798-
if (foundComponent != null)
799-
{
800-
results.Add(foundComponent);
801-
return results;
802-
}
803-
}
804-
else
805-
{
806-
results.AddRange(rootObject.GetComponentsInChildren<T>(true));
807-
}
820+
results.Add(foundComponent);
821+
return results;
808822
}
809823
}
824+
else
825+
{
826+
results.AddRange(rootObject.GetComponentsInChildren<T>(true));
827+
}
810828
}
829+
811830
return results;
812831
}
813832
}

0 commit comments

Comments
 (0)