Skip to content

Commit

Permalink
fix: replace WaitForCompletion with async logic (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoborks committed Dec 20, 2023
1 parent b1b4fc3 commit 3f87bc4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ jobs:
id: tests
env:
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
with:
projectPath: ${{ matrix.project-path }}
testMode: ${{ matrix.testMode }}
Expand Down
28 changes: 15 additions & 13 deletions .releaserc.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"tagFormat": "${version}",
"plugins": [
["@semantic-release/commit-analyzer", { "preset": "angular" }],
"@semantic-release/release-notes-generator",
["@semantic-release/changelog", { "preset": "angular" }],
["@semantic-release/npm", { "npmPublish": false, "pkgRoot": "Packages/mygamedevtools-scene-loader" }],
["@semantic-release/git", {
"assets": ["Packages/mygamedevtools-scene-loader/package.json", "CHANGELOG.md"],
"message": "ci(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
}],
"@semantic-release/github"
]
{
"tagFormat": "${version}",
"plugins": [
["@semantic-release/commit-analyzer", { "preset": "angular" }],
"@semantic-release/release-notes-generator",
["@semantic-release/changelog", {
"changelogTitle": "# Changelog"
}],
["@semantic-release/npm", { "npmPublish": false, "pkgRoot": "Packages/mygamedevtools-scene-loader" }],
["@semantic-release/git", {
"assets": ["Packages/mygamedevtools-scene-loader/package.json", "CHANGELOG.md"],
"message": "ci(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
}],
"@semantic-release/github"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public async ValueTask<Scene[]> LoadScenesAsync(ILoadSceneInfo[] sceneInfos, int
if (setIndexActive >= sceneInfos.Length)
throw new ArgumentException(nameof(setIndexActive), $"[{GetType().Name}] Provided index to set active is bigger than the provided scene group size.");

var operationGroup = GetLoadSceneOperations(sceneInfos, ref setIndexActive);
var operationGroup = await GetLoadSceneOperations(sceneInfos, setIndexActive);
if (operationGroup.Operations.Count == 0)
return Array.Empty<Scene>();

Expand All @@ -98,8 +98,8 @@ public async ValueTask<Scene[]> LoadScenesAsync(ILoadSceneInfo[] sceneInfos, int
foreach (var sceneInstance in loadedScenes)
SceneLoaded?.Invoke(sceneInstance.Scene);

if (setIndexActive >= 0)
SetActiveScene(loadedScenes[setIndexActive].Scene);
if (operationGroup.SetIndexActive >= 0)
SetActiveScene(loadedScenes[operationGroup.SetIndexActive].Scene);

return ToSceneArray(loadedScenes);
}
Expand Down Expand Up @@ -131,14 +131,16 @@ public async ValueTask<Scene[]> UnloadScenesAsync(ILoadSceneInfo[] sceneInfos)
for (i = 0; i < unloadingLength; i++)
loadedScenes.Remove(unloadingScenes[i]);

var operationGroup = new AsyncOperationHandleGroup(loadedScenes.Count);
var operationList = new List<AsyncOperationHandle<SceneInstance>>(loadedScenes.Count);
foreach (var sceneInstance in loadedScenes)
{
operationGroup.Operations.Add(Addressables.UnloadSceneAsync(sceneInstance));
operationList.Add(Addressables.UnloadSceneAsync(sceneInstance));
_loadedScenes.Remove(sceneInstance);
_unloadingScenes.Add(sceneInstance);
}

var operationGroup = new AsyncOperationHandleGroup(operationList);

while (!operationGroup.IsDone)
#if USE_UNITASK
await UniTask.Yield();
Expand Down Expand Up @@ -187,18 +189,19 @@ async ValueTask<Scene> WaitForSceneUnload(SceneInstance sceneInstance)
return sceneInstance.Scene;
}

AsyncOperationHandleGroup GetLoadSceneOperations(ILoadSceneInfo[] sceneInfos, ref int setIndexActive)
async ValueTask<AsyncOperationHandleGroup> GetLoadSceneOperations(ILoadSceneInfo[] sceneInfos, int setIndexActive)
{
var sceneLength = sceneInfos.Length;
var operationGroup = new AsyncOperationHandleGroup(sceneLength);
var operationList = new List<AsyncOperationHandle<SceneInstance>>(sceneLength);
for (int i = 0; i < sceneLength; i++)
{
if (TryGetLoadSceneOperation(sceneInfos[i], out var operation))
operationGroup.Operations.Add(operation);
var operation = await GetLoadSceneOperation(sceneInfos[i]);
if (operation.IsValid())
operationList.Add(operation);
else if (i == setIndexActive)
setIndexActive = -1;
}
return operationGroup;
return new AsyncOperationHandleGroup(operationList, setIndexActive);
}

IList<SceneInstance> GetLastLoadedScenesByInfos(ILoadSceneInfo[] sceneInfos, out int[] unloadingIndexes)
Expand Down Expand Up @@ -256,26 +259,23 @@ Scene[] ToSceneArray(IList<SceneInstance> sceneInstances)
return sceneArray;
}

bool TryGetLoadSceneOperation(ILoadSceneInfo sceneInfo, out AsyncOperationHandle<SceneInstance> operationHandle)
async ValueTask<AsyncOperationHandle<SceneInstance>> GetLoadSceneOperation(ILoadSceneInfo sceneInfo)
{
operationHandle = default;
if (sceneInfo.Reference is AssetReference assetReference)
operationHandle = assetReference.LoadSceneAsync(LoadSceneMode.Additive);
return assetReference.LoadSceneAsync(LoadSceneMode.Additive);
else if (sceneInfo.Reference is string name)
{
if (ValidateAssetReference(name))
operationHandle = Addressables.LoadSceneAsync(name, LoadSceneMode.Additive);
if (await ValidateAssetReference(name))
return Addressables.LoadSceneAsync(name, LoadSceneMode.Additive);
else
{
Debug.LogWarning($"[{GetType().Name}] Scene '{name}' couldn't be loaded because its address found no Addressable Assets.");
return false;
return default;
}
}

bool isValid = operationHandle.IsValid();
if (!isValid)
Debug.LogWarning($"[{GetType().Name}] Unexpected {nameof(ILoadSceneInfo.Reference)} type: {sceneInfo.Reference}");
return isValid;
Debug.LogWarning($"[{GetType().Name}] Unexpected {nameof(ILoadSceneInfo.Reference)} type: {sceneInfo.Reference}");
return default;
}

bool TryGetInstanceFromScene(Scene scene, out SceneInstance sceneInstance)
Expand All @@ -291,10 +291,15 @@ bool TryGetInstanceFromScene(Scene scene, out SceneInstance sceneInstance)
return false;
}

bool ValidateAssetReference(object reference)
async ValueTask<bool> ValidateAssetReference(object reference)
{
var operation = Addressables.LoadResourceLocationsAsync(reference);
operation.WaitForCompletion();
#if USE_UNITASK
await operation;
#else
while (!operation.IsDone)
await Task.Yield();
#endif

return operation.Result.Count > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ public bool IsDone
}
}

public AsyncOperationHandleGroup(int initialCapacity)
public readonly int SetIndexActive;

public AsyncOperationHandleGroup(List<AsyncOperationHandle<SceneInstance>> operationList, int setIndexActive = -1)
{
Operations = new List<AsyncOperationHandle<SceneInstance>>(initialCapacity);
Operations = operationList;
SetIndexActive = setIndexActive;
}

public IList<SceneInstance> GetResult()
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2023.2.1f1
m_EditorVersionWithRevision: 2023.2.1f1 (a6dd9a634651)
m_EditorVersion: 2023.2.3f1
m_EditorVersionWithRevision: 2023.2.3f1 (21747dafc6ee)

0 comments on commit 3f87bc4

Please sign in to comment.