- Home /
LoadSceneAsync never completes if allowSceneActivation is set to false
Hello,
I'm working on a project using Unity 2021.2.11f1 and I'm facing a weird issue where loading the scene asynchronously never completes if allowSceneActivation is set to false in the asyncOperation.
Here is my custom scene loading method
private IEnumerator LoadScene(string sceneName, LoadSceneMode loadMode, bool enableOnLoad = false)
{
//If the scene name is empty or if there is scene load already in progress, just exit
if (sceneName.IsNullOrWhitespace() || _loadSceneOperation != null && !_loadSceneOperation.isDone)
yield return null;
//Load scene asynchronously
_loadSceneOperation = SceneManager.LoadSceneAsync(sceneName, loadMode);
//BUG: Currently setting allowSceneActivation to false causes the scene loading to get stuck and never call the OnSceneLoaded event
_loadSceneOperation.allowSceneActivation = enableOnLoad;
_loadSceneOperation.completed += SceneLoadingOperationCompleted;
while (_loadSceneOperation != null && !_loadSceneOperation.isDone)
yield return null;
}
And here is how I call it
StartCoroutine(LoadScene(MainSceneName, LoadSceneMode.Additive));
The AsyncOperation never finishes and SceneManager.sceneLoaded event never gets triggered.
And in the editor the scene shows up as 'is loading' perpetually
If anyone has any idea, please advice!
Answer by rh_galaxy · Feb 15 at 11:13 AM
In your while loop, you need to allow scene activation at 90%, or it will never activate.
//wait until the asynchronous scene fully loads
while (!_loadSceneOperation.isDone)
{
//scene has loaded as much as possible, the last 10% can't be multi-threaded
if (_loadSceneOperation.progress >= 0.9f)
{
//(here you can do other stuff, like custom loading or something)
//then when done, you must call
_loadSceneOperation.allowSceneActivation = true;
}
yield return null;
}
Well, that's the thing. I don't want to activate the scene as soon as its loaded. I have an animation that I want to complete before making the scene visible. Which is why I call the below method after animation completion
private void OnSplashSequenceComplete()
{
if (_mainSceneLoaded)
{
//Set the main scene as active after the splash sequence is complete
SceneManager.SetActiveScene(SceneManager.GetSceneByName(MainSceneName));
}
}
But the problem here is that the '_mainSceneLoaded' boolean is always false because its set to true in the SceneManager.sceneLoaded callback which never gets called if the allowSceneActivation is set to false.
If I just set the allowSceneActivation to true while loading the scene, everything works fine but the scene becomes visible while the animation is still playing which is not what I want.
Maybe I missed something with _mainSceneLoaded, but this might work
bool splashComplete = false;
private void OnSplashSequenceComplete()
{
splashComplete = true;
}
//in the while loop
if (_loadSceneOperation.progress >= 0.9f)
{
if(splashComplete)
_loadSceneOperation.allowSceneActivation = true;
}
I see. I guess I can wait for splashCompletion in scene loading method instead of checking for scene loading completion in the splash animation method. I can make this work. Thanks for the workaround suggestion.
But the doubt still remains in my mind. The SceneManager.sceneLoaded event does not get called if allowSceneActivation is set to false while loading Async. Is this by design? Does the SceneManager.sceneLoaded callback not get called until the scene has been activated?
Your answer
Follow this Question
Related Questions
SceneManager.LoadScene used within promise (or async callback) does nothing 0 Answers
Get a reference to the newly loaded scene after a LoadSceneAsync() 2 Answers
Particular scene loads fine in the Unity Editor, but unable to load in the executable? 0 Answers
Can Jobs be used to load large scenes asynchronously without freezing? 1 Answer
About unnactive scenes and performace... 0 Answers