NickP_2
September 2, 2013, 11:57pm
1
I want to create a loading screen when data is processing in a for loop.
an example:
void Start()
{
double currentData;
double endData = 50 * 50 * 50;
for(int i = 0; i < 50; i++)
{
for(int e = 0; e < 50; e++)
{
for(int a = 0; a < 50; a++)
{
currentData = a * e * i;
}
}
}
}
void OnGUI()
{
GUI.Label(new Rect((Screen.width - 200)/2, (Screen.height - 50)/2, 200, 50), "Loading blocks: " + 100/endData * currentData + "%");
}
Unfortunately this doesn’t work, any suggestions?
Don’t loop through things in one method? It’s going to go through that entire loop before it even touches OnGUI. You should use a coroutine or do this over the course of multiple frames via the update method, if you want it to share processing time with your loading graphics.
Try something like this:
void Start()
{
double currentData;
double endData = 50 * 50 * 50;
StartCoroutine("LoadData");
}
void OnGUI()
{
GUI.Label(new Rect((Screen.width - 200)/2, (Screen.height - 50)/2, 200, 50), "Loading blocks: " + 100/endData * currentData + "%");
}
IEnumerator LoadData()
{
for(int i = 0; i < 50; i++)
{
for(int e = 0; e < 50; e++)
{
for(int a = 0; a < 50; a++)
{
currentData = a * e * i;
yield return null;
}
}
}
StopCoroutine("LoadData");
}