Hello,
I’ve noticed an issue: when threading a native code function the Unity Android app is crashing immediately (I get an Android “Unfortunately myapp has stopped.” message box).
I’ve reproduced it in a very simple way, starting from the libnative.so and AndroidNativePlugin.unitypackage official example available here.
Here is my code:
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System.Threading;
public class CallNativeCode : MonoBehaviour {
[DllImport("native")]
private static extern float add(float x, float y);
private static float x,y,res;
void Start () {
x = 3;
y = 10;
res = -1;
// Threading an extern function crashes
Thread t = new Thread(new ThreadStart(NativeAdd));
t.Start();
// Direct call to extern function below works
//NativeAdd();
}
void NativeAdd () {
res = add(x,y);
// Regular expression below works when threaded
//res = x + y;
}
void OnGUI ()
{
GUI.Label (new Rect (15, 125, 450, 100), "adding " + x + " and " + y + " in native code equals " + res);
}
}
The C function is rather simple:
float add(float x, float y)
{
return x + y;
}
Any idea would be greatly appreciated !