2 using System.Collections;
4 using UnityEngine.Events;
5 using UnityEngine.Serialization;
13 [AddComponentMenu(
"AR+GPS/AR Location Provider")]
14 [HelpURL(
"https://http://docs.unity-ar-gps-location.com/guide/#arlocationprovider")]
15 [DisallowMultipleComponent]
26 [FormerlySerializedAs(
"LocationUpdateSettings")]
27 [Tooltip(
"The options for the Location Provider.")]
28 [Header(
"Update Settings")]
31 [Tooltip(
"The data of mock location. If present, overrides the Mock Location above.")]
35 [Tooltip(
"The maximum wait time to wait for location initialization.")]
36 [Header(
"Initialization")]
37 public uint MaxWaitTime = 200;
39 [Tooltip(
"Wait this many seconds before starting location services. Useful when using Unity Remote.")]
40 public uint StartUpDelay;
43 [Tooltip(
"When debug mode is enabled, this component will print relevant messages to the console. Filter by 'ARLocationProvider' in the log output to see the messages.")]
44 public bool DebugMode;
48 [Tooltip(
"Called after the first location is read.")]
51 [Tooltip(
"Called after each new location update.")]
54 [Tooltip(
"Called after each new raw device GPS data is obtained.")]
57 [Tooltip(
"Called after each new compass update.")]
111 private int measurementCount;
114 public event LocationUpdatedDelegate OnLocationUpdatedDelegate;
115 public event CompassUpdateDelegate OnCompassUpdateDelegate;
116 public event Action OnRestartDelegate;
118 private Vector3 cameraPositionAtLastUpdate;
119 public override void Awake()
126 if (MockLocationData !=
null)
128 Logger.LogFromMethod(
"ARLocationProvider",
"Awake", $
"Using mock location {MockLocationData}", DebugMode);
131 #elif ARGPS_CUSTOM_PROVIDER
134 Provider =
new ARGpsCustomLocationProvider();
138 Logger.LogFromMethod(
"ARLocationProvider",
"Awake",
": Using provider " +
Provider.Name, DebugMode);
143 private void InitProviderEventListeners()
145 Logger.LogFromMethod(
"ARLocationProvider",
"InitProviderEventListeners",
"Initializing location provider listeners.", DebugMode);
147 Provider.LocationUpdated += Provider_LocationUpdated;
148 Provider.CompassUpdated += Provider_CompassUpdated;
149 Provider.LocationUpdatedRaw += ProviderOnLocationUpdatedRaw;
151 Provider.OnEnabled(OnProviderEnabledDelegate);
159 private void ProviderOnLocationUpdatedRaw(LocationReading currentLocation, LocationReading lastLocation)
161 OnRawLocationUpdated?.Invoke(currentLocation.ToLocation());
164 private void OnProviderEnabledDelegate()
166 Logger.LogFromMethod(
"ARLocationProvider",
"OnProviderEnabledDelegate",
"Provider enabled; emitting 'OnEnabled' event.", DebugMode);
172 InitProviderEventListeners();
174 Provider.Options = LocationProviderSettings;
176 Logger.LogFromMethod(
"ARLocationProvider",
"Start",
"Starting the location provider", DebugMode);
177 yield
return StartCoroutine(
Provider.Start(MaxWaitTime, StartUpDelay));
182 Logger.LogFromMethod(
"ARLocationProvider",
"Mute",
"Muting ARLocationProvider.", DebugMode);
186 public void Unmute(
bool emit =
true)
188 Logger.LogFromMethod(
"ARLocationProvider",
"Mute",
"Un-muting ARLocationProvider.", DebugMode);
193 private void Provider_CompassUpdated(HeadingReading heading, HeadingReading lastReading)
197 OnCompassUpdateDelegate?.Invoke(heading, lastReading);
198 OnCompassUpdated?.Invoke(heading);
201 private void Provider_LocationUpdated(LocationReading currentLocation, LocationReading lastLocation)
207 if ((LocationProviderSettings.MaxNumberOfUpdates > 0) && (measurementCount >= LocationProviderSettings.MaxNumberOfUpdates))
212 Logger.LogFromMethod(
"ARLocationProvider",
"Provider_LocationUpdated",$
"New location {currentLocation}.", DebugMode);
214 cameraPositionAtLastUpdate = ARLocationManager.Instance.MainCamera.transform.position;
215 OnLocationUpdatedDelegate?.Invoke(currentLocation, lastLocation);
216 OnLocationUpdated?.Invoke(currentLocation.ToLocation());
225 Logger.LogFromMethod(
"ARLocationProvider",
"ForceLocationUpdate",
"Emitting a forced location update", DebugMode);
244 Logger.LogFromMethod(
"ARLocationProvider",
"Pause",
"Pausing the location provider.", DebugMode);
253 Logger.LogFromMethod(
"ARLocationProvider",
"Resume",
"Resuming the location provider.", DebugMode);
262 Logger.LogFromMethod(
"ARLocationProvider",
"Restart",
"Restarting the location provider.", DebugMode);
264 OnRestartDelegate?.Invoke();
283 OnLocationUpdatedDelegate += locationUpdatedDelegate;
286 public void OnProviderRestartEvent(Action del)
288 OnRestartDelegate += del;
297 OnCompassUpdateDelegate += compassUpdateDelegate;