PlaceAlongPath.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using UnityEngine;
  2. using UnityEngine.Serialization;
  3. namespace ARLocation
  4. {
  5. using Utils;
  6. /// <summary>
  7. /// This component places instances of a given prefab/GameObject along
  8. /// equally spaced positions in a LocationPath. Should be placed in
  9. /// the ARLocationRoot GameObject.
  10. /// </summary>
  11. [AddComponentMenu("AR+GPS/Place Along Path")]
  12. [HelpURL("https://http://docs.unity-ar-gps-location.com/guide/#placealongpath")]
  13. public class PlaceAlongPath : MonoBehaviour
  14. {
  15. /// <summary>
  16. /// The path to place the prefab instances on.
  17. /// </summary>
  18. [Header("Path Settings")]
  19. [FormerlySerializedAs("path")] [Tooltip("The path to place the prefab instances on.")]
  20. public LocationPath Path;
  21. /// <summary>
  22. /// The prefab/GameObject to be palced along the path.
  23. /// </summary>
  24. [FormerlySerializedAs("prefab")] [Tooltip("The prefab/GameObject to be palced along the path.")]
  25. public GameObject Prefab;
  26. /// <summary>
  27. /// The number of object instances to be placed, excluding the endpoints. That is,
  28. /// the total number of instances is equal to objectCount + 2
  29. /// </summary>
  30. [FormerlySerializedAs("objectCount")] [Tooltip("The number of object instances to be placed, excluding the endpoints. That is, the total number of instances is equal to objectCount + 2")]
  31. public int ObjectCount = 10;
  32. /// <summary>
  33. /// The size of the sample used to calculate the spline.
  34. /// </summary>
  35. [FormerlySerializedAs("splineSampleSize")] [Tooltip("The size of the sample used to calculate the spline.")]
  36. public int SplineSampleSize = 200;
  37. public PlaceAtLocation.PlaceAtOptions PlacementSettings;
  38. public AltitudeMode AltitudeMode = AltitudeMode.DeviceRelative;
  39. [Space(4.0f)]
  40. [Header("Debug")]
  41. [Tooltip("When debug mode is enabled, this component will print relevant messages to the console. Filter by 'PlaceAlongPath' in the log output to see the messages.")]
  42. public bool DebugMode;
  43. [Space(4.0f)]
  44. private Spline spline;
  45. private Vector3[] points;
  46. private void Start()
  47. {
  48. points = new Vector3[Path.Locations.Length];
  49. for (var i = 0; i < points.Length; i++)
  50. {
  51. points[i] = Path.Locations[i].ToVector3();
  52. }
  53. spline = Misc.BuildSpline(Path.SplineType, points, SplineSampleSize, Path.Alpha);
  54. var sample = spline.SamplePoints(ObjectCount);
  55. for (var i = 0; i < sample.Length; i++)
  56. {
  57. var location = new Location()
  58. {
  59. Latitude = sample[i].z,
  60. Longitude = sample[i].x,
  61. Altitude = sample[i].y,
  62. AltitudeMode = AltitudeMode
  63. };
  64. var instance = PlaceAtLocation.CreatePlacedInstance(Prefab, location, PlacementSettings, DebugMode);
  65. instance.name = $"{gameObject.name} - {i}";
  66. }
  67. }
  68. }
  69. }