private static IEnumerator _DownloadFile(string url, string savePath, OnRequestSuccess onDownloadStart, OnRequestSuccess onFinish, OnRequestError onError, OnRequestProgress onProgress) { var headRequest = UnityWebRequest.Head(url); yield return(headRequest.SendWebRequest()); var totalSize = long.Parse(headRequest.GetResponseHeader("Content-Length")); var fileInfo = new FileInfo(savePath); if (fileInfo.Exists && fileInfo.Length == totalSize) { onFinish?.Invoke(headRequest); } else { using (var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET)) { var handler = new DownloadHandlerFileRange(savePath, request); request.downloadHandler = handler; onDownloadStart?.Invoke(request); var asyncOp = request.SendWebRequest(); while (!asyncOp.isDone) { yield return(null); onProgress?.Invoke(asyncOp); } if (request.isNetworkError || request.isHttpError) { handler.Close(); if (onError != null) { Debug.LogWarningFormat("SendRequest Failed:\nresponseCode :{0}\nerror :{1}\nurl:{2}", request.responseCode, request.error, request.url); onError(request); } else { Debug.LogErrorFormat("SendRequest Failed:\nresponseCode :{0}\nerror :{1}\nurl:{2}", request.responseCode, request.error, request.url); } } else { Debug.LogFormat("Finish UnityWebRequest: {0}\nresponseCode :{1}", request.url, request.responseCode); onFinish?.Invoke(request); } } } }
private void _DownloadRes(PatchInfo patchInfo) { var patchZipUrl = $"{this._remoteRoot}/{patchInfo.fileName}?{DateTime.Now.Ticks}"; var savePath = ResManager.DocumentDirPath + patchInfo.fileName; DownloadHandlerFileRange downloadHandler = null; WebRequestMgr.DownloadFile(patchZipUrl, savePath, (e) => { downloadHandler = e.downloadHandler as DownloadHandlerFileRange; onMsg("开始下载..."); onProgress(0f); }, (request) => { onMsg("解压补丁包..."); onProgress(0.8f); ZipTool.UncompressFile(savePath, ResManager.DocumentBundlePath); FileExt.DeleteFile(savePath); UpdateResVer(patchInfo.nextVer); needReload = true; //更新完毕再重新拉取最新patchInfo FetchPatchInfo(); }, (request) => { UpdateResVer(null); onMsg("下载补丁包失败,忽略更新"); onProgress(1f); onFinish(); }, (asyncOp) => { if (downloadHandler != null) { long totalSize = downloadHandler.FileSize; long curSize = downloadHandler.DownloadedSize; onMsg($"下载补丁包中...({curSize}/{totalSize})({downloadHandler.DownloadProgress * 100}%)"); } else { onMsg($"下载补丁包中...({asyncOp.webRequest.downloadedBytes}/{patchInfo.fileSize})"); } onProgress(asyncOp.progress * 0.8f); }); }
private IEnumerator DoDownload() { UnityWebRequest unityWebRequest = new UnityWebRequest(Url); unityWebRequest.downloadHandler = new DownloadHandlerFileRange(LocalPath, unityWebRequest, UseRange, FileSize); DownloadHandlerFileRange handler = (DownloadHandlerFileRange)unityWebRequest.downloadHandler; unityWebRequest.useHttpContinue = false; handler.StartDownloadEvent += () => { if (FileSize > -1 && FileSize != handler.FileSize) { ErrorText = "fileSize error: targetSize->" + handler.FileSize + " ConfigSize->" + FileSize; _onError(this); handler.Dispose(); unityWebRequest.Dispose(); unityWebRequest = null; } }; handler.ErrorEvent += () => { ErrorText = "targetSize->" + handler.FileSize + " ConfigSize->" + FileSize; _onError(this); handler.Dispose(); unityWebRequest.Dispose(); unityWebRequest = null; }; unityWebRequest.SendWebRequest(); if (unityWebRequest == null) { yield break; } while (unityWebRequest.isDone == false) { if (_isCancel) { _onCancel?.Invoke(this); break; } _onProgress?.Invoke(handler.DownloadProgress); yield return(null); } if (_isCancel == false) { if (unityWebRequest.isHttpError || unityWebRequest.isNetworkError) { ErrorText = unityWebRequest.error; _onError?.Invoke(this); } else { CheckFile(); } } handler.Dispose(); unityWebRequest.Dispose(); }