using UnityEngine;
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T instance;
public static T Instance
{
get
{
if (instance == null)
{
instance = FindObjectOfType<T>();
if (instance == null)
{
GameObject singletonObject = new GameObject(typeof(T).Name);
instance = singletonObject.AddComponent<T>();
}
}
return instance;
}
}
}
Unity 单例模式
- 上一篇: golang格式化、代码检测工具
- 下一篇: Linux安装Rust
已有 0 条评论