111
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user