This post isn’t a complaining thread, but instead is a solution thread.
I’ve read several posts of people complaining about how there are many places where Unity isn’t multi-threaded.
The following are places where I’ve found that multi-threading can be safely used in Unity. Feel free to add ideas and uses to this thread ![]()
Real-time Mesh Manipulation
- Store the Vector3 array of the mesh’s verts property into an array in your script.
- Start a thread (or threads at N indexes) that manipulate the Vector3 data
- If the data needs to be 100% accurate, set a flag or do a Thread.Sleep in the main thread (such as Unity’s Update function or whatever) until the data has been updated. In most cases I suspect the data doesn’t need to be 100% accurate, but can be arbitrarily assigned back to the mesh.verts property
- In the main thread assign the Vector3 array back to the mesh’s verts property.
NOTE: This method would be beneficial for a single time mesh manipulation, but would need to be a large mesh to offset the cost of starting the threads.
Image processing
- In the main thread, grab the Color[ ] data using the Texture2D.GetPixels function
- Start the thread/threads to manipulate the Color data
- Wait in main thread until the threads have completed
- assign the manipulated color data to the texture in the main thread using the Texture2D.SetPixels function
NOTE: This is only beneficial on large images such as 512x512 or greater to offset the cost of starting the threads.
Pathfinding
If you have several characters you can store the nav mesh into thread safe data such as the Vector3 class and calculate each pathfinding actor into their own thread, or all in a single thread depending on your needs.
Calculating Distances of Vector3’s
If you have a ton of objects (maybe a few hundred or more) you can store the positions into a Vector3 array every N frames and calculate the distances between them in another thread.
Why? Because 3D distance calculations use the Pythagorean theorem which use a square root. This could possibly alleviate hundreds or thousands of square root calculations per check. You may not want to be pushing data around every frame however, because reading/writing data can be more expensive than the square root calculation itself. You definitely will need to do testing to see if it does more harm or good.
NOTE: this wouldn’t make much difference and may make your app slower if there are only a few distances to calculate.
Sorting
NOTE 1: This would be pretty rare since to balance the cost of starting the threads necessary you’d need a lot of data to sort.
NOTE 2: I’m not writing a psuedo algorithm since it depends on the algorithm you use, however it’s possible to use pretty much any algorithm to sort data. I recommend the merge sort for data that must be compared. I’ve written multi-threaded code for the Bubble sort that has sped the sorting up by 20x or more with a dual core CPU, but, this is still slower than sorting with the merge sort in the main thread.
Again, please feel free to add to this thread as I’m curious where others use multi-threading in Unity ![]()
Thanks,
Nathan