- Home /
Loading Bar for Awake Function
Hello, I have a pretty simple loading bar currently in a main menu scene.
IEnumerator LoadingScreen(string sceneName)
{
loadingScreenObj.SetActive(true);
aSync = SceneManager.LoadSceneAsync(sceneName);
aSync.allowSceneActivation = false;
while (!aSync.isDone)
{
loadSlider.value = aSync.progress;
if(aSync.progress == 0.9f)
{
loadSlider.value = 1f;
aSync.allowSceneActivation = true;
}
yield return null;
}
}
This is all fine and dandy, and works well enough (I followed a tutorial to get the effect). The problem is that the game scene that it loads into has an Awake() function that takes about 20 seconds to get through because it generates land and initializes a lot of scripts.
After a bit of research, I'm still confused. How do I get it so that the loading bar in the main menu includes this awake function in the game scene? Can I make it so that Text in the UI of the main menu scene changes based on the part of the super long awake function?
Is any of this even possible?
This is a duplicate of this question: https://answers.unity.com/questions/1631829/loading-bar-in-different-scenes.html
Answer by Pangamini · May 15, 2019 at 08:48 AM
The only solution here is: Don't create Awake() function that takes so long. Make it initiate the process, but if your Awake() blocks for 20 seconds, there is no way how you can render its progress (no frame is rendered until the Awake() finishes (maybe some native rendering plugin could do it, just a blind guess) What you can do is to split your awake to a coroutine that yields frequently, allowing the engine loop to continue. Of course, you'd need to prevent your active game objects to start 'living', either by postponing their creation (spawning them last) or setting a timescale to 0, so nothing really happens in that time (your characters don't fall through unfinished level).
From the coroutine, you can even start some other threads (if your level generator includes code that can be executed on other threads), and wait for the thread completion in the coroutine.
Sometimes you have a level with many small awake/start calls, but together are very slow. You can avoid that by: Off-loading the awake/start work, pre-bake the situation. But this is not possible if you generate things procedurally. What I did on one project was similar, a coroutine that activates gameObjects in batches in a coroutine. This is possible, if your awake/start objects don't depend entirely on the order of activation of other objects (eg. you don't care that one character had awake and start called before the other one had awake called).
In conclusion, there are options, but you have to split the execution between frames.