Parallel for Unity can make your games run faster! *Includes Perlin Noise Example
What is Parallel for Unity?
Loops run over x2 faster
Unlocks the power of multi-thread CPUs
Divides work across multiple threads
Executes for/foreach loops in Parallel
Executes lambda expressions in Parallel
Why is Parallel for Unity useful?
If you have expensive thread-safe loops you can easily run them in Parallel without writing complex thread management code.
How? Replace: for(int i = 0; i < max; i++){ … }
Parallel.For(0, max, (i) => { … });
A bunch of that code isn’t thread-safe. You can’t Instantiate or modify Transforms/GameObjects in a separate thread. Async Scheduler can schedule that code on the main thread, however the less thread-safe code is running in parallel the less benefits you get (You won’t benefit from Parallel if all of the code runs through Async). Most C# operations including math are thread-safe, Unity Engine classes are not.
The block of code containing the yield statement doesn’t belong in the loop because it only runs once at the end. It could simply be run after the loop.
You’re mixing co-routines and threads, if you wanted to add a delay in threads you use Thread.Sleep. But you must run the entire InstantiateHotspots method in a separate thread with Task or native threading.
This was made for .NET 3.5 when .NET 4 functionality such as Tasks wasn’t supported. So I guess it’s only useful if you cannot use .NET 4 or higher