Skip to content

Commit 3f87bc4

Browse files
authored
fix: replace WaitForCompletion with async logic (#24)
1 parent b1b4fc3 commit 3f87bc4

File tree

5 files changed

+51
-39
lines changed

5 files changed

+51
-39
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ jobs:
3333
id: tests
3434
env:
3535
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
36+
UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
37+
UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
3638
with:
3739
projectPath: ${{ matrix.project-path }}
3840
testMode: ${{ matrix.testMode }}

.releaserc.json

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
{
2-
"tagFormat": "${version}",
3-
"plugins": [
4-
["@semantic-release/commit-analyzer", { "preset": "angular" }],
5-
"@semantic-release/release-notes-generator",
6-
["@semantic-release/changelog", { "preset": "angular" }],
7-
["@semantic-release/npm", { "npmPublish": false, "pkgRoot": "Packages/mygamedevtools-scene-loader" }],
8-
["@semantic-release/git", {
9-
"assets": ["Packages/mygamedevtools-scene-loader/package.json", "CHANGELOG.md"],
10-
"message": "ci(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
11-
}],
12-
"@semantic-release/github"
13-
]
1+
{
2+
"tagFormat": "${version}",
3+
"plugins": [
4+
["@semantic-release/commit-analyzer", { "preset": "angular" }],
5+
"@semantic-release/release-notes-generator",
6+
["@semantic-release/changelog", {
7+
"changelogTitle": "# Changelog"
8+
}],
9+
["@semantic-release/npm", { "npmPublish": false, "pkgRoot": "Packages/mygamedevtools-scene-loader" }],
10+
["@semantic-release/git", {
11+
"assets": ["Packages/mygamedevtools-scene-loader/package.json", "CHANGELOG.md"],
12+
"message": "ci(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
13+
}],
14+
"@semantic-release/github"
15+
]
1416
}

Packages/mygamedevtools-scene-loader/Runtime/Managers/SceneManagerAddressable.cs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Packages/mygamedevtools-scene-loader/Runtime/Utilities/AsyncOperationHandleGroup.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ public bool IsDone
4141
}
4242
}
4343

44-
public AsyncOperationHandleGroup(int initialCapacity)
44+
public readonly int SetIndexActive;
45+
46+
public AsyncOperationHandleGroup(List<AsyncOperationHandle<SceneInstance>> operationList, int setIndexActive = -1)
4547
{
46-
Operations = new List<AsyncOperationHandle<SceneInstance>>(initialCapacity);
48+
Operations = operationList;
49+
SetIndexActive = setIndexActive;
4750
}
4851

4952
public IList<SceneInstance> GetResult()

ProjectSettings/ProjectVersion.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
m_EditorVersion: 2023.2.1f1
2-
m_EditorVersionWithRevision: 2023.2.1f1 (a6dd9a634651)
1+
m_EditorVersion: 2023.2.3f1
2+
m_EditorVersionWithRevision: 2023.2.3f1 (21747dafc6ee)

0 commit comments

Comments
 (0)