using UnityEngine;
public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
protected static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
_instance = (T)FindObjectOfType(typeof(T));
if (_instance == null)
{
Debug.LogError("An instance of " + typeof(T) +
" is needed in the scene, but there is none.");
}
}
return _instance;
}
set
{
_instance = value;
}
}
public virtual void Awake()
{
if (_instance == null)
{
_instance = this as T;
}
else
{
Debug.LogError("Repeat XComponentSingleton" + typeof(T));
}
}
}
public class Singleton<T> where T : new()
{
protected static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
_instance = new T();
}
return _instance;
}
}
}
单例实现
最新推荐文章于 2023-10-31 13:27:06 发布