总结的两种方法,供参考。
方法一
(要将方法写到UnityPlayerActivity 类下,不然调用不到):
1 //获得类 2 3 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 4 5 6 7 //获得java对象 8 9 AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); 10 11 12 13 //执行方法 14 15 jo.Call("Method",arg); 16 17
方法二:
1 //JNI 新建一个线程 2 3 AndroidJNI.AttachCurrentThread(); 4 5 6 7 //获得类 8 9 IntPtr cls = AndroidJNI.FindClass("com/unity3d/player/UnityPlayer"); 10 11 IntPtr fid = AndroidJNI.GetStaticFieldID(cls, "currentActivity", "Landroid/app/Activity;"); 12 13 IntPtr obj = AndroidJNI.GetStaticObjectField(cls, fid); 14 15 16 17 //获得类 18 19 IntPtr cls_OurAppActitvityClass = AndroidJNI.FindClass("com/bodhi/TalkingBodhi"); 20 21 22 23 //获得方法 24 25 IntPtr enableMethod = AndroidJNI.GetMethodID(cls_OurAppActitvityClass, "EnableADs", "V"); 26 27 28 29 //如果方法存在,则执行 30 31 if (AndroidJNI.IsInstanceOf(obj, cls_OurAppActitvityClass) != false) { 32 33 jvalue[] myArray = new jvalue[1]; 34 35 AndroidJNI.CallVoidMethod(obj, enableMethod, myArray); 36 37 }
时间: 2024-11-05 12:15:29