public class TestArrayThroughJNI
{
private static AndroidJavaObject _plugin;
static GPlayIABPlugin()
{
if( Application.platform != RuntimePlatform.Android )
return;
//This is apparently good practice, but seems to have no effect anyways!
AndroidJNI.AttachCurrentThread();
// find the plugin instance
using (var pluginClass = new AndroidJavaClass("com.company.myplugin"))
_plugin = pluginClass.CallStatic<AndroidJavaObject>( "instance" );
}
public static void PassArrayToJava()
{
float[] testFltArray = {1.5f, 2.5f, 3.5f, 3.6f};
IntPtr jAryPtr = AndroidJNIHelper.ConvertToJNIArray(testFltArray);
jvalue[] blah = new jvalue[1];
blah[0].l = jAryPtr;
IntPtr methodId = AndroidJNIHelper.GetMethodID(_plugin.GetRawClass(), "testArrayFunction");
AndroidJNI.CallVoidMethod(_plugin.GetRawObject(), methodId, blah);
}
}
Android端代码:
publicclassJavaTestArray{//Used by Unity to grab the instance of this Java class.privatestatic IABUnityPlugin _instance;//Magic happens here.publicvoidtestArrayFunction(float[] test){
Log.i("Unity","Float[]: "+ test +" and length: "+ test.length);for(int i =0; i < test.length; i++){
Log.i("Unity","["+ i +"] "+ test[i]);}}publicstatic JavaTestArray instance(){if(_instance == null)
_instance =newJavaTestArray();return _instance;}}