- Home /
Is there a way to show progress bar for a long running script?
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...
Answer by SirPaddow · Jul 29, 2019 at 07:17 PM
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: https://docs.unity3d.com/Manual/Coroutines.html
Your answer

Follow this Question
Related Questions
Load Level 01 Progress Screen 0 Answers
Want to create a moving line over a filling object 1 Answer
Trying to pick one or the other 1 Answer
Timer Progress Bar 2 Answers
Need help with basic magic system 0 Answers