Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by jni97 · Jan 30, 2016 at 09:53 AM · coroutinelagthread

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Verusoft · Jan 30, 2016 at 10:14 AM 0
Share

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)

avatar image jni97 · Jan 30, 2016 at 10:29 AM 0
Share

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...

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jni97 · Jan 30, 2016 at 10:32 AM 0
Share

Thanks, this answer is really helpful! A lot of work to do...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

38 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to reduce lag in coroutines - specifically WaitForSeconds()? 1 Answer

Does framerate affect Coroutines? 1 Answer

Editor test runner makes unity hang 1 Answer

My Android game is lagging.. 0 Answers

Massive lag spike on first Input.GetTouch with nothing on scene but one script (Android) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges