PlaceAtLocations.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Serialization;
  5. // ReSharper disable CollectionNeverQueried.Local
  6. // ReSharper disable MemberCanBePrivate.Global
  7. namespace ARLocation
  8. {
  9. /// <summary>
  10. /// This class instantiates a prefab at the given GPS locations. Must
  11. /// be in the `ARLocationRoot` GameObject with a `ARLocatedObjectsManager`
  12. /// Component.
  13. /// </summary>
  14. [AddComponentMenu("AR+GPS/Place At Locations")]
  15. [HelpURL("https://http://docs.unity-ar-gps-location.com/guide/#placeatlocations")]
  16. public class PlaceAtLocations : MonoBehaviour
  17. {
  18. [Serializable]
  19. public class Entry
  20. {
  21. public LocationData ObjectLocation;
  22. public OverrideAltitudeData OverrideAltitude = new OverrideAltitudeData();
  23. }
  24. [Tooltip("The locations where the objects will be instantiated.")]
  25. public List<PlaceAtLocation.LocationSettingsData> Locations;
  26. public PlaceAtLocation.PlaceAtOptions PlacementOptions;
  27. /// <summary>
  28. /// The game object that will be instantiated.
  29. /// </summary>
  30. [FormerlySerializedAs("prefab")] [Tooltip("The game object that will be instantiated.")]
  31. public GameObject Prefab;
  32. [Space(4.0f)]
  33. [Header("Debug")]
  34. [Tooltip("When debug mode is enabled, this component will print relevant messages to the console. Filter by 'PlateAtLocations' in the log output to see the messages.")]
  35. public bool DebugMode;
  36. [Space(4.0f)]
  37. private readonly List<Location> locations = new List<Location>();
  38. private readonly List<GameObject> instances = new List<GameObject>();
  39. public List<GameObject> Instances => instances;
  40. private void Start()
  41. {
  42. foreach (var entry in Locations)
  43. {
  44. var newLoc = entry.GetLocation();
  45. AddLocation(newLoc);
  46. }
  47. }
  48. public void AddLocation(Location location)
  49. {
  50. var instance = PlaceAtLocation.CreatePlacedInstance(Prefab, location, PlacementOptions, DebugMode);
  51. instance.name = $"{gameObject.name} - {locations.Count}";
  52. locations.Add(location);
  53. instances.Add(instance);
  54. }
  55. }
  56. }