| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- 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<bool> 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<bool> 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<AndroidJavaObject>("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<AndroidJavaObject>("setTitle", "Network Error").Dispose();
- builder.Call<AndroidJavaObject>("setMessage", message).Dispose();
- builder.Call<AndroidJavaObject>("setPositiveButton", "Retry", retryListener).Dispose();
- 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)
- {
- // Android UI callbacks and iOS callbacks must resume Unity work on its player loop.
- UniTask.Post(() =>
- {
- var callback = pendingCallback;
- pendingCallback = null;
- callback?.Invoke(result);
- });
- }
- }
|