Solution to Flash Back of Unity Multi thread Calling Android Method
![Solution to Flash Back of Unity Multi thread Calling Android Method Solution to Flash Back of Unity Multi thread Calling Android Method](http://up-free-imgs.azimiao.com/wp-content/uploads/2017/10/imageUnityOk02.jpg)
Problem recurrence
1. Synchronous call (normal)
void Start() { androidObj = new AndroidJavaObject("com.azimiao.test.class1"); } void CallAndroidFunc(params object[] test) { androidObj?.Call("TestFunc",test); }
2. Asynchronous thread call (flashback, error call method)
void CallAndroidFunc(params object[] test) { testThread = new Thread(() => { androidObj?.Call("TestFunc", test); }); testThread.Start(); }
solve the problem
-
Attach the current thread to the Java (Dalvik) VM. -
A thread must be attached to the VM before any other JNI calls can be made.
void Start() { androidObj = new AndroidJavaObject("com.azimiao.test.class1"); } void CallAndroidFunc(params object[] test) { testThread = new Thread(() => { AndroidJNI.AttachCurrentThread(); ndroidObj?.Call("TestFunc", test); }); testThread.Start(); }