Use Coroutine for a heavy calculation
Hi,
I created a terrain generator which works fine. My problem is the time it needs to create(for huge terrains about 30 seconds). During the creation-time the application is frozen, which isn't what I want. I tried to don't use the Unity API and use System.Threading.Thread but it isn't possible because I have to use functions from the Unity API.
I read that you can use Coroutines instead of a new Thread to stop the huge lag.
Now I don't know how I can create a coroutine which runs each frame a part of my method.
Can someone explain me the use of coroutines in that case, please? Thanks!
Check this out: http://docs.unity3d.com/$$anonymous$$anual/Coroutines.html and use while/for cycle in corutines with yield return null; but 30 sec is very long time. I$$anonymous$$HO you should generate terrain before game starts (in editor/during loading time/build time)
The terrain has to created at runtime because its a random terrain generator. The site was useful to understand coroutines, but I can't split my function like the for-loop in the examples. To understand: IEnumerator createTerrain() { CreateHeightmap(); yield return null; CreateTexture(); yield return null; CreateObjects(); yield return null; ... }
During the methods are executed the application is still frozen...
Answer by EvilTak · Jan 30, 2016 at 10:16 AM
For your case, a thread will still be better. You don't need to use the Unity API to generate a terrain, do you? I'd strongly recommend you use a thread to assign the heights of the terrain to a float array and then after the thread has stopped executing, you can assign the heights to the terrain.
If you are doing this in an Editor Script, just use EditorUtility.DisplayProgressBar
to show the progress and let the user know that the terrain is being generated. You don't need to use a coroutine or a thread.
But, if you are generating terrain while the game is running and do want to use coroutines, here's how you should do it.
If you are generating the terrain per heightmap pixel, then simply add a yield return null
after you process each row. For example,
IEnumerator GenerateTerrain()
{
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
// Generate terrain at pixel (x, y)
}
// Wait till the next frame
yield return null;
}
}
If it takes more than say 0.02 ms to generate each row, you could yield return null
earlier, like
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
// Generate terrain at pixel (x, y)
// Wait till the next frame if we have processed half of the row
if(x % (width / 2 + 1) == 0)
yield return null;
}
}
The same basic principle applies even if you have like a voxel or voronoi based terrain generation system. Just add yield return null
after processing each voxel row or each voronoi point.
Thanks, this answer is really helpful! A lot of work to do...