using System; using System.Runtime.InteropServices; using Cysharp.Threading.Tasks; using GUPS.Obfuscator.Attribute; using UnityEngine; [DoNotObfuscateClass] [DoNotObfuscateMethodBody] internal static class NativeDialog { private static Action pendingCallback; #if UNITY_IOS && !UNITY_EDITOR private delegate void DialogCallback(int result); private static readonly DialogCallback Callback = OnIOSResult; [DllImport("__Internal")] private static extern void AppRuntimes_ShowDialog(string message, DialogCallback callback); [AOT.MonoPInvokeCallback(typeof(DialogCallback))] private static void OnIOSResult(int result) => Complete(result != 0); #elif UNITY_ANDROID && !UNITY_EDITOR // Retain proxies until the dialog has finished calling back. private static ClickListener retryListener, cancelListener; private static CancelListener dismissListener; [DoNotObfuscateClass] private sealed class ClickListener : AndroidJavaProxy { private readonly bool result; public ClickListener(bool result) : base("android.content.DialogInterface$OnClickListener") => this.result = result; [UnityEngine.Scripting.Preserve] public void onClick(AndroidJavaObject dialog, int which) => Complete(result); } [DoNotObfuscateClass] private sealed class CancelListener : AndroidJavaProxy { public CancelListener() : base("android.content.DialogInterface$OnCancelListener") { } [UnityEngine.Scripting.Preserve] public void onCancel(AndroidJavaObject dialog) => Complete(false); } #endif internal static void ShowDialog(string message, Action callback) { if (pendingCallback != null) throw new InvalidOperationException("A native dialog is already open."); pendingCallback = callback; try { #if UNITY_EDITOR Complete(UnityEditor.EditorUtility.DisplayDialog("Network Error", message, "Retry", "Cancel")); #elif UNITY_IOS AppRuntimes_ShowDialog(message, Callback); #elif UNITY_ANDROID using (var player = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { var activity = player.GetStatic("currentActivity"); activity.Call("runOnUiThread", new AndroidJavaRunnable(() => { try { using (var builder = new AndroidJavaObject("android.app.AlertDialog$Builder", activity)) { retryListener = new ClickListener(true); cancelListener = new ClickListener(false); dismissListener = new CancelListener(); builder.Call("setTitle", "Network Error").Dispose(); builder.Call("setMessage", message).Dispose(); builder.Call("setPositiveButton", "Retry", retryListener).Dispose(); builder.Call("setNegativeButton", "Cancel", cancelListener).Dispose(); builder.Call("setOnCancelListener", dismissListener).Dispose(); builder.Call("show").Dispose(); } } catch (Exception exception) { Debug.LogException(exception); Complete(false); } finally { activity.Dispose(); } })); } #else Debug.LogWarning("Native network dialogs are supported on iOS, Android and in the Editor."); Complete(false); #endif } catch (Exception exception) { Debug.LogException(exception); Complete(false); } } private static void Complete(bool result) { // Android UI callbacks and iOS callbacks must resume Unity work on its player loop. UniTask.Post(() => { var callback = pendingCallback; pendingCallback = null; callback?.Invoke(result); }); } }