GPSTool.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. //GPS获取地址
  27. //成功返回:string[]{国,省,市} (数组中不允许有null)
  28. //失败返回:null
  29. public static void GetAddress(Action<string[]> callback)
  30. {
  31. #if UNITY_ANDROID
  32. Func<string[]> func = delegate ()
  33. {
  34. if (Application.isEditor) return null;
  35. using (var clsU3D = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  36. using (var objCtx = clsU3D.GetStatic<AndroidJavaObject>("currentActivity"))
  37. using (var objGPSTool = new AndroidJavaClass("com.example.smartbowlib.GPSTool"))
  38. {
  39. string[] address = objGPSTool.CallStatic<string[]>("getAddress", objCtx);
  40. if (address != null)
  41. {
  42. foreach (var item in address)
  43. if (item == null) return null;
  44. }
  45. return address;
  46. }
  47. };
  48. List<string> permissions = new List<string>();
  49. permissions.Add(Permission.CoarseLocation);
  50. permissions.Add(Permission.FineLocation);
  51. for (int i = permissions.Count - 1; i >= 0; i--)
  52. {
  53. if (Permission.HasUserAuthorizedPermission(permissions[i])) permissions.RemoveAt(i);
  54. }
  55. if (permissions.Count > 0)
  56. {
  57. var requestCB = new PermissionCallbacks();
  58. int needPermissionCount = permissions.Count;
  59. requestCB.PermissionGranted += (s) =>
  60. {
  61. Debug.Log("用户同意" + s);
  62. needPermissionCount--;
  63. if (needPermissionCount == 0) callback.Invoke(func.Invoke());
  64. };
  65. requestCB.PermissionDenied += (s) =>
  66. {
  67. Debug.Log("用户拒绝" + s);
  68. };
  69. requestCB.PermissionDeniedAndDontAskAgain += (s) =>
  70. {
  71. Debug.Log("用户拒绝且要求不再询问" + s);
  72. };
  73. Permission.RequestUserPermissions(permissions.ToArray(), requestCB);
  74. }
  75. else
  76. {
  77. callback?.Invoke(func.Invoke());
  78. }
  79. #endif
  80. #if UNITY_IPHONE
  81. callbackOut = callback;
  82. ResultHandler handler = new ResultHandler(resultHandler);
  83. IntPtr fp = Marshal.GetFunctionPointerForDelegate(handler);
  84. LocationPluginInterface.StartUpdatingLocation(fp);
  85. #endif
  86. }
  87. }