Unity Android平台下返回主页面会杀掉App

Unity Android平台下返回主页面会杀掉App

说明:
Unity运行在Android平台时会创建一个“UnityPlayerActivity” Activity。Unity游戏中所有的显示均活跃在此Activity下

步骤 1

在Unity中导出Android Studio工程
在这里插入图片描述

步骤2

将导出的Android Studio工程通过 File --> New --> import Project 导入Android Studio
找到 manifests/AndroidManifes.xml
做如下修改:

修改前:
<activity android:label="游戏名显示" android:screenOrientation="landscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection" android:hardwareAccelerated="false" android:name="com.sniper.of.kill.gun.shoot.ops3d.UnityPlayerActivity">
    </activity>
    <activity android:label="游戏名显示" android:screenOrientation="landscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection" android:hardwareAccelerated="false" android:name="com.sniper.of.kill.gun.shoot.ops3d.UnityPlayerNativeActivity">
修改后:
<activity android:label="游戏名显示" android:screenOrientation="landscape" android:launchMode="standard" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection" android:hardwareAccelerated="false" android:name="com.sniper.of.kill.gun.shoot.ops3d.UnityPlayerActivity">
    </activity>
    <activity android:label="游戏名显示" android:screenOrientation="landscape" android:launchMode="standard" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection" android:hardwareAccelerated="false" android:name="com.sniper.of.kill.gun.shoot.ops3d.UnityPlayerNativeActivity">
修改差异

所有 “游戏名显示” 所属的 activity 标签里的 launchMode=“singleTask” 均改为 launchMode=“standard”

在Android 中会将所有activity添加到一个队列。

    singleTask模式下Unity 游戏回到主页时会进入Android onPause、onStop。从主页恢复时检测到activity队列中已经存在一个UnityPlayerActivity,所以会调用onDestroy先将上一个activity进行销毁。此后进入onCreate重新打开App。
    standard模式下从主页回到游戏App不会将activity队列中的UnityPlayerActivity进行销毁,此时直接执行onResume返回到上次离开的页面位置。
有兴趣的话可以看下Android生命周期和launchMode相关。如有帮助记得点赞加关注哦。

猜你喜欢

转载自blog.csdn.net/X_King_Q/article/details/119040976