Unity 中单例模式基类

Unity单例模式基类,需要时继承即可
 

public class Singleton : MonoBehaviour where T : Singleton
{
    public static T Instance { get; private set; }
 
    protected void Awake()
    {
        if (Instance == null)
        {
            Instance = (T) this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }
}

你可能感兴趣的:(Unity_设计模式,unity,单例模式,c#)