GPSTool.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Android;
  6. using System.Runtime.InteropServices;
  7. using AOT;
  8. public class GPSTool
  9. {
  10. #if UNITY_IPHONE
  11. static Action<string[]> callbackOut;
  12. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  13. public delegate void ResultHandler(string resultString);
  14. [MonoPInvokeCallback(typeof(ResultHandler))]
  15. static void resultHandler(string parameter)
  16. {
  17. //Debug.Log("unity parameter:" + parameter);
  18. string[] components = parameter.Split(new char[] { '\t' }, 6);
  19. string address = components[5];
  20. string[] addressOut = address.Split(new char[] { '\t' }, 3);
  21. callbackOut?.Invoke(addressOut);
  22. // 停止获取位置信息
  23. LocationPluginInterface.StopUpdatingLocation();
  24. }
  25. #endif
  26. #if UNITY_ANDROID
  27. private static Action<string[]> callbackRecord;
  28. public static void OnLocationUpdate()
  29. {
  30. if (callbackRecord != null)
  31. {
  32. GetAddress(callbackRecord);
  33. callbackRecord = null;
  34. }
  35. }
  36. #endif
  37. //GPS获取地址
  38. //成功返回:string[]{国,省,市} (数组中不允许有null)
  39. //失败返回:null
  40. public static void GetAddress(Action<string[]> callback)
  41. {
  42. #if UNITY_ANDROID
  43. Action callAndroidMethod = delegate ()
  44. {
  45. if (Application.isEditor) return;
  46. string[] address = null;
  47. using (var clsU3D = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  48. using (var objCtx = clsU3D.GetStatic<AndroidJavaObject>("currentActivity"))
  49. using (var objGPSTool = new AndroidJavaClass("com.example.smartbowlib.GPSTool"))
  50. {
  51. bool gpsOpened = objGPSTool.CallStatic<bool>("hasOpenGPS", objCtx);
  52. if (!gpsOpened)
  53. {
  54. var view = OpenGPSView.Init();
  55. if (view) view.onOpened = () => GetAddress(callback);
  56. return;
  57. }
  58. address = objGPSTool.CallStatic<string[]>("getAddress", objCtx);
  59. if (address != null)
  60. {
  61. foreach (var item in address)
  62. {
  63. if (item == null)
  64. {
  65. address = null;
  66. break;
  67. }
  68. }
  69. }
  70. if (address != null)
  71. {
  72. Debug.Log("获取地理位置成功:" + string.Join(" ", address));
  73. callback?.Invoke(address);
  74. }
  75. //如果安卓层未能获取到位置(比如gps刚打开),会等待gps更新,至到知道位置后,会通知Unity
  76. //因此可以先保留callback用于下次回调
  77. else if (address == null) callbackRecord = callback;
  78. }
  79. };
  80. List<string> permissions = new List<string>();
  81. permissions.Add(Permission.CoarseLocation);
  82. permissions.Add(Permission.FineLocation);
  83. for (int i = permissions.Count - 1; i >= 0; i--)
  84. {
  85. if (Permission.HasUserAuthorizedPermission(permissions[i])) permissions.RemoveAt(i);
  86. }
  87. if (permissions.Count > 0)
  88. {
  89. var requestCB = new PermissionCallbacks();
  90. int needPermissionCount = permissions.Count;
  91. requestCB.PermissionGranted += (s) =>
  92. {
  93. Debug.Log("用户同意" + s);
  94. needPermissionCount--;
  95. if (needPermissionCount == 0) callAndroidMethod.Invoke();
  96. };
  97. bool hasExecuteOnDenied = false;
  98. requestCB.PermissionDenied += (s) =>
  99. {
  100. Debug.Log("用户拒绝" + s);
  101. if (!hasExecuteOnDenied)
  102. {
  103. hasExecuteOnDenied = true;
  104. AlertNoPermission();
  105. }
  106. };
  107. requestCB.PermissionDeniedAndDontAskAgain += (s) =>
  108. {
  109. Debug.Log("用户拒绝且要求不再询问" + s);
  110. if (!hasExecuteOnDenied)
  111. {
  112. hasExecuteOnDenied = true;
  113. AlertNoPermission();
  114. }
  115. };
  116. Permission.RequestUserPermissions(permissions.ToArray(), requestCB);
  117. }
  118. else
  119. {
  120. callAndroidMethod.Invoke();
  121. }
  122. #endif
  123. #if UNITY_IPHONE
  124. callbackOut = callback;
  125. ResultHandler handler = new ResultHandler(resultHandler);
  126. IntPtr fp = Marshal.GetFunctionPointerForDelegate(handler);
  127. LocationPluginInterface.StartUpdatingLocation(fp);
  128. #endif
  129. }
  130. //提示用户:尚未授予定位权限
  131. static void AlertNoPermission()
  132. {
  133. ModalManager.ShowConfirmModal_NoLocationPermission();
  134. }
  135. }