9 using UnityEngine.Events;
11 #if !ARGPS_USE_VUFORIA
12 using UnityEngine.XR.ARFoundation;
25 [RequireComponent(typeof(ARLocationOrientation))]
26 [DisallowMultipleComponent]
27 [AddComponentMenu(
"AR+GPS/AR Location Manager")]
28 [HelpURL(
"https://http://docs.unity-ar-gps-location.com/guide/#arlocationmanager")]
31 [Tooltip(
"The AR Camera that will be used for rendering the AR content. If this is not set, the camera tagger as 'MainCamera' will be used." +
32 " Make sure either this is set, or a camera is tagged as 'MainCamera', or an error will be thrown.")]
35 [Tooltip(
"If true, wait until the AR Tracking starts to start with location and orientation updates and object placement.")]
36 public bool WaitForARTrackingToStart =
true;
38 [Tooltip(
"If true, every time the AR tracking is lost and regained, the AR+GPS system is restarted, repositioning all the objects.")]
39 public bool RestartWhenARTrackingIsRestored;
41 [Tooltip(
"If true, the manager will set 'Application.targetFrameRate' to 60." )]
42 public bool SetTargetFrameRateTo60Mhz =
true;
45 [Tooltip(
"When debug mode is enabled, this component will print relevant messages to the console. Filter by 'ARLocationManager' in the log output to see the messages.")]
46 public bool DebugMode;
48 [Header(
"AR Session Events")]
49 public UnityEvent OnTrackingStarted =
new UnityEvent();
50 public UnityEvent OnTrackingLost =
new UnityEvent();
51 public UnityEvent OnTrackingRestored =
new UnityEvent();
70 private Action onARTrackingStartedAction;
71 private GameObject groundHeightDummy;
76 public override void Awake()
80 if (SetTargetFrameRateTo60Mhz)
82 Logger.LogFromMethod(
"ARLocationManager",
"Awake",
"Setting 'Application.targetFrameRate' to 60", DebugMode);
83 Application.targetFrameRate = 60;
90 throw new NullReferenceException(
"[AR+GPS][ARLocationManager#Start]: Missing Camera. " +
91 "Either set the 'Camera' property to the AR Camera, or tag it as a 'MainCamera'.");
97 arLocationOrientation = GetComponent<ARLocationOrientation>();
100 if (WaitForARTrackingToStart)
102 arLocationProvider.Mute();
105 #if !ARGPS_USE_VUFORIA
106 var arSession = FindObjectOfType<ARSession>();
110 throw new NullReferenceException(
"[AR+GPS][ARLocationManager#Start]: No ARSession found in the scene!");
124 groundHeightDummy =
new GameObject(
"ARLocationGroundHeight");
125 groundHeightDummy.transform.SetParent(
MainCamera.transform);
126 groundHeightDummy.transform.localPosition =
new Vector3();
127 groundHeight = groundHeightDummy.AddComponent<GroundHeight>();
130 private void ARTrackingLostCallback()
132 Logger.LogFromMethod(
"ARLocationManager",
"ARTrackingLostCallback",
"'ARTrackingLost' event received.", DebugMode);
133 OnTrackingLost?.Invoke();
136 private void ARTrackingRestoredCallback()
138 Logger.LogFromMethod(
"ARLocationManager",
"ARTrackingRestoredCallback",
"'ARTrackingRestore' event received.", DebugMode);
140 if (RestartWhenARTrackingIsRestored)
142 Logger.LogFromMethod(
"ARLocationManager",
"ARTrackingRestoredCallback",
"'RestartWhenARTrackingIsRestored' is enabled; restarting.", DebugMode);
146 OnTrackingRestored?.Invoke();
149 private void ARTrackingStartedCallback()
151 Logger.LogFromMethod(
"ARLocationManager",
"ARTrackingStartedCallback",
"'OnARTrackingStarted' event received.", DebugMode);
153 if (WaitForARTrackingToStart)
155 arLocationProvider.Unmute();
158 OnTrackingStarted?.Invoke();
159 onARTrackingStartedAction?.Invoke();
168 Logger.LogFromMethod(
"ARLocationManager",
"ResetARSession",
"Resetting the AR Session...", DebugMode);
172 Logger.LogFromMethod(
"ARLocationManager",
"ResetARSession",
"ARSession restarted. Resetting AR+GPS location...", DebugMode);
183 Logger.LogFromMethod(
"ARLocationManager",
"Restart",
"Resetting AR+GPS location...", DebugMode);
185 arLocationOrientation.
Restart();
188 Logger.LogFromMethod(
"ARLocationManager",
"Restart",
"Done.", DebugMode);
217 onARTrackingStartedAction += o;