Im making a networked app. Part of the situation is on changing avatar I have to instantiate the Avatar selected on both the local client (for the player) and remote clients (other players) so everyone sees the same avatar.
I cant pre instantiate and disable in advance, as playuers could end up using the same avatar if they want, which I cant forsee ahead of time.
This instantiate causes a 1sec lag on all clients. Now multiply this when any player changes avatar it is not playable.
What Im hoping to do is place the instantiae part in a CoRoutine so it happens separately from the main thread and not cause a lag.
Does this make sense to do? If so …what would that look like.
From reading up its a combination of yield, IEnumarator and coRoutine I think…but this is new to me.
The trouble is that the co-routine just pauses the main thread. Unity doesn’t do multithreading so easily (Unless things have changed in the past 2 months) and everything takes place in the same CPU thread.
The better question would be: Why does player instantiation cause such a long pause?
Unfortunately as mentioned I cant pool them as I have no idea before hand what avatars wil be chosen.
This is an online VR social app, so 20+ people willl have selection of many avatars (as I add their availability)
The instantiation is from the using the Morph3d Player. Must be a lot happening on instantiate.
I thought the whole purpose of Coroutines is that is DOESNT pause the main thread, but allows that thread to move on while the coroutine splits off on another thread?
EDIT: Reading more threads I see this is not the case.
I will try Corountine and yield, but I think it wont help the lag
It’s easy to confuse coroutines with threads, but coroutines are definitely not threads. The couroutine code executes in the main thread, as any other functions (Start, Update, FixedUpdate, LateUpdate, …).
The Unity API is not thread safe. So, if you want to do multi-threading, you can, but with the limitation that you can not use anything from Unity in other threads than main.
@pjenness_1
I don’t know about Thread Ninja and what it does exactly. But you can perfectly split a cpu intensive work over several frames to avoid lags. You would need to define how much work you could do per frame, the Profiler can help you with that.
Note that you can do this with or without coroutines.