更改统一播放器的屏幕方向时我的应用程序崩溃

My app crashes while changing screen orientation of unity player

我的 android 应用程序可以在点击时打开统一播放器。我的统一播放器表现符合预期。

问题: 在更改屏幕旋转时,应用程序崩溃。

调试控制台输出:

I/Unity: StopVuforia

         (Filename: ./Runtime/Export/Debug.bindings.h Line: 43)
D/AR: RenderManager: Could not retrieve a valid GLSurfaceView in view hierarchy, therefore cannot set any render mode
W/MessageQueue: Handler (android.os.Handler) {90dbee5} sending message to a Handler on a dead thread
                java.lang.IllegalStateException: Handler (android.os.Handler) {90dbee5} sending message to a Handler on a dead thread
                    at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543)
                    at android.os.Handler.enqueueMessage(Handler.java:643)
                    at android.os.Handler.sendMessageAtTime(Handler.java:612)
                    at android.os.Handler.sendMessageDelayed(Handler.java:582)
                    at android.os.Handler.sendMessage(Handler.java:519)
                    at android.os.Message.sendToTarget(Message.java:416)
                    at com.unity3d.player.UnityPlayer$e.a(Unknown Source)
                    at com.unity3d.player.UnityPlayer$e.b(Unknown Source)
                    at com.unity3d.player.UnityPlayer.b(Unknown Source)
                    at com.unity3d.player.UnityPlayer.a(Unknown Source)
                    at com.unity3d.player.UnityPlayer.a(Unknown Source)
                    at com.unity3d.player.UnityPlayer.surfaceDestroyed(Unknown Source)
                    at android.view.SurfaceView.updateWindow(SurfaceView.java:588)
                    at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:247)
                    at android.view.View.dispatchDetachedFromWindow(View.java:15567)
                    at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:4914)
                    at android.view.ViewGroup.removeAllViews(ViewGroup.java:4860)
                    at com.unity3d.player.UnityPlayer.quit(Unknown Source)
                    at com.Accenture.Activity.UnityPlayerActivity.onDestroy(UnityPlayerActivity.java:66)
                    at android.app.Activity.performDestroy(Activity.java:6889)
                    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1175)
                    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4231)
                    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4262)
                    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4536)
                    at android.app.ActivityThread.-wrap19(ActivityThread.java)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:154)
                    at android.app.ActivityThread.main(ActivityThread.java:6165)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
W/MessageQueue: Handler (android.os.Handler) {90dbee5} sending message to a Handler on a dead thread
                java.lang.IllegalStateException: Handler (android.os.Handler) {90dbee5} sending message to a Handler on a dead thread
                    at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543)
                    at android.os.Handler.enqueueMessage(Handler.java:643)
                    at android.os.Handler.sendMessageAtTime(Handler.java:612)
                    at android.os.Handler.sendMessageDelayed(Handler.java:582)
                    at android.os.Handler.sendMessage(Handler.java:519)
                    at android.os.Message.sendToTarget(Message.java:416)
                    at com.unity3d.player.UnityPlayer$e.b(Unknown Source)
                    at com.unity3d.player.UnityPlayer.b(Unknown Source)
                    at com.unity3d.player.UnityPlayer.a(Unknown Source)
                    at com.unity3d.player.UnityPlayer.a(Unknown Source)
                    at com.unity3d.player.UnityPlayer.surfaceDestroyed(Unknown Source)
                    at android.view.SurfaceView.updateWindow(SurfaceView.java:588)
                    at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:247)
                    at android.view.View.dispatchDetachedFromWindow(View.java:15567)
                    at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:4914)
                    at android.view.ViewGroup.removeAllViews(ViewGroup.java:4860)
                    at com.unity3d.player.UnityPlayer.quit(Unknown Source)
                    at com.Accenture.Activity.UnityPlayerActivity.onDestroy(UnityPlayerActivity.java:66)
                    at android.app.Activity.performDestroy(Activity.java:6889)
                    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1175)
                    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4231)
                    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4262)
                    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4536)
                    at android.app.ActivityThread.-wrap19(ActivityThread.java)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:154)
                    at android.app.ActivityThread.main(ActivityThread.java:6165)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
W/Unity: Timeout while trying detaching primary window.

AndroidManifest

<activity
            android:name=".UnityPlayerActivity"
            android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="landscape"
            android:theme="@style/UnityThemeSelector">
            <meta-data
                android:name="unityplayer.UnityActivity"
                android:value="true" />
        </activity>

解决方法:

Either "fixing of screen changing app crash" or "force landscape view" will make my resolution.

  1. 我试过将统一播放器设置更改为横向左。
  2. 我已经尝试从 android 清单文件中删除我的 orientation|layoutDirection|screenLayout
  3. 我试过添加 screenOrientation="landscape/user/nosensor/fulluser"

我在谷歌上搜索了很多关于更改方向时 Unity Player 崩溃的问题,但没有找到有用的解决方案。

因此,考虑到我的可行性,我将方向锁定为 "landscape" 模式。

为此,

第 1 步:Unity 播放器设置

Go to Edit > Project Settings > Player it will open Player Settings in the inspector.

Select the target platform from the list.

Click the Resolution and Presentation section.

Set the Default Orientation to Landscape left / right.

参考此博客:https://answers.unity.com/questions/774186/landscape-mode-only.html

第 2 步:Android 代码更改。

在清单文件中,

<activity
            android:name=".UnityPlayerActivity"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="landscape"
            android:theme="@style/UnityThemeSelector">
            <meta-data
                android:name="unityplayer.UnityActivity"
                android:value="true" />
</activity>

此外,还要以编程方式设置方向。

private int screenOrientation;
// lock screen orientation to landscape mode only.
screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
setRequestedOrientation(screenOrientation);