CustomRoute.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. namespace ARLocation.MapboxRoutes
  4. {
  5. [CreateAssetMenu(fileName = "CustomRoute", menuName = "AR+GPS/Route")]
  6. public class CustomRoute : ScriptableObject
  7. {
  8. [System.Serializable]
  9. public class Point
  10. {
  11. [Tooltip("The geographical location of this route point.")]
  12. public Location Location;
  13. [Tooltip("If true, this point is considered a \"Step\" in thre route. A route \"Step\" is a point of the route where"
  14. + "a meneuver is expected to happen, e.g., \"TurnRight\".")]
  15. public bool IsStep;
  16. [Tooltip("The name of the point (optional)")]
  17. public string Name;
  18. [Tooltip("The instruction to the user informing of the maneuver to be exectured (in case \"IsStep\" is true).")]
  19. public string Instruction;
  20. }
  21. [Tooltip("The name of the custom route.")]
  22. public string Name;
  23. [Tooltip("A list of point defining the custom route.")]
  24. public List<Point> Points = new List<Point>() { new Point { }, new Point { } };
  25. [HideInInspector]
  26. public float SceneViewScale = 1.0f;
  27. private bool isDirty = true;
  28. public bool IsDirty
  29. {
  30. get => isDirty;
  31. set { isDirty = value; }
  32. }
  33. public void OnValidate()
  34. {
  35. isDirty = true;
  36. if (Points.Count == 0)
  37. {
  38. Points.Add(new Point { });
  39. Points.Add(new Point { });
  40. }
  41. if (Points.Count == 1)
  42. {
  43. Points.Add(new Point { });
  44. }
  45. if (Points.Count > 0)
  46. {
  47. Points[0].IsStep = true;
  48. Points[Points.Count - 1].IsStep = true;
  49. }
  50. }
  51. public List<Waypoint> GetWaypoints()
  52. {
  53. var result = new List<Waypoint>();
  54. if (Points.Count < 2)
  55. {
  56. return result;
  57. }
  58. result.Add(new Waypoint { location = Points[0].Location, name = Points[0].Name });
  59. result.Add(new Waypoint { location = Points[Points.Count - 1].Location, name = Points[Points.Count - 1].Name });
  60. return result;
  61. }
  62. public Route ToMapboxRoute()
  63. {
  64. var route = new Route { };
  65. var leg = new Route.RouteLeg();
  66. leg.steps = new List<Route.Step>();
  67. route.geometry = new Route.Geometry();
  68. route.legs = new List<Route.RouteLeg> { leg };
  69. route.name = Name; ;
  70. foreach (var p in Points)
  71. {
  72. route.geometry.coordinates.Add(p.Location.Clone());
  73. if (p.IsStep)
  74. {
  75. var step = new Route.Step();
  76. step.geometry = new Route.Geometry();
  77. step.geometry.coordinates.Add(p.Location.Clone());
  78. step.name = p.Name;
  79. step.maneuver = new Route.Maneuver();
  80. step.maneuver.location = p.Location.Clone();
  81. step.maneuver.instruction = p.Instruction;
  82. leg.steps.Add(step);
  83. }
  84. }
  85. return route;
  86. }
  87. }
  88. }