From 8a2c2db32314659e921ba59f54485903528d55be Mon Sep 17 00:00:00 2001 From: Natalie Bunduwongse Date: Thu, 8 Aug 2024 08:30:43 +1200 Subject: [PATCH] feat: support domain reloading --- .../Runtime/Scripts/Public/Passport.cs | 96 +++++++++++++++---- 1 file changed, 75 insertions(+), 21 deletions(-) diff --git a/src/Packages/Passport/Runtime/Scripts/Public/Passport.cs b/src/Packages/Passport/Runtime/Scripts/Public/Passport.cs index 15f7713c..9d825ac7 100644 --- a/src/Packages/Passport/Runtime/Scripts/Public/Passport.cs +++ b/src/Packages/Passport/Runtime/Scripts/Public/Passport.cs @@ -13,6 +13,9 @@ using Immutable.Passport.Core; using Cysharp.Threading.Tasks; using UnityEngine; +#if UNITY_EDITOR +using UnityEditor; +#endif namespace Immutable.Passport { @@ -52,6 +55,10 @@ private Passport() OnDeepLinkActivated(Application.absoluteURL); } #endif + +#if UNITY_EDITOR + EditorApplication.playModeStateChanged += OnPlayModeStateChanged; +#endif } /// @@ -65,18 +72,13 @@ private Passport() /// (Windows only) Timeout duration in milliseconds to wait for the default Windows browser engine to start. /// (Windows only) Custom Windows browser to use instead of the default browser in the SDK. public static UniTask Init( -#if UNITY_STANDALONE_WIN || (UNITY_ANDROID && UNITY_EDITOR_WIN) || (UNITY_IPHONE && UNITY_EDITOR_WIN) - string clientId, - string environment, - string redirectUri = null, - string logoutRedirectUri = null, - int engineStartupTimeoutMs = 30000, - IWindowsWebBrowserClient windowsWebBrowserClient = null -#else string clientId, string environment, string redirectUri = null, string logoutRedirectUri = null +#if UNITY_STANDALONE_WIN || (UNITY_ANDROID && UNITY_EDITOR_WIN) || (UNITY_IPHONE && UNITY_EDITOR_WIN) + , int engineStartupTimeoutMs = 30000, + IWindowsWebBrowserClient windowsWebBrowserClient = null #endif ) { @@ -178,19 +180,6 @@ private async UniTask Initialise( } } - -#if UNITY_STANDALONE_WIN || (UNITY_ANDROID && UNITY_EDITOR_WIN) || (UNITY_IPHONE && UNITY_EDITOR_WIN) - /// - /// Handles clean-up when the application quits. - /// - private void OnQuit() - { - Debug.Log($"{TAG} Quitting the Player"); - webBrowserClient.Dispose(); - Instance = null; - } -#endif - /// /// Sets the timeout time for waiting for each call to respond (in milliseconds). /// This only applies to functions that use the browser communications manager. @@ -502,5 +491,70 @@ private void OnPassportAuthEvent(PassportAuthEvent authEvent) OnAuthEvent.Invoke(authEvent); } } + + /// + /// Handles clean-up when the application quits + /// + private void OnQuit() + { + Debug.Log($"{TAG} Quitting the Player"); + +#if UNITY_EDITOR + EditorApplication.playModeStateChanged -= OnPlayModeStateChanged; +#endif + + DisposeAll(); + } + +#if UNITY_EDITOR + /// + /// Handles play mode state changes in the editor + /// + /// The current play mode state + private void OnPlayModeStateChanged(PlayModeStateChange state) + { + // Dispose of all resources when exiting play mode + if (state == PlayModeStateChange.ExitingPlayMode) + { + DisposeAll(); + } + } +#endif + + /// + /// Disposes of all resources and unsubscribes from events + /// + private void DisposeAll() + { + // Dispose of the web browser client for Windows only +#if UNITY_STANDALONE_WIN || (UNITY_ANDROID && UNITY_EDITOR_WIN) || (UNITY_IPHONE && UNITY_EDITOR_WIN) + if (webBrowserClient != null) + { + webBrowserClient.Dispose(); + webBrowserClient = null; + } +#endif + + // Unsubscribe from Passport authentication events + // and dispose of the Passport implementation + if (passportImpl != null) + { + passportImpl.OnAuthEvent -= OnPassportAuthEvent; + passportImpl = null; + } + + // Unsubscribe from application quitting event + Application.quitting -= OnQuit; + +#if UNITY_IPHONE || UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX + // Unsubscribe from deep link activation events on iOS and macOS + Application.deepLinkActivated -= OnDeepLinkActivated; +#endif + + // Reset static fields + Instance = null; + deeplink = null; + readySignalReceived = false; + } } }