Files
PrinceOfGlory/Assets/Scripts/XRCore/Base/Singleton.cs
kridoo 6e91a0c7f0 111
2025-09-15 17:32:08 +08:00

35 lines
894 B
C#

namespace BigSpace.XRCore.Base
{
public abstract class Singleton<T> where T : class, new()
{
private static readonly object _lock = new object();
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
_instance = new T();
}
}
}
return _instance;
}
}
protected Singleton()
{
// 防止通过反射创建多个实例
if (_instance != null)
{
throw new System.Exception($"An instance of {typeof(T)} already exists!");
}
}
}
}