- Home /
How do I run a method in another thread?
I have a method that is pretty resource intensive and I'd have to run it on another thread. The method should be able to access variables and other methods from other classes and modify them. I have taken look at C# tasks, UnityTasks and the Job System, but I'm not sure which one is best here. Either way, I haven't been able to get any of the above options to working correctly yet. I probably need a simple example on how to do this correctly. I did use tasks in a C# project some time ago, but apparently they don't work in Unity.
Answer by Bunny83 · Dec 15, 2018 at 12:31 PM
The method should be able to access variables and other methods from other classes and modify them
That's "simply" not possible. Just running code on a seperate thread is quite easy. However you have to take care about thread safety. If two or more threads may access / modify the same variables you have to make sure only one thread can access them at the same time. That either involves implementing locking or other means to ensure thread safety (via flags, wait handles). Another option is to duplicate the necessary data for the thread and join the data once the thread has completed its task.
Keep in mind that most parts of the Unity API is not thread safe and can not be used in any other thread than the main thread. This may involve more than you think. For example the position property of the transform class can not be read or written to in another thread at all. All common unity methods (GetComponent, .transform, .gameObject, GameObject.Find, ....) can not be used in a thread.
Since you asked a very abstracted question the answer is simple: There is no simple solution which you can apply to any problem you may come across.
Threading is a huge topic on its own which is not specific to Unity. You should have a well understanding of the threading basics. The underlying problems with threads are never methods but data. You must ensure that you don't create race conditions between two threads. Unity has implemented a thread check in almost all of their API methods which actively generates an error if called from a different thread than the main thread. So when you want to use Unity API methods in a thread, locking and synchronising is not enough.
The new Job system and Entity component system is quite powerful but may require you to restructure how your project works. So your question "which one is best here" can't be answered without knowing the actual problem you want to solve, which methods you want to use and which data is involved. Your question is a bit like asking for a simple complete example how to build a nuclear power plant so you can build one in your basement. This is just impossible unless you already have a great understanding of the subject which you seem lacking.
You may want to read through those questions:
The method is a sound engine that generates sound real time according to the data in a file. The part I'm trying to test at the moment reads the file and does other calculations for data to save in variables that the actual sound generator uses to produce the sound. If I can run the entire thing on another thread I'd only have to pass a variable to enable/disable it and return data to an audio output, which might be on the main thread. The code itself is not that heavy, it just needs to run many times a frame which adds up when you try to run it on the main thread with everything unity already has to do. The engine is multiple scripts that are on a single gameobject that is loaded to a scene
Your answer
