From 6f8bca27f4ee5245c6685642fbb114751c0e550b Mon Sep 17 00:00:00 2001 From: Natalie Bunduwongse Date: Wed, 14 Feb 2024 13:15:09 +1300 Subject: [PATCH] feat: ability to target android/ios and run the sdk in the unity mac editor --- sample/Assets/Scripts/AuthenticatedScript.cs | 2 +- sample/Assets/Scripts/UnauthenticatedScript.cs | 13 +++---------- .../Runtime/Scripts/Private/PassportImpl.cs | 4 ++-- .../Gree/Assets/Plugins/GreeBrowserClient.cs | 6 +++++- .../Gree/Assets/Plugins/WebViewObject.cs | 16 ++++++++-------- 5 files changed, 19 insertions(+), 22 deletions(-) diff --git a/sample/Assets/Scripts/AuthenticatedScript.cs b/sample/Assets/Scripts/AuthenticatedScript.cs index 69a31e5a..60d3d49c 100644 --- a/sample/Assets/Scripts/AuthenticatedScript.cs +++ b/sample/Assets/Scripts/AuthenticatedScript.cs @@ -180,7 +180,7 @@ public async void GetAddress() public async void Logout() { ShowOutput("Logging out..."); -#if UNITY_ANDROID || UNITY_IPHONE || (UNITY_STANDALONE_OSX && !UNITY_EDITOR_OSX) +#if (UNITY_ANDROID && !UNITY_EDITOR_WIN) || (UNITY_IPHONE && !UNITY_EDITOR_WIN) || UNITY_STANDALONE_OSX await passport.LogoutPKCE(); #else await passport.Logout(); diff --git a/sample/Assets/Scripts/UnauthenticatedScript.cs b/sample/Assets/Scripts/UnauthenticatedScript.cs index 13dc90d8..f77fb02f 100644 --- a/sample/Assets/Scripts/UnauthenticatedScript.cs +++ b/sample/Assets/Scripts/UnauthenticatedScript.cs @@ -34,8 +34,7 @@ async void Start() string redirectUri = null; string logoutRedirectUri = null; - // macOS editor (play scene) does not support deeplinking -#if UNITY_ANDROID || UNITY_IPHONE || (UNITY_STANDALONE_OSX && !UNITY_EDITOR_OSX) +#if (UNITY_ANDROID && !UNITY_EDITOR_WIN) || (UNITY_IPHONE && !UNITY_EDITOR_WIN) || UNITY_STANDALONE_OSX redirectUri = "imxsample://callback"; logoutRedirectUri = "imxsample://callback/logout"; #endif @@ -79,8 +78,7 @@ public async void Login() ShowOutput("Called Login()..."); LoginButton.gameObject.SetActive(false); - // macOS editor (play scene) does not support deeplinking -#if UNITY_ANDROID || UNITY_IPHONE || (UNITY_STANDALONE_OSX && !UNITY_EDITOR_OSX) +#if (UNITY_ANDROID && !UNITY_EDITOR_WIN) || (UNITY_IPHONE && !UNITY_EDITOR_WIN) || UNITY_STANDALONE_OSX await passport.LoginPKCE(); #else await passport.Login(); @@ -109,9 +107,7 @@ public async void Login() Debug.Log(error); ShowOutput(error); -#if UNITY_ANDROID || UNITY_IPHONE || UNITY_STANDALONE_OSX LoginButton.gameObject.SetActive(true); -#endif } } @@ -149,8 +145,7 @@ public async void Connect() ShowOutput("Called Connect()..."); ConnectButton.gameObject.SetActive(false); - // macOS editor (play scene) does not support deeplinking -#if UNITY_ANDROID || UNITY_IPHONE || (UNITY_STANDALONE_OSX && !UNITY_EDITOR_OSX) +#if (UNITY_ANDROID && !UNITY_EDITOR_WIN) || (UNITY_IPHONE && !UNITY_EDITOR_WIN) || UNITY_STANDALONE_OSX await passport.ConnectImxPKCE(); #else await passport.ConnectImx(); @@ -179,9 +174,7 @@ public async void Connect() Debug.Log(error); ShowOutput(error); -#if UNITY_ANDROID || UNITY_IPHONE || UNITY_STANDALONE_OSX ConnectButton.gameObject.SetActive(true); -#endif } } diff --git a/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs b/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs index 74c9412c..d3f8bced 100644 --- a/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs +++ b/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs @@ -370,7 +370,7 @@ private async UniTask LaunchAuthUrl() if (response != null && response.success == true && response.result != null) { string url = response.result.Replace(" ", "+"); -#if UNITY_ANDROID +#if UNITY_ANDROID && !UNITY_EDITOR loginPKCEUrl = url; SendAuthEvent(pkceLoginOnly ? PassportAuthEvent.LoginPKCELaunchingCustomTabs : PassportAuthEvent.ConnectImxPKCELaunchingCustomTabs); LaunchAndroidUrl(url); @@ -583,7 +583,7 @@ private async void LaunchLogoutPKCEUrl() { string logoutUrl = await GetLogoutUrl(); -#if UNITY_ANDROID +#if UNITY_ANDROID && !UNITY_EDITOR LaunchAndroidUrl(logoutUrl); #else communicationsManager.LaunchAuthURL(logoutUrl, logoutRedirectUri); diff --git a/src/Packages/Passport/Runtime/ThirdParty/Gree/Assets/Plugins/GreeBrowserClient.cs b/src/Packages/Passport/Runtime/ThirdParty/Gree/Assets/Plugins/GreeBrowserClient.cs index bf563dba..31f50347 100644 --- a/src/Packages/Passport/Runtime/ThirdParty/Gree/Assets/Plugins/GreeBrowserClient.cs +++ b/src/Packages/Passport/Runtime/ThirdParty/Gree/Assets/Plugins/GreeBrowserClient.cs @@ -17,6 +17,10 @@ public class GreeBrowserClient : IWebBrowserClient public GreeBrowserClient() { +#if (UNITY_ANDROID && UNITY_EDITOR_OSX) || (UNITY_IPHONE && UNITY_EDITOR_OSX) + Debug.LogWarning("Native Android and iOS WebViews cannot run in the Editor, so the macOS WebView is currently used to save your development time." + + " Testing your game on an actual device or emulator is recommended to ensure proper functionality."); +#endif webViewObject = new WebViewObject(); webViewObject.Init( cb: InvokeOnUnityPostMessage, @@ -25,7 +29,7 @@ public GreeBrowserClient() auth: InvokeOnAuthPostMessage, log: InvokeOnLogMessage ); -#if UNITY_ANDROID +#if UNITY_ANDROID && !UNITY_EDITOR string filePath = Constants.SCHEME_FILE + ANDROID_DATA_DIRECTORY + Constants.PASSPORT_DATA_DIRECTORY_NAME + Constants.PASSPORT_HTML_FILE_NAME; #elif UNITY_EDITOR_OSX string filePath = Constants.SCHEME_FILE + Path.GetFullPath(MAC_EDITOR_RESOURCES_DIRECTORY) + Constants.PASSPORT_HTML_FILE_NAME; diff --git a/src/Packages/Passport/Runtime/ThirdParty/Gree/Assets/Plugins/WebViewObject.cs b/src/Packages/Passport/Runtime/ThirdParty/Gree/Assets/Plugins/WebViewObject.cs index fe29123a..640570bb 100644 --- a/src/Packages/Passport/Runtime/ThirdParty/Gree/Assets/Plugins/WebViewObject.cs +++ b/src/Packages/Passport/Runtime/ThirdParty/Gree/Assets/Plugins/WebViewObject.cs @@ -74,7 +74,7 @@ public class WebViewObject ErrorCallback onHttpError; Callback onAuth; Callback onLog; -#if UNITY_ANDROID +#if UNITY_ANDROID && !UNITY_EDITOR class AndroidCallback : AndroidJavaProxy { private Action callback; @@ -94,7 +94,7 @@ public void call(String message) { IntPtr webView; #endif -#if UNITY_IPHONE +#if UNITY_IPHONE && !UNITY_EDITOR [DllImport("__Internal")] private static extern IntPtr _CWebViewPlugin_Init(string ua); [DllImport("__Internal")] @@ -113,7 +113,7 @@ private static extern void _CWebViewPlugin_EvaluateJS( private static extern void _CWebViewPlugin_ClearCache(IntPtr instance, bool includeDiskFiles); [DllImport("__Internal")] private static extern void _CWebViewPlugin_ClearStorage(IntPtr instance); -#elif UNITY_STANDALONE_OSX +#elif UNITY_STANDALONE_OSX || (UNITY_ANDROID && UNITY_EDITOR_OSX) || (UNITY_IPHONE && UNITY_EDITOR_OSX) [DllImport("WebView")] private static extern IntPtr _CWebViewPlugin_Init(string ua); [DllImport("WebView")] @@ -228,7 +228,7 @@ public void Init( #elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX //TODO: UNSUPPORTED Debug.LogError("Webview is not supported on this platform."); -#elif UNITY_IPHONE || UNITY_STANDALONE_OSX +#elif UNITY_IPHONE || UNITY_STANDALONE_OSX || (UNITY_ANDROID && UNITY_EDITOR_OSX) webView = _CWebViewPlugin_Init(ua); Singleton.Instance.onJS = ((message) => CallFromJS(message)); Singleton.Instance.onError = ((id, message) => CallOnError(id, message)); @@ -257,7 +257,7 @@ public void LoadURL(string url) Application.ExternalCall("unityWebView.loadURL", url); #elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX //TODO: UNSUPPORTED -#elif UNITY_STANDALONE_OSX || UNITY_IPHONE +#elif UNITY_STANDALONE_OSX || UNITY_IPHONE || (UNITY_ANDROID && UNITY_EDITOR_OSX) if (webView == IntPtr.Zero) return; _CWebViewPlugin_LoadURL(webView, url); @@ -278,7 +278,7 @@ public void EvaluateJS(string js) Application.ExternalCall("unityWebView.evaluateJS", js); #elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX //TODO: UNSUPPORTED -#elif UNITY_STANDALONE_OSX || UNITY_IPHONE +#elif UNITY_STANDALONE_OSX || UNITY_IPHONE || (UNITY_ANDROID && UNITY_EDITOR_OSX) if (webView == IntPtr.Zero) return; _CWebViewPlugin_EvaluateJS(webView, js); @@ -291,7 +291,7 @@ public void EvaluateJS(string js) public void LaunchAuthURL(string url, string redirectUri) { -#if UNITY_STANDALONE_OSX +#if UNITY_STANDALONE_OSX || (UNITY_ANDROID && UNITY_EDITOR_OSX) || (UNITY_IPHONE && UNITY_EDITOR_OSX) if (webView == IntPtr.Zero) return; _CWebViewPlugin_LaunchAuthURL(webView, url, redirectUri != null ? redirectUri : ""); @@ -332,7 +332,7 @@ public void CallFromJS(string message) { if (onJS != null) { -#if !UNITY_ANDROID +#if !(UNITY_ANDROID && !UNITY_EDITOR) #if UNITY_2018_4_OR_NEWER message = UnityWebRequest.UnEscapeURL(message.Replace("+", "%2B")); #else // UNITY_2018_4_OR_NEWER