Unity的重启(Android)与退出(IOS)

Unity的重启(Android)与退出(IOS)
传统的Application.Quit会有各种问题,甚至在某些平台不能够达到应有的效果。
疑难问题:
Android调用Application.Quit 后第二次启动会非常的慢,需要将原始进程杀掉
IOS ForceCrash,测试只有ForcedCrashCategory.FatalError,PureVirtualFunction 比较符合退出效果

    public static void RestartOrKillApp()
    {
        if(Application.isEditor) return;
        if (Application.platform == RuntimePlatform.Android)
        {
            using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
            {
                const int kIntent_FLAG_ACTIVITY_CLEAR_TASK = 0x00008000;
                const int kIntent_FLAG_ACTIVITY_NEW_TASK = 0x10000000;

                var currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
                var pm = currentActivity.Call<AndroidJavaObject>("getPackageManager");
                var intent = pm.Call<AndroidJavaObject>("getLaunchIntentForPackage", Application.identifier);

                intent.Call<AndroidJavaObject>("setFlags", kIntent_FLAG_ACTIVITY_NEW_TASK | kIntent_FLAG_ACTIVITY_CLEAR_TASK);
                currentActivity.Call("startActivity", intent);
                currentActivity.Call("finish");
                var process = new AndroidJavaClass("android.os.Process");
                int pid = process.CallStatic<int>("myPid");
                process.CallStatic("killProcess", pid);
            }

        }
        else if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            //测试只有下面俩种类型好用,FatalError几率卡界面
            UnityEngine.Diagnostics.Utils.ForceCrash(UnityEngine.Diagnostics.ForcedCrashCategory.FatalError);
            //UnityEngine.Diagnostics.Utils.ForceCrash(UnityEngine.Diagnostics.ForcedCrashCategory.PureVirtualFunction);
        }
        else
        {
            Application.Quit();
        }

    }

你可能感兴趣的:(Unity,android,unity,游戏引擎)