Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use c# threads in unity3d for android platform?

I am in need to load files, scenes and play animations in threads.. Tried loading files via www in Android... how to do other stuff via threads? But how come a game engine doesn't allow us to create threads? or my understanding is wrong? how can one create threads in UNITY3D?

like image 633
ParuthidotExE Avatar asked Dec 20 '22 17:12

ParuthidotExE


2 Answers

You can use threads in Unity but the engine is not thread safe. Usually you run detached threads (from the Unity UI) to do long running processes and check on results (you cannot interact with Unity from the working thread). The common approach is to use a class which represents a threading job which will be initialized by the Unity main thread. Then you start a worker thread on a function of that class and let it do it's job (Coroutines run on the Unity main thread so are not real threads. Best article on Coroutines is here)

Here's an example of the approach described above (see accepted answer):

http://answers.unity3d.com/questions/357033/unity3d-and-c-coroutines-vs-threading.html

You might also want to try a UnityGems package that achieves the same effect but provides convenience (such as closure support). See this page

HTH. Best!

like image 133
German Avatar answered Dec 30 '22 10:12

German


From my own personal experience with Unity, you cannot create/run a separate thread unless the thread doesn't use any of Unity's api. So that means no gameObjects or things of similar nature.I've successfully done it myself for my own pathfinding so I know it is possible. Good Luck! I hope this helps.

like image 35
GDM Avatar answered Dec 30 '22 12:12

GDM