单例的区别

一、继承monobehaviour类的单例模式

public class Player : MonoBehaviour
{
    public static Player Instance;
    private void Awake()
    {
        Instance = this;
    }
}

二、普通类的单例模式

public class Player
{
    private static Player _instance;
    public static Player Instance
    {
        get
        {
            if(_instance==null)
            {
                _instance=new Player();
            }
            return _instance;
        }
    }
}

三、区别

 继承monobehaviour类的单例模式不能再空的时候直接new一个对象,但普通的可以

猜你喜欢

转载自blog.csdn.net/cherry_f_f/article/details/140660324