我正在尝试卸载先前添加的场景,然后按该顺序添加加载另一个场景。我使用下面的代码来做到这一点。此代码在编辑器和除 WebGL 之外的所有平台上都能正常工作。我知道 WebGL 不适用于异步代码,但这使用了一个协同程序,它应该在主线程上执行。那么,为什么这在 WebGL 中不起作用?
private IEnumerator UnloadAndLoadNextScene(IslandKey island)
{
// Avoid loading the same island twice.
if (island.ScenePath == previousIslandScenePath)
yield break;
string islandScenePath = island.ScenePath;
previousIslandScenePath = islandScenePath;
// Don't unload and load until previous unload and load is complete.
while (processingUnloadAndLoad)
yield return null;
// Immediately set processing unload and load to true to
// lock this method to only one instance at a time.
processingUnloadAndLoad = true;
// Unload previous scene.
if (unloadSceneHandle.IsValid())
{
AsyncOperationHandle unloadingScene = Addressables.UnloadSceneAsync(unloadSceneHandle);
yield return unloadingScene;
}
// Load scene.
AsyncOperationHandle<SceneInstance> loadingScene = Addressables.LoadSceneAsync(islandScenePath, LoadSceneMode.Additive);
yield return loadingScene;
if (loadingScene.Status == AsyncOperationStatus.Succeeded)
unloadSceneHandle = loadingScene;
...
// Unload and load is now complete, so unlock method.
processingUnloadAndLoad = false;
}
}