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 /
avatar image
0
Question by gorsefan · Feb 12, 2016 at 01:39 PM · coroutinesstartupinitializationprofiling

How to manage expensive startup/initialization operations without Unity hanging?

I've seen questions similar to this, but not one that's been answered.

On startup I need to do a bunch of expensive work, which takes 2-3 seconds (read & parse XML, generate ~10k meshes etc). Unsurprisingly the UI hangs, so on a Mac you get the beachball.

I have tried using coroutines;

 public class GameManager
 {
     private bool buildStarted = false;

     void Update() {
         if (Input.GetKeyDown("f") && !buildStarted) {
             buildStarted = true;
             StartCoroutine(doCreateWorld());
         }
     }

     void Start (){}

     IEnumerator doCreateWorld()
     {
         MeshManager.Init (); // cheap
         yield return new WaitForFixedUpdate();
         RenderManager.Init (); // expensive
         yield return new WaitForFixedUpdate();
         OSM.Territory.Instance.BuildTerritory (); // very expensive
         yield return new WaitForFixedUpdate();
     }

...and in the built-in Profiler I can see (I think!) that the work is now being split across 4 frames, which helps a bit, and does give some insight into what's going on (unlike not using coroutines..).

So, am I using co-routines correctly here? Should I split the expensive tasks into smaller, coroutined tasks? (I am already doing some multithreading in there.) The question is, how can I stop Unity hanging and remain responsive to the player?

Comment
Add comment
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

3 Replies

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

Answer by Dave-Carlile · Feb 12, 2016 at 01:41 PM

Yes, that is a good use of Coroutines. Just keep doing what you started - amortize the cost of long operations across many frames. You can use threading too if you want, but that can add a lot more complexity to deal with synchronization, and the fact that you must call (most) Unity API functions from the main thread.

Comment
Add comment · 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
1

Answer by alankemp · Feb 12, 2016 at 01:45 PM

Yes, that is a good way to do this.

If you are building lots of meshes inside BuildTerritory(), make that a coroutine as well and yield every N (100 or 500 or 1000) meshes.

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 gorsefan · Feb 15, 2016 at 01:11 PM 0
Share

Also a good & correct answer thank you very much, but Accepted Dave's as it was first sorry :)

avatar image
1

Answer by valyard · Feb 15, 2016 at 03:57 PM

You need to think about three things:

  1. What you DO need to generate at runtime.

  2. From what data you want to generate stuff at runtime.

  3. How you are going to spread tasks while generating stuff over time.

The question number (3) has been pretty much answered: slice your work in tasks and do them one by one frame over frame giving control to the system frequently so your app wouldn't look totally frozen. Some progress bar would be nice too.

If you don't depend on Unity API you can as well do these tasks in a separate thread leaving main thread for something more useful.

But questions (1) and (2) are also very important. Why are you generating everything at runtime? Can you generate all this stuff at build time? Can you partially generate something at build time? I guess you answered these questions already if you are generating things at runtime, but anyway.

Question (2) is about how you store/load data from which you want to generate stuff. People usually use JSON or XML which are slow and generate a lot of garbage requiring expensive GC passes. If you want to parse dynamic JSON you'd better use built-in JSON parser in Unity 5.3. If data source is not dynamic you can pregenerate it into Unity ScriptableObject which will be loaded by internal serialization mechanism and will be much faster.

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 gorsefan · Feb 16, 2016 at 01:44 PM 0
Share

Also a good answer, and sensible questions! Except for a camera and a plane, the entire world is procedurally generated, so I need to do a lot of work at runtime. Being slow to parse is the trade-off for the strictness of X$$anonymous$$L, naturally, and I will be serialising the parsed DO$$anonymous$$ at some point in the future. I envisage the finished project containing serialised parsed files, not raw X$$anonymous$$L, and the user will select one before building the world (think of them as "level configurations").

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

6 People are following this question.

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

Related Questions

A lot of time taken by AttributeHelperEngine.GetDefaultExecutionOrderFor() at Android app startup 0 Answers

Physics not initializing right away upon Play Testing in the editor 1 Answer

Does Unity support application bootstrapping? 1 Answer

Android: slow at start, then speeds up. Can the initialization be forced? 0 Answers

startup initialization error with unity 3 0 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