using System; using DialogCallback = NativeApi.DialogCallback; using System.Threading; using Cysharp.Threading.Tasks; using UnityEngine; public static class NativeDialog { public const string NetworkErrorMessage = "There is a problem with the network. Please try again later."; private static Action pendingCallback; #if UNITY_IOS && !UNITY_EDITOR private static readonly DialogCallback Callback = OnIOSResult; [AOT.MonoPInvokeCallback(typeof(DialogCallback))] private static void OnIOSResult(int result) => Complete(result != 0); #elif UNITY_ANDROID && !UNITY_EDITOR private static ClickListener retryListener, cancelListener; private static CancelListener dismissListener; [UnityEngine.Scripting.Preserve] 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); } [UnityEngine.Scripting.Preserve] private sealed class CancelListener : AndroidJavaProxy { public CancelListener() : base("android.content.DialogInterface$OnCancelListener") { } [UnityEngine.Scripting.Preserve] public void onCancel(AndroidJavaObject dialog) => Complete(false); } #endif public static void ShowRetryDialog(Action retry) { if (retry == null) throw new ArgumentNullException(nameof(retry)); ShowDialog(NetworkErrorMessage, result => { if (result) retry(); }, false); } public static void ShowDialog(string message, Action callback) => ShowDialog(message, callback, true); private static void ShowDialog(string message, Action callback, bool allowCancel) { if (callback == null) throw new ArgumentNullException(nameof(callback)); if (pendingCallback != null) throw new InvalidOperationException("A native dialog is already open."); pendingCallback = callback; try { #if UNITY_EDITOR Complete(allowCancel ? UnityEditor.EditorUtility.DisplayDialog("Network Error", message, "Retry", "Cancel") : UnityEditor.EditorUtility.DisplayDialog("Network Error", message, "retry")); #elif UNITY_IOS if (allowCancel) NativeApi.Dialog.AppRuntimes_ShowDialog(message, Callback); else NativeApi.Dialog.AppRuntimes_ShowRetryDialog(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); builder.Call("setTitle", "Network Error").Dispose(); builder.Call("setMessage", message).Dispose(); builder.Call("setPositiveButton", allowCancel ? "Retry" : "retry", retryListener).Dispose(); builder.Call("setCancelable", allowCancel).Dispose(); if (allowCancel) { cancelListener = new ClickListener(false); dismissListener = new CancelListener(); 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) { var callback = Interlocked.Exchange(ref pendingCallback, null); if (callback != null) UniTask.Post(() => callback(result)); } }