How can I get a reference to the new Scene
object loaded after the AsyncOperation
returned by SceneManager.LoadSceneAsync()
completes?
You can use this code snippet:
Scene reference;
bool isLoading;
IEnumerator LoadScene(string sceneName)
{
this.isLoading = false;
AsyncOperation op = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
while (this.isLoading)
{
if (op.isDone)
{
this.isLoading = false;
this.reference = SceneManager.GetSceneByName(sceneName);
}
yield return 0;
}
}
Or you can go with the built-in delegate
sceneWasLoaded found in the SceneManager.
The easiest way to get a reference to the last scene you have loaded is to keep track of the total scene count. You can use this to call SceneManager.GetSceneAt(index). So in short:
int Index = SceneManager.sceneCount;
var operation = SceneManager.LoadSceneAsync(spawnedSceneName, LoadSceneMode.Additive);
operation.completed += (s) =>
{
Scene myScene = SceneManager.GetSceneAt(Index);
};