如何从Unity&发送值用Java接收我的插件?

时间:2012-05-16 07:45:54

标签: android unity3d

我想将Unity中的数据发送到java for android插件。

这是我的Unity代码:

AndroidJNI.AttachCurrentThread();
IntPtr cls_Activity = AndroidJNI.FindClass("com/unity3d/player/UnityPlayer");
    IntPtr fid_Activity = AndroidJNI.GetStaticFieldID(cls_Activity, "currentActivity", "Landroid/app/Activity;");
    IntPtr obj_Activity = AndroidJNI.GetStaticObjectField(cls_Activity, fid_Activity);                  
    Debug.Log("Setup Objects Complete");        

    IntPtr cls_OurAppNameActivityClass = AndroidJNI.FindClass("org/plugin/test/AdmobTestActivity"); 

    IntPtr startAdsMethod = AndroidJNI.GetMethodID(cls_OurAppNameActivityClass, "EnableAds", "(J)V");
    Debug.Log("before If Loop");        

    if (AndroidJNI.IsInstanceOf(obj_Activity, cls_OurAppNameActivityClass) != false)
    {
        //Debug.Log("Activity IS a OurAppNameActivity");
        jvalue[] myArray = new jvalue[1];
            myArray[0] = new jvalue();
            myArray[0].i = 12;
            AndroidJNI.CallVoidMethod(obj_Activity, startAdsMethod, myArray);                    

            Debug.Log("Activity Leaving a OurAppNameActivity");
    }

这是我的Java代码:

public void EnableAds(int qwe)
{       
    Log.i(APP_TAG, "Value" + qwe);
}

错误:

05-16 13:11:30.906: W/dalvikvm(12315): threadid=8: thread exiting with uncaught exception (group=0x40018578)
05-16 13:11:30.921: E/AndroidRuntime(12315): FATAL EXCEPTION: GLThread 9
05-16 13:11:30.921: E/AndroidRuntime(12315): java.lang.NoSuchMethodError: EnableAds
05-16 13:11:30.921: E/AndroidRuntime(12315):    at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
05-16 13:11:30.921: E/AndroidRuntime(12315):    at com.unity3d.player.UnityPlayer.onDrawFrame(Unknown Source)
05-16 13:11:30.921: E/AndroidRuntime(12315):    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1368)
05-16 13:11:30.921: E/AndroidRuntime(12315):    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1123)

AND FORCE CLOSE ..

如果我在java代码中从EnableAdv(int qwe)中删除了int参数,那么这个应用程序工作正常。

任何人都可以告诉我,我在这段代码中遗漏了什么。?

1 个答案:

答案 0 :(得分:1)

我找到了答案..

我想用“(J)V”作为我的论据类型......但是我不喜欢使用“(I)V”。

例如,Java方法:

long f(int n,String s,int [] arr); 具有以下类型签名:

“(ILjava /郎/字符串; [I)V

请参阅this进一步阅读