7 public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
10 private static bool m_ShuttingDown;
11 private static object m_Lock =
new object();
12 private static T m_Instance;
23 Debug.LogWarning(
"[Singleton] Instance '" + typeof(T) +
24 "' already destroyed. Returning null.");
30 if (m_Instance ==
null)
33 m_Instance = (T)FindObjectOfType(typeof(T));
36 if (m_Instance ==
null)
39 var singletonObject =
new GameObject();
40 m_Instance = singletonObject.AddComponent<T>();
41 singletonObject.name = typeof(T) +
" (Singleton)";
44 DontDestroyOnLoad(singletonObject);
53 public virtual void Awake()
55 m_ShuttingDown =
false;
59 private void OnApplicationQuit()
61 m_ShuttingDown =
true;
65 private void OnDestroy()
67 m_ShuttingDown =
true;