在Unity遊戲開發中,寫腳本經常會用到單例模式,所以就寫了一個公用的泛型單例類方便使用,也爲了以後開發偷點懶
public class Singleton<T> where T : class, new()
{
public Singleton()
{
}
private static T _instance;
private static readonly object _synclock = new object();
public static T Instance
{
get
{
if (_instance == null)
{
lock (_synclock)
{
if (_instance == null)
{
_instance = new T();
}
}
}
return _instance;
}
set { _instance = value; }
}
}