Multithreaded Script call in Unity3d

I tried to implement multithreading script execution in Unity3d, but it seems like there is no way in Unity libraries and we have to use System.Threading provided by Mono. But they mentioned that Unity Scripting is not thread safe.

Can I implement safe and efficient multithreading in Unity3D using System.threading or some other platform independent API? Also how can I make sure the scripts are running in parallel?

A tag or link would be much appreciated.

Hello

+3


source to share


2 answers


I need streaming for an iOS app, unfortunately I never found a way to do this in Unity. But you can write a plugin that uses streams. In my application, I needed streaming to sync the music app, so I wrote an audio planning library that uses core-audio in xcode and you can do whatever streams you want there. But managing the sync between Unity and the plugin is pretty painful.



+5


source


Generally speaking, you are free to use multithreading. But there is one important caveat: all Unity API calls must be made from the main thread. So feel free to use streams for AI, pathfinding, whatever. At the end, you will need to pipe the calculation results to the main thread.



Unity offers an alternative to threads that are sometimes useful: coroutines . They allow you to run your function in fragments (exit and resume) and everything is done from the main thread, so you can use the Unity API. Note, however, that they are not suitable for heavy computations.

+1


source







All Articles