OpenGPSView.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. if (ins) return null;
  14. ModalView modalView = ModalView.Show();
  15. OpenGPSView openGPSView = modalView.gameObject.AddComponent<OpenGPSView>();
  16. modalView.textKey = "gps_notopen";
  17. modalView.onAgreeTextKey = "gps_go-to-open";
  18. modalView.onRejectTextKey = "gps_giveup-open";
  19. modalView.willDestroyAfterClick = false;
  20. modalView.onAgree = () => openGPSView.GoToOpenGPS();
  21. modalView.onReject = () => {
  22. modalView.willDestroyAfterClick = true;
  23. };
  24. return openGPSView;
  25. }
  26. void Awake()
  27. {
  28. ins = this;
  29. }
  30. void Start()
  31. {
  32. _clsU3D = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  33. _objCtx = _clsU3D.GetStatic<AndroidJavaObject>("currentActivity");
  34. _objGPSTool = new AndroidJavaClass("com.example.smartbowlib.GPSTool");
  35. }
  36. void OnDestroy()
  37. {
  38. if (ins == this) ins = null;
  39. if (_objGPSTool != null)
  40. {
  41. _objGPSTool.Dispose();
  42. _objGPSTool = null;
  43. }
  44. if (_objCtx != null)
  45. {
  46. _objCtx.Dispose();
  47. _objCtx = null;
  48. }
  49. if (_objCtx != null)
  50. {
  51. _objCtx.Dispose();
  52. _objCtx = null;
  53. }
  54. onOpened = null;
  55. }
  56. float time = 0;
  57. void Update()
  58. {
  59. time += Time.deltaTime;
  60. if (time > 0.1f) {
  61. time = 0;
  62. CheckOpenGPS();
  63. }
  64. }
  65. void CheckOpenGPS()
  66. {
  67. if (_objGPSTool.CallStatic<bool>("hasOpenGPS", _objCtx))
  68. {
  69. Destroy(gameObject);
  70. Debug.Log("onOpenGPS");
  71. onOpened?.Invoke();
  72. Debug.Log("onOpenGPS-Callback-Invoke");
  73. }
  74. }
  75. void GoToOpenGPS()
  76. {
  77. _objGPSTool.CallStatic("goToOpenGPS", _objCtx);
  78. }
  79. }