AR + GPS Location  3.0.0
All Classes Namespaces Functions Variables Properties Events Pages
Singleton.cs
1 using UnityEngine;
2 // ReSharper disable StaticMemberInGenericType
3 // ReSharper disable InconsistentNaming
4 
5 namespace ARLocation.Utils
6 {
7  public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
8  {
9  // Check to see if we're about to be destroyed.
10  private static bool m_ShuttingDown;
11  private static object m_Lock = new object();
12  private static T m_Instance;
13 
17  public static T Instance
18  {
19  get
20  {
21  if (m_ShuttingDown)
22  {
23  Debug.LogWarning("[Singleton] Instance '" + typeof(T) +
24  "' already destroyed. Returning null.");
25  return null;
26  }
27 
28  lock (m_Lock)
29  {
30  if (m_Instance == null)
31  {
32  // Search for existing instance.
33  m_Instance = (T)FindObjectOfType(typeof(T));
34 
35  // Create new instance if one doesn't already exist.
36  if (m_Instance == null)
37  {
38  // Need to create a new GameObject to attach the singleton to.
39  var singletonObject = new GameObject();
40  m_Instance = singletonObject.AddComponent<T>();
41  singletonObject.name = typeof(T) + " (Singleton)";
42 
43  // Make instance persistent.
44  DontDestroyOnLoad(singletonObject);
45  }
46  }
47 
48  return m_Instance;
49  }
50  }
51  }
52 
53  public virtual void Awake()
54  {
55  m_ShuttingDown = false;
56  }
57 
58 
59  private void OnApplicationQuit()
60  {
61  m_ShuttingDown = true;
62  }
63 
64 
65  private void OnDestroy()
66  {
67  m_ShuttingDown = true;
68  }
69  }
70 }
ARLocation.Utils.Singleton.Instance
static T Instance
Access singleton instance through this propriety.
Definition: Singleton.cs:18
ARLocation.Utils
Definition: CreatePointOfInterestTextMeshes.cs:9
ARLocation.Utils.Singleton
Definition: Singleton.cs:8