@@ -262,11 +262,11 @@ public static GameObject FindEvenInactiveGameObject<T>(string gameObjectName = n
262
262
{
263
263
if ( string . IsNullOrEmpty ( gameObjectName ) )
264
264
{
265
- T foundComponent = FindEvenInactiveComponentsInLoadedScenes < T > ( searchAllScenes , true ) . FirstOrDefault ( ) ;
265
+ T foundComponent = FindEvenInactiveComponentsInValidScenes < T > ( searchAllScenes , true ) . FirstOrDefault ( ) ;
266
266
return foundComponent == null ? null : foundComponent . gameObject ;
267
267
}
268
268
269
- return FindEvenInactiveComponentsInLoadedScenes < T > ( searchAllScenes )
269
+ return FindEvenInactiveComponentsInValidScenes < T > ( searchAllScenes )
270
270
. Select ( component =>
271
271
{
272
272
Transform transform = component . gameObject . transform . Find ( gameObjectName ) ;
@@ -287,7 +287,7 @@ public static GameObject FindEvenInactiveGameObject<T>(string gameObjectName = n
287
287
/// <returns>All the found components. If no component is found an empty array is returned.</returns>
288
288
public static T [ ] FindEvenInactiveComponents < T > ( bool searchAllScenes = false ) where T : Component
289
289
{
290
- IEnumerable < T > results = FindEvenInactiveComponentsInLoadedScenes < T > ( searchAllScenes ) ;
290
+ IEnumerable < T > results = FindEvenInactiveComponentsInValidScenes < T > ( searchAllScenes ) ;
291
291
return results . ToArray ( ) ;
292
292
}
293
293
@@ -303,7 +303,7 @@ public static T[] FindEvenInactiveComponents<T>(bool searchAllScenes = false) wh
303
303
/// <returns>The found component. If no component is found `null` is returned.</returns>
304
304
public static T FindEvenInactiveComponent < T > ( bool searchAllScenes = false ) where T : Component
305
305
{
306
- return FindEvenInactiveComponentsInLoadedScenes < T > ( searchAllScenes , true ) . FirstOrDefault ( ) ;
306
+ return FindEvenInactiveComponentsInValidScenes < T > ( searchAllScenes , true ) . FirstOrDefault ( ) ;
307
307
}
308
308
309
309
/// <summary>
@@ -781,33 +781,52 @@ public static BuildTargetGroup[] GetValidBuildTargetGroups()
781
781
/// <param name="searchAllScenes">If true, will search all loaded scenes, otherwise just the active scene.</param>
782
782
/// <param name="stopOnMatch">If true, will stop searching objects as soon as a match is found.</param>
783
783
/// <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
785
785
{
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
+ }
787
803
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 ( ) )
789
814
{
790
- Scene scene = SceneManager . GetSceneAt ( sceneIndex ) ;
791
- if ( scene . isLoaded && ( searchAllScenes || scene == SceneManager . GetActiveScene ( ) ) )
815
+ if ( stopOnMatch )
792
816
{
793
- foreach ( GameObject rootObject in scene . GetRootGameObjects ( ) )
817
+ T foundComponent = rootObject . GetComponentInChildren < T > ( true ) ;
818
+ if ( foundComponent != null )
794
819
{
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 ;
808
822
}
809
823
}
824
+ else
825
+ {
826
+ results . AddRange ( rootObject . GetComponentsInChildren < T > ( true ) ) ;
827
+ }
810
828
}
829
+
811
830
return results ;
812
831
}
813
832
}
0 commit comments