| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 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<bool> 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<bool> callback) => ShowDialog(message, callback, true);
- private static void ShowDialog(string message, Action<bool> 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<AndroidJavaObject>("currentActivity");
- activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
- {
- try
- {
- using (var builder = new AndroidJavaObject("android.app.AlertDialog$Builder", activity))
- {
- retryListener = new ClickListener(true);
- builder.Call<AndroidJavaObject>("setTitle", "Network Error").Dispose();
- builder.Call<AndroidJavaObject>("setMessage", message).Dispose();
- builder.Call<AndroidJavaObject>("setPositiveButton", allowCancel ? "Retry" : "retry", retryListener).Dispose();
- builder.Call<AndroidJavaObject>("setCancelable", allowCancel).Dispose();
- if (allowCancel)
- {
- cancelListener = new ClickListener(false);
- dismissListener = new CancelListener();
- builder.Call<AndroidJavaObject>("setNegativeButton", "Cancel", cancelListener).Dispose();
- builder.Call<AndroidJavaObject>("setOnCancelListener", dismissListener).Dispose();
- }
- builder.Call<AndroidJavaObject>("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));
- }
- }
|