This commit is contained in:
2025-11-12 07:04:31 +08:00
parent 33a4742904
commit f615d8ddb0
68 changed files with 1388 additions and 1478 deletions

View File

@@ -1,28 +1,31 @@
using UnityEngine;
public class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour
namespace Tuan.GameFramework
{
private static T _instance;
private static readonly object _lock = new object();
public static T Inst
public class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour
{
get
private static T _instance;
private static readonly object _lock = new object();
public static T Inst
{
lock (_lock)
get
{
if (_instance == null)
lock (_lock)
{
_instance = FindAnyObjectByType<T>();
if (_instance == null)
{
GameObject singletonObject = new GameObject(typeof(T).Name);
DontDestroyOnLoad(singletonObject);
_instance = singletonObject.AddComponent<T>();
Debug.Log($"实例化{singletonObject.name}");
_instance = FindAnyObjectByType<T>();
if (_instance == null)
{
GameObject singletonObject = new GameObject(typeof(T).Name);
DontDestroyOnLoad(singletonObject);
_instance = singletonObject.AddComponent<T>();
Debug.Log($"实例化{singletonObject.name}");
}
}
return _instance;
}
return _instance;
}
}
}