Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by theprojectabot · May 08, 2012 at 08:06 PM · loadinglevelasync

app freeze and threading

I currently have an action that really hogs system resources( aka building my game world procedurally). For a just a few seconds i get the Mac spinner(aka the app has become unresponsive because it is in a tight loop of processing) . Id like to push all this action of to another thread but I read that Unity3d is not thread safe. Mainly I want this game loading asynchronously...

If anything id love just to throw a progress bar up or ingame spinner as Im loading the level...

sorry if Im being dense.. noob here.

Comment
Add comment · Show 6
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 bompi88 · May 08, 2012 at 09:35 PM 0
Share

You are using the Application.LoadLevelAsync ? Then you can take a look at AsyncOperation.progress for example.

avatar image theprojectabot · May 08, 2012 at 09:51 PM 0
Share

k thanks ill take a gander at that. I saw in another post they were talking about loading scenes in the background. I am actually instantiating a bunch of meshes in the scene after the scene is opened so I dont really want to be reloading the scene over and over again.. but perhaps AsyncOperation.progress is the right method....

avatar image bompi88 · May 08, 2012 at 10:06 PM 0
Share

Okey. You have to know that Application.LoadLevelASync is pro only. You are trying to instantiate terrains and sewing them together, and then instantiate objects in the scene? All this would happen in its own thread and you want to get the progress of this, in a visual way like a progressbar on the screen?

avatar image theprojectabot · May 08, 2012 at 10:10 PM 0
Share

exactly bomp... a progress bar as i load all the assets into the scene asynch style.

avatar image theprojectabot · May 08, 2012 at 10:11 PM 0
Share

once they are all loaded into memory or the process of calculations is done I would draw them to screen.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tomka · May 23, 2012 at 07:27 AM

Unity is not thread safe, however Instantiate is atomic, so you should actually be fine to call Instantiate from a thread (but I haven't tried this so test that out pretty thoroughly).

Also, so long as you aren't interacting with Unity methods you can do whatever you want in threads. So, you could for example you could shift the data processing side of the world generation into a thread, and then do the object instantiation within the main update loop. This won't give you a huge result as object creation uses up a lot of resources anyway, but it may help.

So, if Instantiate does turn out to be atomic, you should be able to do both data processing and object creation within a thread - bam: threaded world creation.

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 CHPedersen · May 23, 2012 at 09:24 AM 0
Share

No. This won't work. Please see my reply for more elaborate information. :)

avatar image
0

Answer by CHPedersen · May 23, 2012 at 09:24 AM

No. Not at all. Instantiate may NOT be called from within a separate thread. That an action is atomic does not by any means imply thread safety; rather, it means that a CPU core cannot suspend in the middle of its execution to concentrate on something else. Each atomic action blocks a thread until complete and represents a single term in the sequence, when you list actions in an interleaving.

Here's an example to demonstrate how this could go horribly, horribly wrong:

Suppose Unity's mainthread is in the middle of rendering the scene. It has come to the point where all vertices of all known objects have been sent to the graphics card, and the GPU is now in the process of passing vertices through the pipeline one by one. Every time an object is turned into pixels by the rasterizer, the graphics card compares the depth value of the fragments to the depths already recorded in the depth buffer, to see if the new object is closer to the camera, or perhaps occluded by an object that came through the pipeline at an earlier stage.

But now, all of a sudden, and concurrently in a separate thread, somebody calls Instantiate, and inserts a brand new object in the world. This totally fucks up the graphics card's work, because:

A: The new object's vertices haven't even been uploaded to the graphics card yet.

B: The new object might have a renderer whose shader belongs to the Opaque queue, and maybe the graphics card is currently busy rendering the Transparent queue. This screws up the order in which objects should be rendered to account for alpha correctly.

C: Maybe the new object has scripts attached to it that are supposed to start executing, but Unity's execution order might currently be in the phase where coroutines are executed. Now what? Is Unity supposed to execute the script's Awake() and Start(), or perhaps wait till next frame? Or what?

D: + 100 other things that a pipelined rendering system is not tailored for.

So, how to get around this?

One answer is to organize the extra workload so that the variables and objects affected are not directly related to Unity's rendering engine. This means, do not attempt to modify transforms, physics values, particle systems, and so on and so forth. Instead, perform calculations on non-Unity variables and objects, and then have Unity react to changes in these variables when it Updates its next frame. That way, you can split mathematically expensive calculations to a separate thread without messing with the sequence of things done by Unity.

Another answer is to attempt to split the instantiations into smaller chunks of work and then do it in coroutines. I.e., suppose your procedural code needs to instantiate 1000 boxes. Instead of writing code that loops from 1-1000 and call Instantiate on each iteration, write code that loops from 1-1000 and then yields for every 50 instantiations. Then you have effectively split the work over 20 frames instead of insisting on doing all 1000 in a single frame. This lets the rendering continue while work takes place.

How you choose to do this is up to you, but a general pointer is:

Coroutines make for prettier code, and are easier to write, maintain, and understand, but they're ultimately executed by the main thread, so you might still see a performance hit depending on how much work you do per yield.

True, separate threads offer more power, but are harder to implement and result in a code which is more difficult to debug and may behave randomly. Here be dragons. So choose wisely.

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

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

7 People are following this question.

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

Related Questions

Scene starts before scene loading is finished 0 Answers

A node in a childnode? 1 Answer

LoadLevel takes more time : Unity 0 Answers

getting loading progress for a level 1 Answer

Additive async loading breaks my lightmapping on mobile devices 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