WebMapLoader.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System.Collections.Generic;
  2. using System.Globalization;
  3. using System.Xml;
  4. using UnityEngine;
  5. namespace ARLocation
  6. {
  7. public class WebMapLoader : MonoBehaviour
  8. {
  9. public class DataEntry
  10. {
  11. public int id;
  12. public double lat;
  13. public double lng;
  14. public double altitude;
  15. public string altitudeMode;
  16. public string name;
  17. public string meshId;
  18. public float movementSmoothing;
  19. public int maxNumberOfLocationUpdates;
  20. public bool useMovingAverage;
  21. public bool hideObjectUtilItIsPlaced;
  22. public AltitudeMode getAltitudeMode()
  23. {
  24. if (altitudeMode == "GroundRelative")
  25. {
  26. return AltitudeMode.GroundRelative;
  27. }
  28. else if (altitudeMode == "DeviceRelative")
  29. {
  30. return AltitudeMode.DeviceRelative;
  31. }
  32. else if (altitudeMode == "Absolute")
  33. {
  34. return AltitudeMode.Absolute;
  35. }
  36. else
  37. {
  38. return AltitudeMode.Ignore;
  39. }
  40. }
  41. }
  42. /// <summary>
  43. /// The `PrefabDatabase` ScriptableObject, containing a dictionary of Prefabs with a string ID.
  44. /// </summary>
  45. public PrefabDatabase PrefabDatabase;
  46. /// <summary>
  47. /// The XML data file download from the Web Map Editor (htttps://editor.unity-ar-gps-location.com)
  48. /// </summary>
  49. public TextAsset XmlDataFile;
  50. /// <summary>
  51. /// If true, enable DebugMode on the `PlaceAtLocation` generated instances.
  52. /// </summary>
  53. public bool DebugMode;
  54. /// <summary>
  55. /// Returns a list of the `PlaceAtLocation` instances created by this compoonent.
  56. /// >/summary>
  57. public List<PlaceAtLocation> Instances
  58. {
  59. get => _placeAtComponents;
  60. }
  61. private List<DataEntry> _dataEntries = new List<DataEntry>();
  62. private List<PlaceAtLocation> _placeAtComponents = new List<PlaceAtLocation>();
  63. // Start is called before the first frame update
  64. void Start()
  65. {
  66. LoadXmlFile();
  67. BuildGameObjects();
  68. }
  69. /// <summary>
  70. ///
  71. /// Calls `SetActive(value)` for each of the gameObjects created by this component.
  72. ///
  73. /// </summary>
  74. public void SetActiveGameObjects(bool value)
  75. {
  76. foreach (var i in _placeAtComponents)
  77. {
  78. i.gameObject.SetActive(value);
  79. }
  80. }
  81. /// <summary>
  82. ///
  83. /// Hides all the meshes contained on each of the gameObjects created
  84. /// by this component, but does not disable the gameObjects.
  85. ///
  86. /// </summary>
  87. public void HideMeshes()
  88. {
  89. foreach (var i in _placeAtComponents)
  90. {
  91. Utils.Misc.HideGameObject(i.gameObject);
  92. }
  93. }
  94. /// <summary>
  95. ///
  96. /// Makes all the gameObjects visible after calling `HideMeshes`.
  97. ///
  98. /// </summary>
  99. public void ShowMeshes()
  100. {
  101. foreach (var i in _placeAtComponents)
  102. {
  103. Utils.Misc.ShowGameObject(i.gameObject);
  104. }
  105. }
  106. void BuildGameObjects()
  107. {
  108. foreach (var entry in _dataEntries)
  109. {
  110. var Prefab = PrefabDatabase.GetEntryById(entry.meshId);
  111. if (!Prefab)
  112. {
  113. Debug.LogWarning($"[ARLocation#WebMapLoader]: Prefab {entry.meshId} not found.");
  114. continue;
  115. }
  116. var PlacementOptions = new PlaceAtLocation.PlaceAtOptions()
  117. {
  118. MovementSmoothing = entry.movementSmoothing,
  119. MaxNumberOfLocationUpdates = entry.maxNumberOfLocationUpdates,
  120. UseMovingAverage = entry.useMovingAverage,
  121. HideObjectUntilItIsPlaced = entry.hideObjectUtilItIsPlaced
  122. };
  123. var location = new Location()
  124. {
  125. Latitude = entry.lat,
  126. Longitude = entry.lng,
  127. Altitude = entry.altitude,
  128. AltitudeMode = entry.getAltitudeMode(),
  129. Label = entry.name
  130. };
  131. var instance = PlaceAtLocation.CreatePlacedInstance(Prefab,
  132. location,
  133. PlacementOptions,
  134. DebugMode);
  135. _placeAtComponents.Add(instance.GetComponent<PlaceAtLocation>());
  136. }
  137. }
  138. // Update is called once per frame
  139. void LoadXmlFile()
  140. {
  141. var xmlString = XmlDataFile.text;
  142. Debug.Log(xmlString);
  143. XmlDocument xmlDoc = new XmlDocument();
  144. try
  145. {
  146. xmlDoc.LoadXml(xmlString);
  147. }
  148. catch (XmlException e)
  149. {
  150. Debug.LogError("[ARLocation#WebMapLoader]: Failed to parse XML file: " + e.Message);
  151. }
  152. var root = xmlDoc.FirstChild;
  153. var nodes = root.ChildNodes;
  154. foreach (XmlNode node in nodes)
  155. {
  156. Debug.Log(node.InnerXml);
  157. Debug.Log(node["id"].InnerText);
  158. int id = int.Parse(node["id"].InnerText);
  159. double lat = double.Parse(node["lat"].InnerText, CultureInfo.InvariantCulture);
  160. double lng = double.Parse(node["lng"].InnerText, CultureInfo.InvariantCulture);
  161. double altitude = double.Parse(node["altitude"].InnerText, CultureInfo.InvariantCulture);
  162. string altitudeMode = node["altitudeMode"].InnerText;
  163. string name = node["name"].InnerText;
  164. string meshId = node["meshId"].InnerText;
  165. float movementSmoothing = float.Parse(node["movementSmoothing"].InnerText, CultureInfo.InvariantCulture);
  166. int maxNumberOfLocationUpdates = int.Parse(node["maxNumberOfLocationUpdates"].InnerText);
  167. bool useMovingAverage = bool.Parse(node["useMovingAverage"].InnerText);
  168. bool hideObjectUtilItIsPlaced = bool.Parse(node["hideObjectUtilItIsPlaced"].InnerText);
  169. DataEntry entry = new DataEntry()
  170. {
  171. id = id,
  172. lat = lat,
  173. lng = lng,
  174. altitudeMode = altitudeMode,
  175. altitude = altitude,
  176. name = name,
  177. meshId = meshId,
  178. movementSmoothing = movementSmoothing,
  179. maxNumberOfLocationUpdates = maxNumberOfLocationUpdates,
  180. useMovingAverage = useMovingAverage,
  181. hideObjectUtilItIsPlaced = hideObjectUtilItIsPlaced
  182. };
  183. _dataEntries.Add(entry);
  184. Debug.Log($"{id}, {lat}, {lng}, {altitude}, {altitudeMode}, {name}, {meshId}, {movementSmoothing}, {maxNumberOfLocationUpdates}, {useMovingAverage}, {hideObjectUtilItIsPlaced}");
  185. }
  186. }
  187. }
  188. }