Question by
unity_sq26Sg98wIfncw · Mar 27, 2020 at 07:25 PM ·
scene-loadingscene-switchingload scenescene loadloadlevelasync
Order of SceneManager.LoadSceneAsync and UnloadSceneAsync.
I wrote some codes using SceneManager.LoadSceneAsync()
IEnumerator LoadTest()
{
//load persistent scene(some managers are placed here)
yield return SceneManager.LoadSceneAsync("persistentScene", LoadSceneMode.Single);
//load first scene
yield return SceneManager.LoadSceneAsync("firstScene", LoadSceneMode.Additive);
//preload second scene
var secondScene= SceneManager.LoadSceneAsync("secondScene", LoadSceneMode.Additive);
//dont activate immediately
secondScene.allowSceneActivation = false;
//instead of fadeout
yield return new WaitForSeconds(1.0f);
//Unload first scene
SceneManager.UnloadSceneAsync("firstScene");
//activate second scene
secondScene.allowSceneActivation = true;
}
It works like
construct "persistentScene"
↓
construct "firstScene"
↓
construct "secondScene"
↓
destruct "firstScene"
How can i destruct "firstScene" before construct "secondScene"?
Comment
Did you solve or find a workaround for your problem? I'm currently facing the same issue and don't see any way to achieve my goal. The Unloading always waits for the loaded scene to be activated :(.
Yes.
//Get current scene instance
var currentScene = Scene$$anonymous$$anager.GetSceneByName(currentSceneName);
//preload
var loadAsync = Scene$$anonymous$$anager.LoadSceneAsync(_nextScene, LoadScene$$anonymous$$ode.Additive);
loadAsync.allowSceneActivation = false;
//wait for complete loading and fadeout
while(loadAsync.progress < 0.9f || !Fade.IsFadeoutCompleted)
yield return null;
//Destroy GameObjects in CurrentScene
foreach (GameObject go in currentScene.GetRootGameObjects()) Destroy(go);
//or like this
//GameObject[] gameObjects = currentScene.GetRootGameObjects();
//int maxDestroyPerFrame = 128;
//for(int i = 0, int cnt = 0; i<gameObjects.length; ++i,++cnt)
//{
// Destroy(gameObjects[i]);
// if(cnt > maxDestroyPerFrame)
// cnt = 0; yield return null;
//}
loadAsync.allowSceneActivation = true;
yield return loadAsync;
yield return Scene$$anonymous$$anager.UnloadSceneAsync(currentScene);
Hey, thanks for your answer. Its seems a bit hacky, because i don't get a sceneUnloaded-callback this way and I need special handling whenever someone asks "IsSceneLoaded", but I guess it's better than nothing :).