Unity main thread

224 views
Skip to first unread message

Tejas Gajera

unread,
Oct 19, 2020, 3:38:19 PM10/19/20
to StrangeIoC
Hello Experts!

I have question regarding how to execute code on unity main thread.

I have a C# library which I am using in my Unity project. This library has event handler which tells me which type of primitive game objects needs to be created. Once I receive  that event I fire a StrangeIoC signal which is attached to a command and runs execute method. Now, in this command execute method, I am creating a primitive object but it crashes because unity says you can only create game object on unity main thread.

I would like to know how can I access unity main thread in StrangeIoC command?     
 

wcorwin

unread,
Oct 19, 2020, 4:16:51 PM10/19/20
to StrangeIoC

This isn't really a strangeioc issue, this is how Unity works.

Typically management of main thread calls is done by some kind of main thread manager or coroutine runner. I'll attach this MB to my main/base ContextView and it will be responsible for scheduling all main thread activities. This includes things like  WebRequests. This is a very standard pattern across unity games, and it gets you pretty far with threaded background calls.

It's worth noting that all of strange functions just fine on other threads, but any restriction Unity applies to threads still apply. Which mostly means nothing on a GameObjects/MonoBehaviours, and therefore nothing related to Views/Mediation.

Tejas Gajera

unread,
Oct 19, 2020, 9:41:30 PM10/19/20
to StrangeIoC
Since I am new to Unity and StrangeIoC, I would appreciate if you could point me in the right direction by showing me an example or link where it's done at main/base ContextView.

Tejas Gajera

unread,
Nov 18, 2020, 10:55:16 AM11/18/20
to StrangeIoC
I was able to solve how to run code on the main unity thread. However, I have another question and would appreciate it if you could answer. 

First of all, this is what I have
1. I've game objects created already in my unity scene. 
2. I've real-time transform (position and orientation) data coming from a networking library for those game objects (>100 Hz).

What would be the best way to apply those transform (position and orientation) to the game objects?

I would think of two ways to do it
1. Unity update function (Store real-time transform data into Concurrent Queue and use them inside the update function) - Don't know how to approach this at the moment
2. Raise a signal which executes a command as soon as real-time data arrives and it will update game objects onto the main unity thread. - In this approach, I am updating transforms much faster than unity update function 60Hz.

If there is any other way to update the game object transform, I would be happy to know.

Reply all
Reply to author
Forward
0 new messages