- Home /
Android plugins and callbacks on non-scripting threads
We have a plugin that we "wrapped" and call into using the standard AndroidJavaClass and AndroidJavaObject classes.
Some of the methods accept callbacks, and for that we use the AndoidJavaProxy class, that is passed to the plugin code and gets invoked automagically by Unity.
The issue is, the callback is raised on an arbitrary thread (may be the UI thread or some other worker thread) that is NOT Unity's scripting thread.
Is there any infrastructure for easily marshalling calls to the scripting thread (automatically) ?
Currently we have a scene object and we enqueue the callbacks on it. That object runs its Update() methods and raises the callbacks (on the scripting thread).
I am looking for a built-in and easy way of doing the same.
Answer by Yury-Habets · Dec 29, 2014 at 02:22 PM
This is a good question.
In fact, I think that your solution
Currently we have a scene object and we enqueue the callbacks on it. That object runs its Update() methods and raises the callbacks (on the scripting thread).
is a good way to go. We do similar things in our Java code to post events to the main thread. Adding built-in infrastructure is complex and does not provide control when you want the callbacks to be called (in your code you can do it in Update() or at any convenient point).
It would've been nice to have that baked in. I use the AndroidJavaProxy class, I believe it could be possible to have the wrapper that you create to support this class to callback through an internal object that will raise the callback on the scripting thread, ins$$anonymous$$d of creating this object by the developer.
Also, on Android there's already infrastructure for that (Looper) to allow posting messages between threads. The only problem is i don't think the scripting thread uses a looper so I cannot use this mechanism at all.
Right, but we are talking about C# and scripting, not really Android APIs, aren't we? :) I got your point, though cannot promise this gets released in the nearest future. Thanks!