- Home /
Question by
Numes · Jul 14, 2015 at 10:26 AM ·
androidandroidpluginthreading
Android plugin in thread
Hi. I have problem with my game and I don't know how to resolve it.
In my app I need to use android plugin for read something from USB. Plugin works great, but data from USB can come in anytime, so my app need to still checking it.
Initially I tried do something like that:
IEnumerator readData(){
while(!stopThread)
{
data = androidObject.Call<string> ("read");
yield return new WaitForSeconds(0.1f);
}
}
It works, but fps drops from 60 to 5 (I expected that).
Next idea were threads. My code:
AndroidJavaClass _MyActivityClass,_ActivityClass;
AndroidJavaObject androidObject;
private Thread ThreadRead;
void Start () {
#if UNITY_ANDROID && !UNITY_EDITOR
_ActivityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
_MyActivityClass = new AndroidJavaClass("com.example.permissiontest.MainActivity");
androidObject = _ActivityClass.GetStatic<AndroidJavaObject>("currentActivity");
#endif
ThreadRead = new Thread (readData);
ThreadRead.Start ();
}
void readData(){
data = androidObject.Call<string> ("read");
}
That should start thread on the beggining and read once from android. But whenever i use androidObject.Call in thread, my app crashes. Do you have any other idea how to do that?
Comment
Answer by Numes · Jul 14, 2015 at 02:02 PM
Ok I found other solution.
In Android I created new thread with whole read stuff and whenever it read something i use:
UnityPlayer.UnitySendMessage("GameObjectName", "MethodName", "Message to send");
to send data to unity.
Your answer
