I have a pretty big script that takes several seconds to load
I wonder if there is a way to show a progress bar while the user waits
I tried break up the script into several parts with int progress, but the progress go straight from 0 to 100…
I have a pretty big script that takes several seconds to load
I wonder if there is a way to show a progress bar while the user waits
I tried break up the script into several parts with int progress, but the progress go straight from 0 to 100…
You need to work with a coroutine, so that the frame can end (avoid freezing). If you don’t, all the process is actually done within one frame, so it’s normal that your value goes from 0 to 100.
Here is how I do:
public class BigProcessMB : MonoBehaviour
{
public int progress; // 0 - 100
public void LaunchBigProcess()
{
progress = 0;
StartCoroutine(DoBigProcess());
}
public IEnumerator DoBigProcess()
{
// Do stuff
progress = 10;
yield return null;
// Do stuff
progress = 20;
yield return null;
// etc.
}
}
More about coroutines here: Unity - Manual: Coroutines