以下の記載は古くなっていて、現在はApplication.Quit()が使えます。後述します。2022-09-07
Unityのアプリケーションではプログラム終了の際、Application.Quit()を呼び出すのだが、これがOculus Quest(Quest 2も)ではプログラムは終了するが、次回の起動に失敗するという問題がある。結論はApplication.Quit()の代わりに、以下を呼び出せばよい。
System.Diagnostics.Process.GetCurrentProcess().Kill();
Channel Playerでは、2回に1回は起動できないという不具合があり、メモリーリークなどを疑い、場所を特定するために、機能をどんどんはずしていて、ついにシーン1個で、Start()でただApplication.Quit()するだけでも事象が発生することが判明した。そこで削るのではなく新たにプロジェクトを作り、デフォルトのシーンでは事象が発生しないことが判明。ただしこのプロジェクトではソースコードが無いので自力ではアプリケーション終了できず、システムメニューから終了させていた。そこでスクリプトを1個置いて、Application.Quit()を記述すると事象が発生し、ここに問題があることが特定できた。
それまでは、debug logの以下のようなログから、Consumer closed input channel or an error occurred. events=0x9 でネットを検索しても原因は特定できずに困っていたが
11-03 09:55:29.617: W/InputDispatcher(1058): channel '8c14a4d com.Applet_LLC.Channel_Player/com.unity3d.player.UnityPlayerActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9
11-03 09:55:29.617: E/InputDispatcher(1058): channel '8c14a4d com.Applet_LLC.Channel_Player/com.unity3d.player.UnityPlayerActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
11-03 09:55:29.618: I/WindowManager(1058): WIN DEATH: Window{8c14a4d u0 com.Applet_LLC.Channel_Player/com.unity3d.player.UnityPlayerActivity}
11-03 09:55:29.618: W/InputDispatcher(1058): Attempted to unregister already unregistered input channel '8c14a4d com.Applet_LLC.Channel_Player/com.unity3d.player.UnityPlayerActivity (server)'
Application.Quit Oculusでネット検索すると、すぐに以下の記述がヒットした。AndroidのApplication.Quit()ではきちんと終了できていないそうだ。
Application.Quit() causes crashing on next app launch — Oculus https://forums.oculusvr.com/developer/discussion/81042/application-quit-causes-crashing-on-next-app-launch
以下追記(2022-09-07)。
Unityで作ったAndroidアプリがBackキー押下で終了できない場合の仮対応 – Qiita https://qiita.com/ganessa/items/3d80b9065546788c30d4
によると System.Diagnostics.Process.GetCurrentProcess().Kill();でも良いが、以下で安定するとのこと。
Application.runInBackground = false;
Application.Quit();
return;
綺麗に終了するようになったが、2回目起動しない問題再発。
Application.Quit(); したあとで、System.Diagnostics.Process.GetCurrentProcess().Kill();を呼ぶようにしてみるが、これではだめだった。マルチスレッドで動いていて、親プロセスだけ終了させてもダメらしい。
https://issuetracker.unity3d.com/issues/android-application-dot-quit-and-application-dot-unload-crashes-the-app-on-android-9-dot-x-devices-when-scripting-backend-is-set-to-il2cpp
上記によれば、2018.3.8f1では良いらしい。2018.4.6でも直したようなので、これにしてみたら
Unity2018.4.6f1では、Application.Quit&()で正常終了することを確認した。
結論は、本コラム記載当初とは逆となり、System.Diagnostics.Process.GetCurrentProcess().Kill();は、Arm64向けでは失敗する。