背景:采用Google VR SDK for Unity 开发3D场景功能,然后导出Android项目,合并到一个Android App里面,供其它Activity调用。
- 用Google VR for Unity SDK开发的Untiy项目导出来的Android项目,主Activity为com.google.unity.GoogleUnityActivity。如果需要在此基础上扩展一些功能,则需要实现自己的Activity(比如命名为GoogleCardboardActivity),并继承com.google.unity.GoogleUnityActivity,并把AndroidManifest.xml里面的主Activity修改为自己实现的Activity(比如刚才创建的GoogleCardboardActivity)。
- 如果把主Activity替换成UnityPlayerActivity,则调用Untiy场景时,出来的内容看不到Cardboard的UI(返回按钮,中间的分割线,以及设置按钮)。
- 从Android Activity调用GoogleUnityActivity的代码如下:
1 Intent googleUnityIntent = new Intent(this, GoogleUnityActivity.class); 2 AndroidActivity.this.startActivity(googleUnityIntent); 3 AndroidActivity.this.finish();
如果是自定义的Activity,则将GoogleUnityActivity.class替换成自定义的Activity.class。
- 在Unity场景中调用GoogleUnityActivity定义的方法:
1 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 2 AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); 3 jo.Call("someAndroidMethod");
someAndroidMethod为写在GoogleUnityActivity中的Android方法。
-
如果该Android方法中需要调用到mUnityPlayer,则需要把调用mUnityPlayer的代码写在Android的UI线程里面,否则会出错:
1 public void someAndroidMethod() { 2 runOnUiThread(new Runnable() { 3 @Override 4 public void run() { 5 mUnityPlayer.quit(); 6 } 7 }); 8 }
- couldn't find "libgvrunity.so"
AndroidRuntime: FATAL EXCEPTION: main Process: com.elfnaga.googlecardboard, PID: 6401 java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.elfnaga.googlecardboard-1/base.apk"],nativeLibraryDirectories=[/data/app/com.elfnaga.googlecardboard-1/lib/arm64, /data/app/com.elfnaga.googlecardboard-1/base.apk!/lib/arm64-v8a, /vendor/lib64, /system/lib64]]] couldn't find "libgvrunity.so" at java.lang.Runtime.loadLibrary(Runtime.java:367) at java.lang.System.loadLibrary(System.java:1076) at com.google.unity.GoogleUnityActivity.<clinit>(GoogleUnityActivity.java:55) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1068) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2374) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535) at android.app.ActivityThread.access$1100(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5582) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 12-01 10:20:38.089 13512-14679/? W/ActivityManager: Force finishing activity com.elfnaga.googlecardboard/com.google.unity.GoogleUnityActivity
原因是Google VR SDK for Unity中的libgvrunity.so只提供了armv7版和x86版,没有提供arm64版本,所以需要修改Android项目中的build.gradle,添加产品偏好:
1 android { 2 productFlavors { 3 armv7 { 4 ndk { 5 abiFilter "armeabi-v7a" 6 } 7 } 8 x86 { 9 ndk { 10 abiFilter "x86" 11 } 12 } 13 fat 14 } 15 }
则不会出现找不到libgvrunity.so。