Files
BlueArchiveMiniGame/Assets/Scripts/Runtime/Singleton.cs

24 lines
477 B
C#
Raw Normal View History

2025-09-29 07:45:07 +08:00
2025-09-17 18:56:28 +08:00
using UnityEngine;
2025-09-29 07:45:07 +08:00
public class Singleton<T> where T:class,new()
2025-09-17 18:56:28 +08:00
{
2025-09-29 07:45:07 +08:00
static T _inst;
static readonly object _lock = new object();
2025-09-17 18:56:28 +08:00
public static T Inst
{
get
{
2025-09-29 07:45:07 +08:00
lock (_lock)
2025-09-17 18:56:28 +08:00
{
2025-09-29 07:45:07 +08:00
if(_inst == null)
2025-09-17 18:56:28 +08:00
{
2025-09-29 07:45:07 +08:00
_inst = new T();
Debug.Log($"[Singleton] 创建 {typeof(T).Name} 实例");
2025-09-17 18:56:28 +08:00
}
2025-09-29 07:45:07 +08:00
return _inst;
2025-09-17 18:56:28 +08:00
}
}
}
2025-09-29 07:45:07 +08:00
}