feat: 受击音效更新类别控制
This commit is contained in:
38
UIFrame/Singleton.cs
Normal file
38
UIFrame/Singleton.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
|
||||
namespace UIFrame
|
||||
{
|
||||
public abstract class Singleton<T> where T : class
|
||||
{
|
||||
private static T? _instance;
|
||||
private static readonly object _lock = new object(); // 用于多线程安全
|
||||
public static T? Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
// 在多线程环境下,确保只有一个线程能够创建实例
|
||||
lock (_lock)
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
|
||||
_instance = Activator.CreateInstance(typeof(T), true) as T;
|
||||
if (_instance == null)
|
||||
{
|
||||
throw new InvalidOperationException($"无法为类型 {typeof(T).Name} 创建单例实例。" +
|
||||
"请确保它有一个私有的无参构造函数。");
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
protected Singleton()
|
||||
{
|
||||
if (_instance != null)
|
||||
{
|
||||
throw new InvalidOperationException($"试图创建第二个单例实例 {typeof(T).Name}。请通过 Singleton<{typeof(T).Name}>.Instance 访问。");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user