Unity 5 - Multithreading?

Hello all,

If this question does not belong here, feel free to redirect me anywhere else :slight_smile:

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,
Garlik

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: Threading in C# - Free E-book

How about Application.LoadLevelAdditiveAsync? Is not it what you want?

Here is a package on Asset Store will help you to using threading easier. iS.CentralDispatch - a Multi-Threading Framework | Integration | Unity Asset Store
You can simply use only one line of code to start a thread and execute Unity API safely.

Try this… Thread Ninja.

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)