From 0026364d9aedebf71a9287ee7c2494c7d2477ff5 Mon Sep 17 00:00:00 2001 From: Cluster7ck Date: Wed, 24 Jun 2020 11:30:30 +0200 Subject: [PATCH] Application.quitting instead of MonoBehaviour.OnApplicationQuit `MonoBehaviour.OnApplicationQuit` is called before `Application.wantsToQuit` which leads to undesired behavior. `Application.wantsToQuit` can be used to interrupt a quit process, so you can show the user a window ("do you really want to quit?"). If you cancel the quit, but `MonoBehaviour.OnApplicationQuit` executes anyways, stuff breaks. I am aware this changes current behavior and is dealing with inconsistencies in the Unity API --- .../Scripts/UnityEngineBridge/MainThreadDispatcher.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Assets/Plugins/UniRx/Scripts/UnityEngineBridge/MainThreadDispatcher.cs b/Assets/Plugins/UniRx/Scripts/UnityEngineBridge/MainThreadDispatcher.cs index 91cc8c45..f6f51102 100644 --- a/Assets/Plugins/UniRx/Scripts/UnityEngineBridge/MainThreadDispatcher.cs +++ b/Assets/Plugins/UniRx/Scripts/UnityEngineBridge/MainThreadDispatcher.cs @@ -497,7 +497,9 @@ void Awake() updateMicroCoroutine = new MicroCoroutine(ex => unhandledExceptionCallback(ex)); fixedUpdateMicroCoroutine = new MicroCoroutine(ex => unhandledExceptionCallback(ex)); endOfFrameMicroCoroutine = new MicroCoroutine(ex => unhandledExceptionCallback(ex)); - + + Application.quitting += OnApplicationQuitting; + StartCoroutine(RunUpdateMicroCoroutine()); StartCoroutine(RunFixedUpdateMicroCoroutine()); StartCoroutine(RunEndOfFrameMicroCoroutine()); @@ -590,7 +592,7 @@ void OnDestroy() { instance = GameObject.FindObjectOfType(); initialized = instance != null; - + Application.quitting -= OnApplicationQuitting; /* // Although `this` still refers to a gameObject, it won't be found. var foundDispatcher = GameObject.FindObjectOfType(); @@ -669,7 +671,7 @@ public static IObservable OnApplicationPauseAsObservable() Subject onApplicationQuit; - void OnApplicationQuit() + void OnApplicationQuitting() { isQuitting = true; if (onApplicationQuit != null) onApplicationQuit.OnNext(Unit.Default); @@ -680,4 +682,4 @@ public static IObservable OnApplicationQuitAsObservable() return Instance.onApplicationQuit ?? (Instance.onApplicationQuit = new Subject()); } } -} \ No newline at end of file +}