@@ -78,7 +78,7 @@ public async ValueTask<Scene[]> LoadScenesAsync(ILoadSceneInfo[] sceneInfos, int
7878 if ( setIndexActive >= sceneInfos . Length )
7979 throw new ArgumentException ( nameof ( setIndexActive ) , $ "[{ GetType ( ) . Name } ] Provided index to set active is bigger than the provided scene group size.") ;
8080
81- var operationGroup = GetLoadSceneOperations ( sceneInfos , ref setIndexActive ) ;
81+ var operationGroup = await GetLoadSceneOperations ( sceneInfos , setIndexActive ) ;
8282 if ( operationGroup . Operations . Count == 0 )
8383 return Array . Empty < Scene > ( ) ;
8484
@@ -98,8 +98,8 @@ public async ValueTask<Scene[]> LoadScenesAsync(ILoadSceneInfo[] sceneInfos, int
9898 foreach ( var sceneInstance in loadedScenes )
9999 SceneLoaded ? . Invoke ( sceneInstance . Scene ) ;
100100
101- if ( setIndexActive >= 0 )
102- SetActiveScene ( loadedScenes [ setIndexActive ] . Scene ) ;
101+ if ( operationGroup . SetIndexActive >= 0 )
102+ SetActiveScene ( loadedScenes [ operationGroup . SetIndexActive ] . Scene ) ;
103103
104104 return ToSceneArray ( loadedScenes ) ;
105105 }
@@ -131,14 +131,16 @@ public async ValueTask<Scene[]> UnloadScenesAsync(ILoadSceneInfo[] sceneInfos)
131131 for ( i = 0 ; i < unloadingLength ; i ++ )
132132 loadedScenes . Remove ( unloadingScenes [ i ] ) ;
133133
134- var operationGroup = new AsyncOperationHandleGroup ( loadedScenes . Count ) ;
134+ var operationList = new List < AsyncOperationHandle < SceneInstance > > ( loadedScenes . Count ) ;
135135 foreach ( var sceneInstance in loadedScenes )
136136 {
137- operationGroup . Operations . Add ( Addressables . UnloadSceneAsync ( sceneInstance ) ) ;
137+ operationList . Add ( Addressables . UnloadSceneAsync ( sceneInstance ) ) ;
138138 _loadedScenes . Remove ( sceneInstance ) ;
139139 _unloadingScenes . Add ( sceneInstance ) ;
140140 }
141141
142+ var operationGroup = new AsyncOperationHandleGroup ( operationList ) ;
143+
142144 while ( ! operationGroup . IsDone )
143145#if USE_UNITASK
144146 await UniTask . Yield ( ) ;
@@ -187,18 +189,19 @@ async ValueTask<Scene> WaitForSceneUnload(SceneInstance sceneInstance)
187189 return sceneInstance . Scene ;
188190 }
189191
190- AsyncOperationHandleGroup GetLoadSceneOperations ( ILoadSceneInfo [ ] sceneInfos , ref int setIndexActive )
192+ async ValueTask < AsyncOperationHandleGroup > GetLoadSceneOperations ( ILoadSceneInfo [ ] sceneInfos , int setIndexActive )
191193 {
192194 var sceneLength = sceneInfos . Length ;
193- var operationGroup = new AsyncOperationHandleGroup ( sceneLength ) ;
195+ var operationList = new List < AsyncOperationHandle < SceneInstance > > ( sceneLength ) ;
194196 for ( int i = 0 ; i < sceneLength ; i ++ )
195197 {
196- if ( TryGetLoadSceneOperation ( sceneInfos [ i ] , out var operation ) )
197- operationGroup . Operations . Add ( operation ) ;
198+ var operation = await GetLoadSceneOperation ( sceneInfos [ i ] ) ;
199+ if ( operation . IsValid ( ) )
200+ operationList . Add ( operation ) ;
198201 else if ( i == setIndexActive )
199202 setIndexActive = - 1 ;
200203 }
201- return operationGroup ;
204+ return new AsyncOperationHandleGroup ( operationList , setIndexActive ) ;
202205 }
203206
204207 IList < SceneInstance > GetLastLoadedScenesByInfos ( ILoadSceneInfo [ ] sceneInfos , out int [ ] unloadingIndexes )
@@ -256,26 +259,23 @@ Scene[] ToSceneArray(IList<SceneInstance> sceneInstances)
256259 return sceneArray ;
257260 }
258261
259- bool TryGetLoadSceneOperation ( ILoadSceneInfo sceneInfo , out AsyncOperationHandle < SceneInstance > operationHandle )
262+ async ValueTask < AsyncOperationHandle < SceneInstance > > GetLoadSceneOperation ( ILoadSceneInfo sceneInfo )
260263 {
261- operationHandle = default ;
262264 if ( sceneInfo . Reference is AssetReference assetReference )
263- operationHandle = assetReference . LoadSceneAsync ( LoadSceneMode . Additive ) ;
265+ return assetReference . LoadSceneAsync ( LoadSceneMode . Additive ) ;
264266 else if ( sceneInfo . Reference is string name )
265267 {
266- if ( ValidateAssetReference ( name ) )
267- operationHandle = Addressables . LoadSceneAsync ( name , LoadSceneMode . Additive ) ;
268+ if ( await ValidateAssetReference ( name ) )
269+ return Addressables . LoadSceneAsync ( name , LoadSceneMode . Additive ) ;
268270 else
269271 {
270272 Debug . LogWarning ( $ "[{ GetType ( ) . Name } ] Scene '{ name } ' couldn't be loaded because its address found no Addressable Assets.") ;
271- return false ;
273+ return default ;
272274 }
273275 }
274276
275- bool isValid = operationHandle . IsValid ( ) ;
276- if ( ! isValid )
277- Debug . LogWarning ( $ "[{ GetType ( ) . Name } ] Unexpected { nameof ( ILoadSceneInfo . Reference ) } type: { sceneInfo . Reference } ") ;
278- return isValid ;
277+ Debug . LogWarning ( $ "[{ GetType ( ) . Name } ] Unexpected { nameof ( ILoadSceneInfo . Reference ) } type: { sceneInfo . Reference } ") ;
278+ return default ;
279279 }
280280
281281 bool TryGetInstanceFromScene ( Scene scene , out SceneInstance sceneInstance )
@@ -291,10 +291,15 @@ bool TryGetInstanceFromScene(Scene scene, out SceneInstance sceneInstance)
291291 return false ;
292292 }
293293
294- bool ValidateAssetReference ( object reference )
294+ async ValueTask < bool > ValidateAssetReference ( object reference )
295295 {
296296 var operation = Addressables . LoadResourceLocationsAsync ( reference ) ;
297- operation . WaitForCompletion ( ) ;
297+ #if USE_UNITASK
298+ await operation ;
299+ #else
300+ while ( ! operation . IsDone )
301+ await Task . Yield ( ) ;
302+ #endif
298303
299304 return operation . Result . Count > 0 ;
300305 }
0 commit comments