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;
}
}

原文地址:https://www.cnblogs.com/tqvdong/p/15131447.html