GPSDemo.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace Imagine.WebAR.Demo{
  7. [System.Serializable]
  8. public class TrackedPin{
  9. public GPSPin pin;
  10. [HideInInspector] public RectTransform pinMarker;
  11. }
  12. public class GPSDemo : MonoBehaviour
  13. {
  14. [SerializeField] Camera cam;
  15. [SerializeField] RectTransform pinMarkerPrefab;
  16. [HideInInspector] List<TrackedPin> trackedPins = new List<TrackedPin>();
  17. [SerializeField] TextMeshProUGUI youReachedPinText;
  18. private WorldTracker wt;
  19. IEnumerator Start(){
  20. wt = FindObjectOfType<WorldTracker>();
  21. wt.geolocationSettings.OnGPSPositionUpdated.AddListener(OnGPSPositionUpdated);
  22. pinMarkerPrefab.gameObject.SetActive(false);
  23. youReachedPinText.gameObject.SetActive(false);
  24. //delay initialization to allo for worldtracker to create pins
  25. yield return new WaitForEndOfFrame();
  26. CreatePinTrackers();
  27. }
  28. void OnDestroy(){
  29. wt.geolocationSettings.OnGPSPositionUpdated.RemoveListener(OnGPSPositionUpdated);
  30. }
  31. void OnGPSPositionUpdated(GPSData data){
  32. }
  33. void CreatePinTrackers(){
  34. foreach(var pin in wt.geolocationSettings.gpsPins){
  35. var pinMarker = Instantiate(pinMarkerPrefab, pinMarkerPrefab.parent);
  36. var trackedPin = new TrackedPin{
  37. pin = pin,
  38. pinMarker = pinMarker
  39. };
  40. pinMarker.gameObject.SetActive(true);
  41. trackedPin.pinMarker = pinMarker;
  42. var pinText = pinMarker.GetComponentInChildren<TextMeshProUGUI>();
  43. pinText.text = trackedPin.pin.id;
  44. pinMarker.name = trackedPin.pin.id + " Pin";
  45. trackedPins.Add(trackedPin);
  46. }
  47. }
  48. void Update(){
  49. var container = pinMarkerPrefab.parent.GetComponent<RectTransform>();
  50. var canvasW = container.rect.width;
  51. var canvasH = container.rect.height;
  52. foreach(var trackedPin in trackedPins){
  53. var pin = trackedPin.pinMarker;
  54. var viewPos = cam.WorldToViewportPoint(trackedPin.pin.transform.position);
  55. SetPinText(pin, viewPos.x > 0.5f);
  56. viewPos.x -= 0.5f;
  57. viewPos.y -= 0.5f;
  58. viewPos.x = Mathf.Clamp(viewPos.x, -0.5f, 0.5f);
  59. //viewPos.y = Mathf.Clamp(viewPos.y, -0.5f, 0.5f);
  60. pin.anchoredPosition = new Vector2(viewPos.x * canvasW, viewPos.y * canvasH);
  61. pin.gameObject.SetActive(viewPos.z > 0.5f && trackedPin.pin.transform.gameObject.activeInHierarchy);
  62. var dist = Mathf.Round(trackedPin.pin.transform.position.magnitude / 5) * 5;
  63. var pinText = pin.GetComponentInChildren<TextMeshProUGUI>();
  64. pinText.text = trackedPin.pin.id + "\n" + dist + "m";
  65. }
  66. }
  67. void SetPinText(RectTransform pin, bool right = true){
  68. var pinText = pin.GetComponentInChildren<TextMeshProUGUI>();
  69. if(!right){
  70. pinText.rectTransform.anchoredPosition = new Vector2(10, 0);
  71. pinText.alignment = TextAlignmentOptions.MidlineLeft;
  72. }
  73. else {
  74. pinText.rectTransform.anchoredPosition = new Vector2(-430, 0);
  75. pinText.alignment = TextAlignmentOptions.MidlineRight;
  76. }
  77. //toggle pin to force reactivate changes
  78. pinText.gameObject.SetActive(false);
  79. pinText.gameObject.SetActive(true);
  80. }
  81. public void OnEnterPin(GPSPin pin){
  82. youReachedPinText.gameObject.SetActive(true);
  83. youReachedPinText.text = "You reached Pin - " + pin.id + "!";
  84. }
  85. public void OnExitPin(GPSPin pin){
  86. youReachedPinText.gameObject.SetActive(false);
  87. }
  88. }
  89. }