NewPathLineRenderer.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using UnityEngine;
  2. namespace ARLocation
  3. {
  4. public class NewPathLineRenderer : MonoBehaviour
  5. {
  6. public LocationPath Path;
  7. public int MaxNumberOfUpdates = 4;
  8. private ARLocationProvider locationProvider;
  9. private LineRenderer lineRenderer;
  10. private Transform arLocationRoot;
  11. bool initialized;
  12. int updateCount;
  13. void Start()
  14. {
  15. #if UNITY_EDITOR
  16. MaxNumberOfUpdates = 1;
  17. #endif
  18. }
  19. public void Init(LocationPath path, LineRenderer renderer)
  20. {
  21. Path = path;
  22. lineRenderer = renderer;
  23. arLocationRoot = ARLocationManager.Instance.gameObject.transform;
  24. initialized = true;
  25. locationProvider = ARLocationProvider.Instance;
  26. locationProvider.OnLocationUpdatedDelegate += locationUpdatedHandler;
  27. if (locationProvider.IsEnabled) {
  28. locationUpdatedHandler(locationProvider.CurrentLocation, locationProvider.CurrentLocation);
  29. }
  30. }
  31. private void locationUpdatedHandler(LocationReading locationReading, LocationReading _)
  32. {
  33. if (!initialized || updateCount >= MaxNumberOfUpdates)
  34. {
  35. return;
  36. }
  37. var points = new Vector3[Path.Locations.Length];
  38. var cameraTransform = ARLocationManager.Instance.MainCamera.transform;
  39. var location = locationReading.ToLocation();
  40. for (int i = 0; i < points.Length; i++)
  41. {
  42. var loc = Path.Locations[i];
  43. points[i] = Location.GetGameObjectPositionForLocation(arLocationRoot, ARLocationManager.Instance.MainCamera.transform, location, loc, true);
  44. //points[i] = lineRenderer.gameObject.transform.worldToLocalMatrix.MultiplyPoint(points[i]);
  45. points[i].y = points[i].z;
  46. points[i].z = 0;
  47. }
  48. lineRenderer.useWorldSpace = false;
  49. lineRenderer.positionCount = Path.Locations.Length;
  50. lineRenderer.SetPositions(points);
  51. lineRenderer.alignment = LineAlignment.TransformZ;
  52. lineRenderer.gameObject.transform.localRotation = Quaternion.Euler(90, 0, 0);
  53. updateCount++;
  54. }
  55. void Update()
  56. {
  57. if (!initialized)
  58. {
  59. return;
  60. }
  61. lineRenderer.gameObject.transform.localPosition = MathUtils.SetY(lineRenderer.gameObject.transform.localPosition, Camera.main.transform.position.y - 1.5f);
  62. }
  63. }
  64. }