private IEnumerator DownLoadHandler(ResDownReqInfo info) { // 开始下载 WebClient client = new WebClient(); client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(DownloadFileCompleted); client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); client.DownloadFileAsync(new Uri(info.m_url), ResDef.OuterPackageDirectory + info.m_name); clientinfomap[client] = info; // 正在下载,设置下载状态 info.m_result.eState = ResDownState.Downing; while (info.m_result.fProgress <= 1.0f) { yield return(new WaitForEndOfFrame()); } if (info.m_result.sErrorMsg == "") { // 通知请求下载对象,下载资源成功 info.m_iDown.ResourceDownSucc(info.m_result); } else // 下载失败 { // 通知请求下载对象,下载资源失败 info.m_iDown.ResourceDownFail(info.m_result); } // 重置下载中标识 m_DowningFileList.Remove(info.m_name); client.Dispose(); client = null; }
// 资源下载刷新 public void Update() { //暂停 if (m_isPause) { return; } // 有异常下载任务,优先下载 if (m_ExceptionQueue.Count > 0) { // 没有正在下载 if (m_DowningFileList.Count <= 0) { // 开启协程下载 for (int i = 0; i < ConcurrenceNumber; i++) { if (m_ExceptionQueue.Count > 0) { ResDownReqInfo info = m_ExceptionQueue.Dequeue(); m_DowningFileList[info.m_name] = info.m_url; StartCoroutine(DownLoadHandler(info)); } } } else { return; } } // 有正常下载任务,其次下载 if (m_DownQueue.Count > 0) { // 没有正在下载 if (m_DowningFileList.Count <= 0) { for (int i = 0; i < ConcurrenceNumber; i++) { if (m_DownQueue.Count > 0) { // 开启协程下载 ResDownReqInfo info = m_DownQueue.Dequeue(); m_DowningFileList[info.m_name] = info.m_url; StartCoroutine(DownLoadHandler(info)); } } } else { return; } } // // 下载结束,取消订阅Update事件 // if (m_ExceptionQueue.Count <= 0 && m_DownQueue.Count <= 0) // { // SubscribeUpdateEvent(false); // } }
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) { WebClient client = (WebClient)sender; ResDownReqInfo info = clientinfomap[client]; info.m_result.fProgress = e.ProgressPercentage / 100.0f; }
/// <summary> /// 获取当前下载信息 /// </summary> /// <returns>返回当前下载信息</returns> public ResDownResult?GetCurDownInfo() { if (m_DownQueue.Count > 0) { // 取出当前下载请求信息对象 ResDownReqInfo info = m_DownQueue.Peek(); return(info.m_result); } return(null); }
void DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { WebClient client = (WebClient)sender; ResDownReqInfo info = clientinfomap[client]; clientinfomap.Remove(client); if (e.Error == null) { // 更新下载结果信息 info.m_result.eState = ResDownState.DownSucc; info.m_result.fProgress = 1.01f; info.m_result.sErrorMsg = ""; } else // 下载失败 { // 更新下载结果信息 info.m_result.eState = ResDownState.DownFail; info.m_result.fProgress = 1.01f; info.m_result.sErrorMsg = e.Error.ToString(); } }