OpenGPSView.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class OpenGPSView : MonoBehaviour
  5. {
  6. private AndroidJavaClass _clsU3D;
  7. private AndroidJavaObject _objCtx;
  8. private AndroidJavaClass _objGPSTool;
  9. public System.Action onOpened;
  10. private static OpenGPSView ins;
  11. public static OpenGPSView Init()
  12. {
  13. // 已废弃:不再弹出「未开 GPS / 前往打开」引导。地区请在个人资料中填写;无 GPS 设备跳系统定位页无意义。(保留旧实现备查,勿删)
  14. // if (ins) return null;
  15. // ModalView modalView = ModalView.Show();
  16. // OpenGPSView openGPSView = modalView.gameObject.AddComponent<OpenGPSView>();
  17. // modalView.textKey = "gps_notopen";
  18. // modalView.onAgreeTextKey = "gps_go-to-open";
  19. // modalView.onRejectTextKey = "gps_giveup-open";
  20. // modalView.willDestroyAfterClick = false;
  21. // modalView.onAgree = () => openGPSView.GoToOpenGPS();
  22. // modalView.onReject = () => {
  23. // modalView.willDestroyAfterClick = true;
  24. // };
  25. // return openGPSView;
  26. return null;
  27. }
  28. void Awake()
  29. {
  30. ins = this;
  31. }
  32. void Start()
  33. {
  34. _clsU3D = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  35. _objCtx = _clsU3D.GetStatic<AndroidJavaObject>("currentActivity");
  36. _objGPSTool = new AndroidJavaClass("com.example.smartbowlib.GPSTool");
  37. }
  38. void OnDestroy()
  39. {
  40. if (ins == this) ins = null;
  41. if (_objGPSTool != null)
  42. {
  43. _objGPSTool.Dispose();
  44. _objGPSTool = null;
  45. }
  46. if (_objCtx != null)
  47. {
  48. _objCtx.Dispose();
  49. _objCtx = null;
  50. }
  51. if (_objCtx != null)
  52. {
  53. _objCtx.Dispose();
  54. _objCtx = null;
  55. }
  56. onOpened = null;
  57. }
  58. float time = 0;
  59. void Update()
  60. {
  61. time += Time.deltaTime;
  62. if (time > 0.1f) {
  63. time = 0;
  64. CheckOpenGPS();
  65. }
  66. }
  67. void CheckOpenGPS()
  68. {
  69. if (_objGPSTool.CallStatic<bool>("hasOpenGPS", _objCtx))
  70. {
  71. Destroy(gameObject);
  72. Debug.Log("onOpenGPS");
  73. onOpened?.Invoke();
  74. Debug.Log("onOpenGPS-Callback-Invoke");
  75. }
  76. }
  77. void GoToOpenGPS()
  78. {
  79. // 旧:跳转系统「定位源/GPS」设置页(smartbowlib.GPSTool.goToOpenGPS → ACTION_LOCATION_SOURCE_SETTINGS)(保留,勿删)
  80. // _objGPSTool.CallStatic("goToOpenGPS", _objCtx);
  81. }
  82. }