SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
6.0 KiB

4 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Xml;
  5. using UnityEngine;
  6. namespace ARLocation {
  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. return AltitudeMode.GroundRelative;
  26. } else if (altitudeMode == "DeviceRelative") {
  27. return AltitudeMode.DeviceRelative;
  28. } else if (altitudeMode == "Absolute") {
  29. return AltitudeMode.Absolute;
  30. } else {
  31. return AltitudeMode.Ignore;
  32. }
  33. }
  34. }
  35. /// <summary>
  36. /// The `PrefabDatabase` ScriptableObject, containing a dictionary of Prefabs with a string ID.
  37. /// </summary>
  38. public PrefabDatabase PrefabDatabase;
  39. /// <summary>
  40. /// The XML data file download from the Web Map Editor (htttps://editor.unity-ar-gps-location.com)
  41. /// </summary>
  42. public TextAsset XmlDataFile;
  43. /// <summary>
  44. /// If true, enable DebugMode on the `PlaceAtLocation` generated instances.
  45. /// </summary>
  46. public bool DebugMode;
  47. private List<DataEntry> _dataEntries = new List<DataEntry>();
  48. private List<GameObject> _stages = new List<GameObject>();
  49. private List<PlaceAtLocation> _placeAtComponents = new List<PlaceAtLocation>();
  50. // Start is called before the first frame update
  51. void Start()
  52. {
  53. LoadXmlFile();
  54. BuildGameObjects();
  55. }
  56. void BuildGameObjects()
  57. {
  58. foreach (var entry in _dataEntries)
  59. {
  60. var Prefab = PrefabDatabase.GetEntryById(entry.meshId);
  61. if (!Prefab)
  62. {
  63. Debug.LogWarning($"[ARLocation#WebMapLoader]: Prefab {entry.meshId} not found.");
  64. continue;
  65. }
  66. var PlacementOptions = new PlaceAtLocation.PlaceAtOptions()
  67. {
  68. MovementSmoothing = entry.movementSmoothing,
  69. MaxNumberOfLocationUpdates = entry.maxNumberOfLocationUpdates,
  70. UseMovingAverage = entry.useMovingAverage,
  71. HideObjectUntilItIsPlaced = entry.hideObjectUtilItIsPlaced
  72. };
  73. var location = new Location()
  74. {
  75. Latitude = entry.lat,
  76. Longitude = entry.lng,
  77. Altitude = entry.altitude,
  78. AltitudeMode = entry.getAltitudeMode(),
  79. Label = entry.name
  80. };
  81. var instance = PlaceAtLocation.CreatePlacedInstance(Prefab,
  82. location,
  83. PlacementOptions,
  84. DebugMode);
  85. _stages.Add(instance);
  86. }
  87. }
  88. // Update is called once per frame
  89. void LoadXmlFile()
  90. {
  91. var xmlString = XmlDataFile.text;
  92. Debug.Log(xmlString);
  93. XmlDocument xmlDoc = new XmlDocument();
  94. try {
  95. xmlDoc.LoadXml(xmlString);
  96. } catch(XmlException e) {
  97. Debug.LogError("[ARLocation#WebMapLoader]: Failed to parse XML file: " + e.Message);
  98. }
  99. var root = xmlDoc.FirstChild;
  100. var nodes = root.ChildNodes;
  101. foreach (XmlNode node in nodes)
  102. {
  103. Debug.Log(node.InnerXml);
  104. Debug.Log(node["id"].InnerText);
  105. int id = int.Parse(node["id"].InnerText);
  106. double lat = double.Parse(node["lat"].InnerText, CultureInfo.InvariantCulture);
  107. double lng = double.Parse(node["lng"].InnerText, CultureInfo.InvariantCulture);
  108. double altitude = double.Parse(node["altitude"].InnerText, CultureInfo.InvariantCulture);
  109. string altitudeMode = node["altitudeMode"].InnerText;
  110. string name = node["name"].InnerText;
  111. string meshId = node["meshId"].InnerText;
  112. float movementSmoothing = float.Parse(node["movementSmoothing"].InnerText, CultureInfo.InvariantCulture);
  113. int maxNumberOfLocationUpdates = int.Parse(node["maxNumberOfLocationUpdates"].InnerText);
  114. bool useMovingAverage = bool.Parse(node["useMovingAverage"].InnerText);
  115. bool hideObjectUtilItIsPlaced = bool.Parse(node["hideObjectUtilItIsPlaced"].InnerText);
  116. DataEntry entry = new DataEntry() {
  117. id = id,
  118. lat = lat,
  119. lng = lng,
  120. altitudeMode = altitudeMode,
  121. altitude = altitude,
  122. name = name,
  123. meshId = meshId,
  124. movementSmoothing = movementSmoothing,
  125. maxNumberOfLocationUpdates = maxNumberOfLocationUpdates,
  126. useMovingAverage =useMovingAverage,
  127. hideObjectUtilItIsPlaced = hideObjectUtilItIsPlaced };
  128. _dataEntries.Add(entry);
  129. Debug.Log($"{id}, {lat}, {lng}, {altitude}, {altitudeMode}, {name}, {meshId}, {movementSmoothing}, {maxNumberOfLocationUpdates}, {useMovingAverage}, {hideObjectUtilItIsPlaced}");
  130. }
  131. }
  132. }
  133. }