NativeDialog.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #if UNITY_EDITOR
  38. Debug.LogError(NetworkErrorMessage);
  39. #else
  40. ShowDialog(NetworkErrorMessage, result => { if (result) retry(); }, false);
  41. #endif
  42. }
  43. public static void ShowDialog(string message, Action<bool> callback) => ShowDialog(message, callback, true);
  44. private static void ShowDialog(string message, Action<bool> callback, bool allowCancel)
  45. {
  46. if (callback == null) throw new ArgumentNullException(nameof(callback));
  47. if (pendingCallback != null)
  48. throw new InvalidOperationException("A native dialog is already open.");
  49. pendingCallback = callback;
  50. try
  51. {
  52. #if UNITY_EDITOR
  53. Complete(allowCancel
  54. ? UnityEditor.EditorUtility.DisplayDialog("Network Error", message, "Retry", "Cancel")
  55. : UnityEditor.EditorUtility.DisplayDialog("Network Error", message, "retry"));
  56. #elif UNITY_IOS
  57. if (allowCancel) NativeApi.Dialog.AppRuntimes_ShowDialog(message, Callback);
  58. else NativeApi.Dialog.AppRuntimes_ShowRetryDialog(message, Callback);
  59. #elif UNITY_ANDROID
  60. using (var player = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  61. {
  62. var activity = player.GetStatic<AndroidJavaObject>("currentActivity");
  63. activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
  64. {
  65. try
  66. {
  67. using (var builder = new AndroidJavaObject("android.app.AlertDialog$Builder", activity))
  68. {
  69. retryListener = new ClickListener(true);
  70. builder.Call<AndroidJavaObject>("setTitle", "Network Error").Dispose();
  71. builder.Call<AndroidJavaObject>("setMessage", message).Dispose();
  72. builder.Call<AndroidJavaObject>("setPositiveButton", allowCancel ? "Retry" : "retry", retryListener).Dispose();
  73. builder.Call<AndroidJavaObject>("setCancelable", allowCancel).Dispose();
  74. if (allowCancel)
  75. {
  76. cancelListener = new ClickListener(false);
  77. dismissListener = new CancelListener();
  78. builder.Call<AndroidJavaObject>("setNegativeButton", "Cancel", cancelListener).Dispose();
  79. builder.Call<AndroidJavaObject>("setOnCancelListener", dismissListener).Dispose();
  80. }
  81. builder.Call<AndroidJavaObject>("show").Dispose();
  82. }
  83. }
  84. catch (Exception exception)
  85. {
  86. Debug.LogException(exception);
  87. Complete(false);
  88. }
  89. finally { activity.Dispose(); }
  90. }));
  91. }
  92. #else
  93. Debug.LogWarning("Native network dialogs are supported on iOS, Android and in the Editor.");
  94. Complete(false);
  95. #endif
  96. }
  97. catch (Exception exception)
  98. {
  99. Debug.LogException(exception);
  100. Complete(false);
  101. }
  102. }
  103. private static void Complete(bool result)
  104. {
  105. var callback = Interlocked.Exchange(ref pendingCallback, null);
  106. if (callback != null) UniTask.Post(() => callback(result));
  107. }
  108. }