0
\$\begingroup\$

As one cannot play around with threads themselves, the user-made code that uses unity's API is executed on the main thread. I'm sure unity uses some sort of thread management on its own though.

Thing is, I wrote some basic UI animation toolkit (rotate, scale, lerp alpha, move and tie events etc) that - as being user code - runs on the main thread, taking up CPU power, even if they are not vital to be completely synchronized with the logic. (Event connected to them only start other animations).

I would like to know if Unity uses another thread for animations as well and if so, is it possible to mark this UI toolkit to be executed on that thread or - as I said - user code with unity api can be only ever executed on the main thread?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

So, you actually can use threads. You're just limited to what Unity features you can call on threads that aren't main. If you simply want to do math, and are using data that isn't locked to integral Unity classes (structs, Game objects, etc), then thread away.

A threaded approach in unity often requires a bit of work to decouple your data from Unity and then find a way to re-inject it when applicable. When I have to do such things, I often use coroutines that act as thread watchers allowing Unity to utilize threads for both asynchronous and parallel applications.

an example of how do decouple something like a transform:

//this contains whatever transform data your threads need to modify.
//Initialized at start / GO instantiation
struct xform{
   Vector3 pos;
   Quaternion rotation;
}

//Allows you to move data from worker thread to main thread.
IEnumerator xformUpdate(){
    while(runningCondition){
       //only grabs data if it's available
       if(readyForUpdate()){
         //transfers data from xform to Transform
         xformToTransform(args);
       } else {
         yield return null;
       }
    }
}

Obviously you'll need to implement your own systems to check if calculations are complete / ensure you don't run into data races.

The other big blocker is that your ability to use threads is very Unity version dependent. Unity 2017 (with experimental mono enabled) has access to tasking and parallel libraries that Unity 5.x does not. Both still have threading options available, but it's definitely worth looking into when developing your solution.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks for the input and yes you are right, though in my case I use very simple coroutines - just a couple of them. I don't really do math except for a negation or addition, but I do set a lot of values (SpriteRenderer sr.sprite = smth) and the like. \$\endgroup\$
    – agiro
    Commented Aug 7, 2017 at 18:16
  • \$\begingroup\$ This is what I meant about decoupling your data. If you can create a threadsafe struct that can store all that data. You can do your calculations in parallel and then do synced SpriteRenderer updates \$\endgroup\$
    – Zebraman
    Commented Aug 7, 2017 at 18:18
  • \$\begingroup\$ Not a bad idea, I will look into it. \$\endgroup\$
    – agiro
    Commented Aug 7, 2017 at 18:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .