AR + GPS Location  3.0.0
All Classes Namespaces Functions Variables Properties Events Pages
WebMapLoader.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using System.Globalization;
4 using System.Xml;
5 using UnityEngine;
6 
7 namespace ARLocation {
8  public class WebMapLoader : MonoBehaviour
9  {
10 
11  public class DataEntry
12  {
13  public int id;
14  public double lat;
15  public double lng;
16  public double altitude;
17  public string altitudeMode;
18  public string name;
19  public string meshId;
20  public float movementSmoothing;
21  public int maxNumberOfLocationUpdates;
22  public bool useMovingAverage;
23  public bool hideObjectUtilItIsPlaced;
24 
25  public AltitudeMode getAltitudeMode()
26  {
27  if (altitudeMode == "GroundRelative") {
28  return AltitudeMode.GroundRelative;
29  } else if (altitudeMode == "DeviceRelative") {
30  return AltitudeMode.DeviceRelative;
31  } else if (altitudeMode == "Absolute") {
32  return AltitudeMode.Absolute;
33  } else {
34  return AltitudeMode.Ignore;
35  }
36  }
37  }
38 
43 
47  public TextAsset XmlDataFile;
48 
52  public bool DebugMode;
53 
54  private List<DataEntry> _dataEntries = new List<DataEntry>();
55  private List<GameObject> _stages = new List<GameObject>();
56  private List<PlaceAtLocation> _placeAtComponents = new List<PlaceAtLocation>();
57 
58  // Start is called before the first frame update
59  void Start()
60  {
61  LoadXmlFile();
62  BuildGameObjects();
63  }
64 
65  void BuildGameObjects()
66  {
67  foreach (var entry in _dataEntries)
68  {
69  var Prefab = PrefabDatabase.GetEntryById(entry.meshId);
70 
71  if (!Prefab)
72  {
73  Debug.LogWarning($"[ARLocation#WebMapLoader]: Prefab {entry.meshId} not found.");
74  continue;
75  }
76 
77  var PlacementOptions = new PlaceAtLocation.PlaceAtOptions()
78  {
79  MovementSmoothing = entry.movementSmoothing,
80  MaxNumberOfLocationUpdates = entry.maxNumberOfLocationUpdates,
81  UseMovingAverage = entry.useMovingAverage,
82  HideObjectUntilItIsPlaced = entry.hideObjectUtilItIsPlaced
83  };
84 
85  var location = new Location()
86  {
87  Latitude = entry.lat,
88  Longitude = entry.lng,
89  Altitude = entry.altitude,
90  AltitudeMode = entry.getAltitudeMode(),
91  Label = entry.name
92  };
93 
94  var instance = PlaceAtLocation.CreatePlacedInstance(Prefab,
95  location,
96  PlacementOptions,
97  DebugMode);
98 
99  _stages.Add(instance);
100  }
101  }
102 
103  // Update is called once per frame
104  void LoadXmlFile()
105  {
106  var xmlString = XmlDataFile.text;
107 
108  Debug.Log(xmlString);
109 
110  XmlDocument xmlDoc = new XmlDocument();
111 
112  try {
113  xmlDoc.LoadXml(xmlString);
114  } catch(XmlException e) {
115  Debug.LogError("[ARLocation#WebMapLoader]: Failed to parse XML file: " + e.Message);
116  }
117 
118  var root = xmlDoc.FirstChild;
119  var nodes = root.ChildNodes;
120  foreach (XmlNode node in nodes)
121  {
122  Debug.Log(node.InnerXml);
123  Debug.Log(node["id"].InnerText);
124 
125  int id = int.Parse(node["id"].InnerText);
126  double lat = double.Parse(node["lat"].InnerText, CultureInfo.InvariantCulture);
127  double lng = double.Parse(node["lng"].InnerText, CultureInfo.InvariantCulture);
128  double altitude = double.Parse(node["altitude"].InnerText, CultureInfo.InvariantCulture);
129  string altitudeMode = node["altitudeMode"].InnerText;
130  string name = node["name"].InnerText;
131  string meshId = node["meshId"].InnerText;
132  float movementSmoothing = float.Parse(node["movementSmoothing"].InnerText, CultureInfo.InvariantCulture);
133  int maxNumberOfLocationUpdates = int.Parse(node["maxNumberOfLocationUpdates"].InnerText);
134  bool useMovingAverage = bool.Parse(node["useMovingAverage"].InnerText);
135  bool hideObjectUtilItIsPlaced = bool.Parse(node["hideObjectUtilItIsPlaced"].InnerText);
136 
137  DataEntry entry = new DataEntry() {
138  id = id,
139  lat = lat,
140  lng = lng,
141  altitudeMode = altitudeMode,
142  altitude = altitude,
143  name = name,
144  meshId = meshId,
145  movementSmoothing = movementSmoothing,
146  maxNumberOfLocationUpdates = maxNumberOfLocationUpdates,
147  useMovingAverage =useMovingAverage,
148  hideObjectUtilItIsPlaced = hideObjectUtilItIsPlaced };
149 
150  _dataEntries.Add(entry);
151 
152  Debug.Log($"{id}, {lat}, {lng}, {altitude}, {altitudeMode}, {name}, {meshId}, {movementSmoothing}, {maxNumberOfLocationUpdates}, {useMovingAverage}, {hideObjectUtilItIsPlaced}");
153  }
154  }
155  }
156 }
ARLocation.WebMapLoader
Definition: WebMapLoader.cs:9
ARLocation.WebMapLoader.DebugMode
bool DebugMode
If true, enable DebugMode on the PlaceAtLocation generated instances.
Definition: WebMapLoader.cs:52
ARLocation.WebMapLoader.XmlDataFile
TextAsset XmlDataFile
The XML data file download from the Web Map Editor (htttps://editor.unity-ar-gps-location....
Definition: WebMapLoader.cs:47
ARLocation.WebMapLoader.DataEntry
Definition: WebMapLoader.cs:12
ARLocation.PrefabDatabase
Definition: PrefabDatabase.cs:8
ARLocation.WebMapLoader.PrefabDatabase
PrefabDatabase PrefabDatabase
The PrefabDatabase ScriptableObject, containing a dictionary of Prefabs with a string ID.
Definition: WebMapLoader.cs:42
ARLocation
Definition: ARLocationConfigInspector.cs:7