AR + GPS Location  3.0.0
All Classes Namespaces Functions Variables Properties Events Pages
ARLocationManager.cs
1 // Copyright (C) 2018 Daniel Fortes <daniel.fortes@gmail.com>
2 // All rights reserved.
3 //
4 // See LICENSE.TXT for more info
5 
6 using System;
7 using ARLocation.Session;
8 using UnityEngine;
9 using UnityEngine.Events;
10 
11 #if !ARGPS_USE_VUFORIA
12 using UnityEngine.XR.ARFoundation;
13 #endif
14 
15 namespace ARLocation
16 {
17  using Utils;
18 
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")]
29  public class ARLocationManager : Singleton<ARLocationManager>
30  {
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.")]
33  public Camera Camera;
34 
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;
37 
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;
40 
41  [Tooltip("If true, the manager will set 'Application.targetFrameRate' to 60." )]
42  public bool SetTargetFrameRateTo60Mhz = true;
43 
44  [Header("Debug")]
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;
47 
48  [Header("AR Session Events")]
49  public UnityEvent OnTrackingStarted = new UnityEvent();
50  public UnityEvent OnTrackingLost = new UnityEvent();
51  public UnityEvent OnTrackingRestored = new UnityEvent();
52 
56  public IARSessionManager SessionManager { get; private set; }
57 
61  public Camera MainCamera { get; private set; }
62 
66  public float CurrentGroundY => groundHeight ? groundHeight.CurrentGroundY : -ARLocation.Config.InitialGroundHeightGuess;
67 
68  private ARLocationOrientation arLocationOrientation;
69  private ARLocationProvider arLocationProvider;
70  private Action onARTrackingStartedAction;
71  private GameObject groundHeightDummy;
72  private GroundHeight groundHeight;
73 
74 
75 
76  public override void Awake()
77  {
78  base.Awake();
79 
80  if (SetTargetFrameRateTo60Mhz)
81  {
82  Logger.LogFromMethod("ARLocationManager", "Awake", "Setting 'Application.targetFrameRate' to 60", DebugMode);
83  Application.targetFrameRate = 60;
84  }
85 
86  MainCamera = Camera ? Camera : Camera.main;
87 
88  if (MainCamera == null)
89  {
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'.");
92  }
93  }
94 
95  private void Start()
96  {
97  arLocationOrientation = GetComponent<ARLocationOrientation>();
98  arLocationProvider = ARLocationProvider.Instance;
99 
100  if (WaitForARTrackingToStart)
101  {
102  arLocationProvider.Mute();
103  }
104 
105  #if !ARGPS_USE_VUFORIA
106  var arSession = FindObjectOfType<ARSession>();
107 
108  if (!arSession)
109  {
110  throw new NullReferenceException("[AR+GPS][ARLocationManager#Start]: No ARSession found in the scene!");
111  }
112 
114  #else
115  SessionManager = new VuforiaSessionManager();
116  #endif
117 
118  SessionManager.DebugMode = DebugMode;
119 
120  SessionManager.OnARTrackingStarted(ARTrackingStartedCallback);
121  SessionManager.OnARTrackingRestored(ARTrackingRestoredCallback);
122  SessionManager.OnARTrackingLost(ARTrackingLostCallback);
123 
124  groundHeightDummy = new GameObject("ARLocationGroundHeight");
125  groundHeightDummy.transform.SetParent(MainCamera.transform);
126  groundHeightDummy.transform.localPosition = new Vector3();
127  groundHeight = groundHeightDummy.AddComponent<GroundHeight>();
128  }
129 
130  private void ARTrackingLostCallback()
131  {
132  Logger.LogFromMethod("ARLocationManager", "ARTrackingLostCallback", "'ARTrackingLost' event received.", DebugMode);
133  OnTrackingLost?.Invoke();
134  }
135 
136  private void ARTrackingRestoredCallback()
137  {
138  Logger.LogFromMethod("ARLocationManager", "ARTrackingRestoredCallback", "'ARTrackingRestore' event received.", DebugMode);
139 
140  if (RestartWhenARTrackingIsRestored)
141  {
142  Logger.LogFromMethod("ARLocationManager", "ARTrackingRestoredCallback", "'RestartWhenARTrackingIsRestored' is enabled; restarting.", DebugMode);
143  Restart();
144  }
145 
146  OnTrackingRestored?.Invoke();
147  }
148 
149  private void ARTrackingStartedCallback()
150  {
151  Logger.LogFromMethod("ARLocationManager", "ARTrackingStartedCallback", "'OnARTrackingStarted' event received.", DebugMode);
152 
153  if (WaitForARTrackingToStart)
154  {
155  arLocationProvider.Unmute();
156  }
157 
158  OnTrackingStarted?.Invoke();
159  onARTrackingStartedAction?.Invoke();
160  }
161 
166  public void ResetARSession(Action cb = null)
167  {
168  Logger.LogFromMethod("ARLocationManager", "ResetARSession", "Resetting the AR Session...", DebugMode);
169 
170  SessionManager?.Reset(() =>
171  {
172  Logger.LogFromMethod("ARLocationManager", "ResetARSession", "ARSession restarted. Resetting AR+GPS location...", DebugMode);
173  Restart();
174  cb?.Invoke();
175  });
176  }
177 
181  public void Restart()
182  {
183  Logger.LogFromMethod("ARLocationManager", "Restart", "Resetting AR+GPS location...", DebugMode);
184 
185  arLocationOrientation.Restart();
186  arLocationProvider.Restart();
187 
188  Logger.LogFromMethod("ARLocationManager", "Restart", "Done.", DebugMode);
189  }
190 
195  public string GetARSessionInfoString()
196  {
197  return SessionManager != null ? SessionManager.GetSessionInfoString() : "None";
198  }
199 
205  {
206  return SessionManager != null ? SessionManager.GetProviderString() : "None";
207  }
208 
213  public void OnARTrackingStarted(Action o)
214  {
215  if (SessionManager == null)
216  {
217  onARTrackingStartedAction += o;
218  }
219  else
220  {
221  SessionManager.OnARTrackingStarted(o);
222  }
223  }
224 
229  public void OnARTrackingRestored(Action callback)
230  {
231  SessionManager?.OnARTrackingRestored(callback);
232  }
233 
238  public void OnARTrackingLost(Action callback)
239  {
240  SessionManager?.OnARTrackingLost(callback);
241  }
242  }
243 }
ARLocation.Session.ARFoundationSessionManager
Definition: ARFoundationSessionManager.cs:43
ARLocation.Session.IARSessionManager
Definition: IARSessionManager.cs:6
ARLocation.ARLocationProvider
Definition: ARLocationProvider.cs:17
ARLocation.ARLocationManager
This Component manages all positioned GameObjects, synchronizing their world position in the scene wi...
Definition: ARLocationManager.cs:30
ARLocation.Utils.Singleton.Instance
static T Instance
Access singleton instance through this propriety.
Definition: Singleton.cs:18
ARLocation.ARLocationManager.OnARTrackingLost
void OnARTrackingLost(Action callback)
Add a event listener for when the AR Tracking is lost.
Definition: ARLocationManager.cs:238
ARLocation.Session
Definition: ARFoundationSessionManager.cs:40
ARLocation.ARLocationManager.SessionManager
IARSessionManager SessionManager
The instance of the 'IARSessionManager'. Handles the interface with the underlying AR session (i....
Definition: ARLocationManager.cs:56
ARLocation.ARLocationManager.OnARTrackingRestored
void OnARTrackingRestored(Action callback)
Add a event listener for when the AR Tracking regained after it was lost.
Definition: ARLocationManager.cs:229
ARLocation.ARLocationManager.GetARSessionInfoString
string GetARSessionInfoString()
Returns a string describing the current AR session tracking status
Definition: ARLocationManager.cs:195
ARLocation.ARLocationOrientation.Restart
void Restart()
Restarts the orientation tracking.
Definition: ARLocationOrientation.cs:88
ARLocation.Utils.Singleton
Definition: Singleton.cs:8
ARLocation.ARLocationManager.ResetARSession
void ResetARSession(Action cb=null)
This will reset the AR Session and the AR+GPS system, repositioning all objects.
Definition: ARLocationManager.cs:166
ARLocation.ARLocationManager.CurrentGroundY
float CurrentGroundY
Returns the Y world-coordinate of the detected plane which is nearest to the user/camera.
Definition: ARLocationManager.cs:66
ARLocation.ARLocationManager.MainCamera
Camera MainCamera
The 'MainCamera' that is being used for rendering the AR content.
Definition: ARLocationManager.cs:61
ARLocation
Definition: ARLocationConfigInspector.cs:7
ARLocation.ARLocationManager.Restart
void Restart()
This will restart the AR+GPS system, repositioning all the objects.
Definition: ARLocationManager.cs:181
ARLocation.ARLocationOrientation
This component should be placed on the "ARLocationRoot" GameObject (which should be a child of the "A...
Definition: ARLocationOrientation.cs:19
ARLocation.ARLocationProvider.Restart
void Restart()
Resets the location provider.
Definition: ARLocationProvider.cs:260
ARLocation.ARLocationManager.GetARSessionProviderString
string GetARSessionProviderString()
Returns a string describing the current AR Session provider, e.g., AR Foundation or Vuforia.
Definition: ARLocationManager.cs:204
ARLocation.ARLocationManager.OnARTrackingStarted
void OnARTrackingStarted(Action o)
Add a event listener for when the AR Tracking starts.
Definition: ARLocationManager.cs:213
ARLocation.GroundHeight
This component will change the Y component of the GameObject's position, so that it is set to the lev...
Definition: GroundHeight.cs:24