Files
TuanTuan-Engine/Assets/GameFramework/Runtime/Singleton.cs

25 lines
592 B
C#
Raw Normal View History

2025-11-03 00:24:36 +08:00
using UnityEngine;
2025-11-12 07:04:31 +08:00
namespace Tuan.GameFramework
2025-11-03 00:24:36 +08:00
{
2025-11-12 07:04:31 +08:00
public class Singleton<T> where T : class, new()
2025-11-03 00:24:36 +08:00
{
2025-11-12 07:04:31 +08:00
static T _inst;
static readonly object _lock = new object();
public static T Inst
2025-11-03 00:24:36 +08:00
{
2025-11-12 07:04:31 +08:00
get
2025-11-03 00:24:36 +08:00
{
2025-11-12 07:04:31 +08:00
lock (_lock)
2025-11-03 00:24:36 +08:00
{
2025-11-12 07:04:31 +08:00
if (_inst == null)
{
_inst = new T();
Debug.Log($"[Singleton] 创建 {typeof(T).Name} 实例");
}
return _inst;
2025-11-03 00:24:36 +08:00
}
}
}
}
2025-11-12 07:04:31 +08:00
}