Exemplo n.º 1
0
    /// <summary>
    /// 下载文件
    /// </summary>
    /// <param name="url"></param>
    /// <param name="type"></param>
    /// <param name="callback"></param>
    /// <param name="cachePath">缓存到本地的相对目录,如果为null则不缓存</param>
    /// <param name="retry"></param>
    public static void Download(string url, FileDownloadType type, FileDownloadCallback callback, string cachePath = null, int retry = 0)
    {
        if (downloader == null)
        {
            downloader = new FileDownloader();
            Updater.Instance.OnPreUpdate.Add(downloader.OnUpdate);
            Updater.Instance.OnPostDestroy.Add(downloader.OnDestroy);
        }
        FileDownloadContext ctx = new FileDownloadContext();

        ctx.url       = url;
        ctx.type      = type;
        ctx.callback  = callback;
        ctx.retry     = retry;
        ctx.cachePath = cachePath;
        if (downloader.CheckCache(ctx))
        {
            if (ctx.callback != null)
            {
                ctx.callback(ctx);
            }
            return;
        }
        downloader.sendQueue.Enqueue(ctx);
    }