If this question does not belong here, feel free to redirect me anywhere else :)
Let me explain briefly my situation and my question:
I am developing with my newbie developer skills a 3D game with a procedural infinite terrain.
Currently, each time a new part of the terrain is being generated, I have a second or two lag. This seems normal, everything happens on the same thread, so until the generation is over, the game freezes.
I read that unity 5 will have a new multithread job scheduller.
Having learned everything I know on development with Unity Answers and other forums, I have to say I do not know what the above means exactly.
Will this new multithread job sheduller allow me to generate my terrain on a separate thread to prevent the freeze of the game during my terrain generation?
What could/couldn't we do with the multithreads?
I have tried to search for answers to this, but couldn't find any (or at least, none that I could understand).
I repeat that I am far from a pro developer and that pro dev language could be hard for me to read.
Thanks for any help understanding this
Happy development to all,
Every line of code needs to be executed. A single thread is basically one set of lines being executed, one after another.
Now sometimes, we don't want these lines to slow down other lines, like e.g. you want to generate a map using a complex algorythm. Now, if the generation happens, it prevents the original lines from continuing.
If you use a second thread for the generation, the original lines will still keep running, but you will not recieve the new map until the generation thread is complete. Once it is complete it can be passed into the lines as a set of computed paramaters.
So in your case you could run the generation algorthym on a seperate thread, you could avoid teh lag, as the rest of the code executes. The only problem might be if you outrun the generation algorythm and end up in empty space. You should generate smaller chunks of the map for faster speeds.
EDIT: I'll just edit in that I am unable to provide a reliable answer to the comment below.
Yes indeed, this would be my goal.
But as unity 4 is not multi-thread safe, I understand I should not try to do this (multi-threading)
My question (maybe unclear) is thus: will I be able to do so in Unity 5?
Indeed smaller chunks helps, but is still not enough to completely eliminate the lag even on my decent computer. This is why I try to understand more on multi-threading (for smaller computer configurations)
As Unity 5 is now out, is there any new information to be given about this topic?
Answer by tanoshimi
Feb 24, 2015 at 11:13 AM
You can already generate terrain (and, in fact, many bulk background tasks) on a separate thread using Unity 4.x - the scripting environment is just C#/Mono, so you can use the methods in System.Threading as you would in any .NET application.
The only thing you can't do is use Unity API methods themselves across different threads, as they are not thread-safe, but that won't prevent you from doing background processing tasks.
See bunny83's answer in this thread, or alexzzzz's example on this forum post for examples of multithreaded code in Unity.
Note that threading is a relatively advanced concept. If you're not a "pro dev" then you might have to do some background reading, as there are a whole lot of very easy ways to cock things up when threading. Try here for a beginner's guide: http://www.albahari.com/threading/
I suppose this will be the best answer for unity 4.x, but do you (or anyone else) knows if Unity API methods will be thread-safe in Unity 5?
Also, from my basic understanding, could you confirm I understand things correctly?:
In a separate thread one can:
Make heavy calculations that can be sent to the main thread
NOT instantiate GameObjects, modify mesh or do anything on GameObjects
Is this indeed what "not thread-safe" means?
Your understanding is correct for Unity 4.x. I don't know about Unity 5.x - I guess we'll find out when it's actually released :)
Thx. Marked as correct answer as its seems no more information currently exist for Unity 5
Answer by incorrect
Feb 24, 2015 at 10:58 AM
How about Application.LoadLevelAdditiveAsync? Is not it what you want?
Actually never thought of using that.
Though this would be a 'workaround' for me, as this requires Unity Pro, witch I do not own currently.
I really would love an explanation on Unity 5 multi-threading if anyone has it.
Thanks for the info in any case, I'll give it a try if/when I own PRO
Answer by QI
Aug 13, 2016 at 01:40 AM
Here is a package on Asset Store will help you to using threading easier. http://u3d.as/wQg You can simply use only one line of code to start a thread and execute Unity API safely.
Answer by JonPQ
Aug 19, 2016 at 05:55 AM
Try this... Thread Ninja. https://www.assetstore.unity3d.com/en/#!/content/15717
I had problem unzipping files, locked the whole game for a few seconds. and also have large procedural geometry generation. Just put your code in an ienumerator, then call it with the special async StartCoRoutine method in ThreadNinja, and bob's your uncle. (translation... it just works)
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
27 People are following this question.
EasyMovieTexture: is possible to load movie files from Application.persistentDataPath ?
why not the canvas show when unity play
how can define silder variable in unity 5.3
Cannot install Unity5 without internet
Installation problem with Unity 5.4.2 free version?What should I do for this?