GPSTool.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. // using UnityEngine.Android; // 旧:Permission 申请定位权限(已废弃,保留备查)
  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. // 旧:原生层在定位就绪后会通知 Unity,用于延迟再次 GetAddress(保留,勿删)
  28. // private static Action<string[]> callbackRecord;
  29. // public static void OnLocationUpdate()
  30. // {
  31. // if (callbackRecord != null)
  32. // {
  33. // GetAddress(callbackRecord);
  34. // callbackRecord = null;
  35. // }
  36. // }
  37. /// <summary>兼容 MsgReceiver 等 JNI 调用链;已不再衔接 GPS 延迟回调。</summary>
  38. public static void OnLocationUpdate() { }
  39. #endif
  40. //GPS获取地址
  41. //成功返回:string[]{国,省,市} (数组中不允许有null)
  42. //失败返回:null
  43. public static void GetAddress(Action<string[]> callback)
  44. {
  45. #if UNITY_ANDROID
  46. // Android:不再申请定位权限 / 不调用原生 GPS;地区取自用户资料(与排行榜一致),未设置国家则返回 null
  47. string c = LoginMgr.myUserInfo.country != null ? LoginMgr.myUserInfo.country.Trim() : "";
  48. string s = LoginMgr.myUserInfo.state != null ? LoginMgr.myUserInfo.state.Trim() : "";
  49. string ci = LoginMgr.myUserInfo.city != null ? LoginMgr.myUserInfo.city.Trim() : "";
  50. if (string.IsNullOrWhiteSpace(c))
  51. callback?.Invoke(null);
  52. else
  53. callback?.Invoke(new string[] { c, s, ci });
  54. // 旧 Android:Unity 权限申请 + 原生 GPSTool(hasOpenGPS / getAddress / OpenGPSView)(保留,勿删)
  55. // Action callAndroidMethod = delegate ()
  56. // {
  57. // if (Application.isEditor) return;
  58. // string[] address = null;
  59. // using (var clsU3D = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  60. // using (var objCtx = clsU3D.GetStatic<AndroidJavaObject>("currentActivity"))
  61. // using (var objGPSTool = new AndroidJavaClass("com.example.smartbowlib.GPSTool"))
  62. // {
  63. // bool gpsOpened = objGPSTool.CallStatic<bool>("hasOpenGPS", objCtx);
  64. // if (!gpsOpened)
  65. // {
  66. // var view = OpenGPSView.Init();
  67. // if (view) view.onOpened = () => GetAddress(callback);
  68. // return;
  69. // }
  70. // address = objGPSTool.CallStatic<string[]>("getAddress", objCtx);
  71. // if (address != null)
  72. // {
  73. // foreach (var item in address)
  74. // {
  75. // if (item == null)
  76. // {
  77. // address = null;
  78. // break;
  79. // }
  80. // }
  81. // }
  82. // if (address != null)
  83. // {
  84. // Debug.Log("获取地理位置成功:" + string.Join(" ", address));
  85. // callback?.Invoke(address);
  86. // }
  87. // //如果安卓层未能获取到位置(比如gps刚打开),会等待gps更新,至到知道位置后,会通知Unity
  88. // //因此可以先保留callback用于下次回调
  89. // else if (address == null) callbackRecord = callback;
  90. // }
  91. // };
  92. // List<string> permissions = new List<string>();
  93. // permissions.Add(Permission.CoarseLocation);
  94. // permissions.Add(Permission.FineLocation);
  95. // for (int i = permissions.Count - 1; i >= 0; i--)
  96. // {
  97. // if (Permission.HasUserAuthorizedPermission(permissions[i])) permissions.RemoveAt(i);
  98. // }
  99. // if (permissions.Count > 0)
  100. // {
  101. // var requestCB = new PermissionCallbacks();
  102. // int needPermissionCount = permissions.Count;
  103. // requestCB.PermissionGranted += (s) =>
  104. // {
  105. // Debug.Log("用户同意" + s);
  106. // needPermissionCount--;
  107. // if (needPermissionCount == 0) callAndroidMethod.Invoke();
  108. // };
  109. // bool hasExecuteOnDenied = false;
  110. // requestCB.PermissionDenied += (s) =>
  111. // {
  112. // Debug.Log("用户拒绝" + s);
  113. // if (!hasExecuteOnDenied)
  114. // {
  115. // hasExecuteOnDenied = true;
  116. // AlertNoPermission();
  117. // }
  118. // };
  119. // requestCB.PermissionDeniedAndDontAskAgain += (s) =>
  120. // {
  121. // Debug.Log("用户拒绝且要求不再询问" + s);
  122. // if (!hasExecuteOnDenied)
  123. // {
  124. // hasExecuteOnDenied = true;
  125. // AlertNoPermission();
  126. // }
  127. // };
  128. // Permission.RequestUserPermissions(permissions.ToArray(), requestCB);
  129. // }
  130. // else
  131. // {
  132. // callAndroidMethod.Invoke();
  133. // }
  134. #endif
  135. #if UNITY_IPHONE
  136. callbackOut = callback;
  137. ResultHandler handler = new ResultHandler(resultHandler);
  138. IntPtr fp = Marshal.GetFunctionPointerForDelegate(handler);
  139. LocationPluginInterface.StartUpdatingLocation(fp);
  140. #endif
  141. }
  142. //提示用户:尚未授予定位权限
  143. static void AlertNoPermission()
  144. {
  145. ModalManager.ShowConfirmModal_NoLocationPermission();
  146. }
  147. }