- Home /
java android plugin worker thread ends up in Unity mainthread
I am using a java android plugin (AndroidJavaClass) to access some native stuff on a tablet. This plugin creates a new worker Thread which collects some data and sends it back(UnityPlayer.UnitySendMessage() ) to my c# scripts in Unity. Now that all works great except for one thing. Aldo the java method
UnityPlayer.UnitySendMessage("someGameObject", "SomeMethodThatReceivesAndStoresThatdata", actualdata)
is called from a worker thread it ends up on a unity main thread(id=1).
Method in unity:
List<int> dataStorage = new List<int>();
public void SomeMethodThatReceivesAndStoresThatdata(int x)
{
dataStorage.Add(x);
}
It seems like the data in List dataStorage is refreshed only every frame not every java worker thread iterration. Now my concern is that this might cause some data loss if the framerate drops considerably.
So to my question:
Are UnityPlayer.UnitySendMessage() calls all stacked in a que and await execution in the next frame?? Or maybe just the last one is actually executed??(testing sugests that is not the case)?? Is there a way to execute UnityPlayer.UnitySendMessage() calls in a unity worker thread. (not as a corutine or some virtual way, but actually a seperate process that runs on another core)??
thanxs
Answer by ICE-jdeacutis · Oct 27, 2021 at 10:16 PM
UnityPlayer.UnitySendMessage calls likely can't be processed until the next frame.