/// <summary> /// 下载需要更新的资源 /// </summary> /// <param name="needDownload_dic"></param> /// <returns></returns> IEnumerator DownloadAsset(Dictionary <string, MD5_FileInfo> needDownload_dic) { int has_download_count = 0; int need_download_count = needDownload_dic.Count; foreach (var pair in needDownload_dic) { string path = Util.PathCombine(AppConst.Res_Download_Address, Client.GetHttpServerBundleDir(), pair.Key); Debug.Log(path); WWW www = new WWW("http://" + path); while (!www.isDone) { yield return(null); } if (string.IsNullOrEmpty(www.error)) { string file_path = Util.PathCombine(Client.GetPersistentPath(), pair.Key); string file_md5 = Util.GetBytesMD5(www.bytes); if (string.Equals(file_md5, pair.Value.MD5)) //下载完成后,对Md5的教研 { string directory = file_path.Substring(0, file_path.LastIndexOf('/')); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } File.WriteAllBytes(file_path, www.bytes); downloaded_dic.Add(pair.Key, pair.Value); has_download_count++; MsgManager.Broadcast(Msg.Res_Download_One, new object[] { (float)has_download_count / (float)need_download_count, pair.Key }); } else { //教研出错的话,需要加到下载队列中,重新下载 Debug.LogError("下载出错 " + pair.Key); } } else { Debug.Log(string.Format("下载出错 {0} 因为 {1} ", pair.Key, www.error)); } } if (has_download_count == need_download_count) { ExportDownloadedMD5(downloaded_dic); MsgManager.Broadcast(Msg.Res_DownLoad_Finish, null); Debug.Log("下载完成"); } else { Debug.Log(string.Format("下载进度 download_count {0} need_download {1}", has_download_count, need_download_count)); } }
void UpdateRes() { old_res_dic = GetMd5_Old(); StartCoroutine(DownLoadNewMD5()); CheckNeedDownDic(); if (need_download_dic.Count == 0) { MsgManager.Broadcast(Msg.Res_DownLoad_Finish, null); } else { MsgManager.Broadcast(Msg.Res_Download_Start, new object[] { res_total_size }); StartCoroutine(DownloadAsset(need_download_dic)); } }
/// <summary> /// 资源的解压 /// </summary> /// <returns></returns> IEnumerator MoveStreamingAssetsToPersisdentPath() { string streamingPath = Client.GetStreamingDataPath(); //随包资源目录 string persistentPath = Client.GetPersistentPath(); //持久化资源目录 if (Directory.Exists(persistentPath)) { Directory.Delete(persistentPath, true); } Directory.CreateDirectory(persistentPath); //先找到MD5 匹配文件 string inFile = Client.GetStreamingMD5Path(); string outFile = Client.GetPersistentMD5Path(); if (File.Exists(outFile)) { File.Delete(outFile); } if (Application.platform == RuntimePlatform.Android) { WWW www = new WWW(inFile); yield return(www); if (www.isDone) { File.WriteAllBytes(outFile, www.bytes); } yield return(0); } else { File.Copy(inFile, outFile, true); } yield return(new WaitForEndOfFrame()); //释放所有文件到数据目录 string[] files = File.ReadAllLines(outFile); MsgManager.Broadcast(Msg.Res_Release_Start, new object[] { files.Length }); foreach (var file in files) { string[] fs = file.Split('|'); inFile = Path.Combine(streamingPath, fs[0]); outFile = Path.Combine(persistentPath, fs[0]); string dir = Path.GetDirectoryName(outFile); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } if (Application.platform == RuntimePlatform.Android) { WWW www = new WWW(inFile); yield return(www); if (www.isDone) { File.WriteAllBytes(outFile, www.bytes); } yield return(0); } else { if (File.Exists(outFile)) { File.Delete(outFile); } File.Copy(inFile, outFile, true); } MsgManager.Broadcast(Msg.Res_Release_One, null); yield return(new WaitForEndOfFrame()); } //解包完更新 UpdateRes(); }