I’m updating my scene management code, and I thought it would be good to start asynchronously loading the next scene while the current one ends, but I’ve encountered a problem.
For context, I’ve got scriptable objects that hold scene info and transitions, and I’m doing all of this from a manager scene that stays loaded.
Here’s what I had in mind:
AsyncOperation newSceneLoad = SceneManager.LoadSceneAsync(newScene.scene.buildIndex, LoadSceneMode.Additive);
//This keeps it from activating while the first scene finishes
newSceneLoad.allowSceneActivation = false;
//Fade out the current scene
yield return currentScene.EndScene();
AsyncOperation unload = SceneManager.UnloadSceneAsync(currentScene.scene.buildIndex);
yield return unload; //Should wait for the first scene to unload...
newSceneLoad.allowSceneActivation = true; //This line won't work here...
yield return newSceneLoad;
yield return newScene.StartScene();
Here I’m using UnloadSceneAsync because UnloadScene is depreciated, but it doesn’t work when used together with LoadSceneAsync. On this page it says: Unity - Scripting API: AsyncOperation.allowSceneActivation
set to false, and another
SceneManager.UnloadSceneAsync ) is
initialized, the last operation will
not be called before the first
allowSceneActivation is set to true.
That means I have to move the “allowSceneActivation = true” line up above the “yield return unload” line for unload to continue. But that means the scenes will overlap-- the first one will finishing loading before the second one is removed. And indeed when I do that I get some “There are 2 audio listeners in the scene” warnings, since the cameras for both scenes are present for a few frames.
So although it makes sense that Async loading could be used like this, it doesn’t seem to be possible. Can anyone confirm this or let me know a different way to do it?