You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
C#
39 lines
1.1 KiB
C#
2 months ago
|
// Singleton pattern: http://wiki.unity3d.com/index.php/Singleton
|
||
|
using UnityEngine;
|
||
|
|
||
|
public class SCC_Singleton<T> : MonoBehaviour where T : MonoBehaviour {
|
||
|
|
||
|
private static T m_Instance;
|
||
|
private static readonly object m_Lock = new object();
|
||
|
|
||
|
public static T Instance {
|
||
|
|
||
|
get {
|
||
|
|
||
|
lock (m_Lock) {
|
||
|
|
||
|
if (m_Instance != null)
|
||
|
return m_Instance;
|
||
|
|
||
|
// Search for existing instance.
|
||
|
m_Instance = (T)FindObjectOfType(typeof(T));
|
||
|
// Create new instance if one doesn't already exist.
|
||
|
if (m_Instance != null) return m_Instance;
|
||
|
// Need to create a new GameObject to attach the singleton to.
|
||
|
var singletonObject = new GameObject();
|
||
|
m_Instance = singletonObject.AddComponent<T>();
|
||
|
singletonObject.name = typeof(T).ToString();
|
||
|
|
||
|
// Make instance persistent.
|
||
|
//DontDestroyOnLoad(singletonObject);
|
||
|
|
||
|
return m_Instance;
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|