关掉游戏,然后再重新启动。不是重新加载入口场景

	public void RestartAndroid()
  {
    if (Application.isEditor) return;

    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);
    }
  }

Sample工程:https://download.csdn.net/download/qq_33205561/80270378

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐