NativeDialog.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using DialogCallback = NativeApi.DialogCallback;
  3. using System.Threading;
  4. using Cysharp.Threading.Tasks;
  5. using UnityEngine;
  6. public static class NativeDialog
  7. {
  8. public const string NetworkErrorMessage = "There is a problem with the network. Please try again later.";
  9. private static Action<bool> pendingCallback;
  10. #if UNITY_IOS && !UNITY_EDITOR
  11. private static readonly DialogCallback Callback = OnIOSResult;
  12. [AOT.MonoPInvokeCallback(typeof(DialogCallback))]
  13. private static void OnIOSResult(int result) => Complete(result != 0);
  14. #elif UNITY_ANDROID && !UNITY_EDITOR
  15. private static ClickListener retryListener, cancelListener;
  16. private static CancelListener dismissListener;
  17. [UnityEngine.Scripting.Preserve]
  18. private sealed class ClickListener : AndroidJavaProxy
  19. {
  20. private readonly bool result;
  21. public ClickListener(bool result) : base("android.content.DialogInterface$OnClickListener")
  22. => this.result = result;
  23. [UnityEngine.Scripting.Preserve]
  24. public void onClick(AndroidJavaObject dialog, int which) => Complete(result);
  25. }
  26. [UnityEngine.Scripting.Preserve]
  27. private sealed class CancelListener : AndroidJavaProxy
  28. {
  29. public CancelListener() : base("android.content.DialogInterface$OnCancelListener") { }
  30. [UnityEngine.Scripting.Preserve]
  31. public void onCancel(AndroidJavaObject dialog) => Complete(false);
  32. }
  33. #endif
  34. public static void ShowRetryDialog(Action retry)
  35. {
  36. if (retry == null) throw new ArgumentNullException(nameof(retry));
  37. ShowDialog(NetworkErrorMessage, result => { if (result) retry(); }, false);
  38. }
  39. public static void ShowDialog(string message, Action<bool> callback) => ShowDialog(message, callback, true);
  40. private static void ShowDialog(string message, Action<bool> callback, bool allowCancel)
  41. {
  42. if (callback == null) throw new ArgumentNullException(nameof(callback));
  43. if (pendingCallback != null)
  44. throw new InvalidOperationException("A native dialog is already open.");
  45. pendingCallback = callback;
  46. try
  47. {
  48. #if UNITY_EDITOR
  49. Complete(allowCancel
  50. ? UnityEditor.EditorUtility.DisplayDialog("Network Error", message, "Retry", "Cancel")
  51. : UnityEditor.EditorUtility.DisplayDialog("Network Error", message, "retry"));
  52. #elif UNITY_IOS
  53. if (allowCancel) NativeApi.Dialog.AppRuntimes_ShowDialog(message, Callback);
  54. else NativeApi.Dialog.AppRuntimes_ShowRetryDialog(message, Callback);
  55. #elif UNITY_ANDROID
  56. using (var player = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  57. {
  58. var activity = player.GetStatic<AndroidJavaObject>("currentActivity");
  59. activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
  60. {
  61. try
  62. {
  63. using (var builder = new AndroidJavaObject("android.app.AlertDialog$Builder", activity))
  64. {
  65. retryListener = new ClickListener(true);
  66. builder.Call<AndroidJavaObject>("setTitle", "Network Error").Dispose();
  67. builder.Call<AndroidJavaObject>("setMessage", message).Dispose();
  68. builder.Call<AndroidJavaObject>("setPositiveButton", allowCancel ? "Retry" : "retry", retryListener).Dispose();
  69. builder.Call<AndroidJavaObject>("setCancelable", allowCancel).Dispose();
  70. if (allowCancel)
  71. {
  72. cancelListener = new ClickListener(false);
  73. dismissListener = new CancelListener();
  74. builder.Call<AndroidJavaObject>("setNegativeButton", "Cancel", cancelListener).Dispose();
  75. builder.Call<AndroidJavaObject>("setOnCancelListener", dismissListener).Dispose();
  76. }
  77. builder.Call<AndroidJavaObject>("show").Dispose();
  78. }
  79. }
  80. catch (Exception exception)
  81. {
  82. Debug.LogException(exception);
  83. Complete(false);
  84. }
  85. finally { activity.Dispose(); }
  86. }));
  87. }
  88. #else
  89. Debug.LogWarning("Native network dialogs are supported on iOS, Android and in the Editor.");
  90. Complete(false);
  91. #endif
  92. }
  93. catch (Exception exception)
  94. {
  95. Debug.LogException(exception);
  96. Complete(false);
  97. }
  98. }
  99. private static void Complete(bool result)
  100. {
  101. var callback = Interlocked.Exchange(ref pendingCallback, null);
  102. if (callback != null) UniTask.Post(() => callback(result));
  103. }
  104. }