unity mono单例

public class SingletonManager<T> : MonoBehaviour where T : MonoBehaviour
{
private static T instance;
public static T GetInstance()
{
if (instance == null)
instance = (T)FindObjectOfType(typeof(T));
return instance;
}

protected virtual void Awake()
{
instance = this as T;
}

protected virtual void OnDestroy()
{
instance = null;
}
}

posted @ 2021-08-12 10:11  T驱动  阅读(85)  评论(0编辑  收藏  举报