I made a loading bar following Brackeys' tutorial here (and have changed a few things later). I noticed that when I load a level, and my loading bar comes out, it first shows 0%, waits, then directly 100% - without intermediate values!.
I'm guessing it could be due to some lag that occurs because the game is already using a lot of hardware, and hasn't left enough power for the rest of the processes, because I tried making it load while doing other things at the same time.
Here is the code that loads the level and changes the slider's value:
public void LoadLevel(int sceneIndex) {
StartCoroutine(StartFadeThenLoadLevel(sceneIndex));
}
IEnumerator StartFadeThenLoadLevel(int sceneIndex)
{
yield return new WaitForSeconds(0.9f);
StartCoroutine(LoadAsynchronously(sceneIndex));
}
IEnumerator LoadAsynchronously(int sceneIndex) {
yield return null;
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);
loadingScreen.SetActive(true);
chooseLevel.SetActive(false);
while (!operation.isDone) {
float progress = Mathf.Clamp01(operation.progress / 0.12f);
if (progress > 1) progress = 1;
slider.value = progress;
progressText.text = (int)(progress * 100f) + "%";
yield return null;
}
}
I saw that this mostly happens on more powerful phones, when it doesn't take more than a second to load the scene, than in older (slower) ones.
How can I fix this? I would like to have a very smooth loading bar that updates at least every 5%!
Thanks in advance for any help!
Edit: I added an FPS counter (Graphy) and i saw that there was a huge frame drop down to 1fps. That is probably what causes everything! Is there any way to prevent that? (If you think this is likely to be the problem, please answer this question instead of the main one!)
int progress
is 0 when operation.progress <= 0
and it is 1 when operation.progress > 0
.
So your progress
is always 1 because operation.progress
is always positive.
Try to simplify your progress calculation:
IEnumerator load(){
yield return null;
//Begin to load the Scene you specify
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneIndex);
//Don't let the Scene activate until you allow it to
asyncOperation.allowSceneActivation = false;
Debug.Log("Pro :" + asyncOperation.progress);
//When the load is still in progress, output the Text and progress bar
while (!asyncOperation.isDone)
{
//Output the current progress
Debug.Log("Loading progress: " + (asyncOperation.progress * 100) + "%");
// Check if the load has finished
if (asyncOperation.progress >= 0.9f)
{
//Change the Text to show the Scene is ready
asyncOperation.allowSceneActivation = true;
}
yield return null;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With