NativeDialog.cs 4.2 KB

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