Unity 使用单例模式

懒汉模式:如果不调用则不会进行实例化

private static danli Instance;
public static danli _Instance()
{
    if (Instance==null)
    {
        Instance= new danli();
    }
    return Instance;
}

 饿汉模式:系统运行则主动实例化

private static danli Instance =new danli();
public static danli _Instance()
{
    return Instance;
}

继承MonoBehaviour:便于其它模块调用,但非唯一性

private static danli Instance;

void Awake()
{
    Instance = this;
}

public static danli _Instance()
{
    return Instance;
}

其他

private static DownLoadManager _Instance = null;
public static DownLoadManager Instance
{
    get
    {
        if (_Instance == null)
        {
            _Instance = FindObjectOfType(typeof(DownLoadManager)) as DownLoadManager;
        }
        return _Instance;
    }
}

 

 

你可能感兴趣的:(Unity)