private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) { //UnityEngine.Debug.Log(e.ProgressPercentage); /*UnityEngine.Debug.Log(string.Format("{0} MB's / {1} MB's", * (e.BytesReceived / 1024d / 1024d).ToString("0.00"), * (e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00")));*/ // float value2 = (float)e.ProgressPercentage / 100f; string value = string.Format("{0} kb/s", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00")); if (e.BytesReceived > 1024d) { value = string.Format("{0} MB's / {1} MB's", (e.BytesReceived / 1024d / 1024d).ToString("0.00"), (e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00")); } NotiData data = new NotiData(NotiConst.UPDATE_PROGRESS, value); if (m_SyncEvent != null) { m_SyncEvent(data); } if (e.ProgressPercentage == 100 && e.BytesReceived == e.TotalBytesToReceive) { sw.Reset(); data = new NotiData(NotiConst.UPDATE_DOWNLOAD, currDownFile); if (m_SyncEvent != null) { m_SyncEvent(data); } } CreateLoading.setTitle("正在加载同步资源," + value); CreateLoading.setProgress(e.ProgressPercentage); }
IEnumerator OnExtractResource() { string dataPath = Util.DataPath; //数据目录 string resPath = Util.AppContentPath(); //游戏包资源目录 if (Directory.Exists(dataPath)) { Directory.Delete(dataPath, true); } Directory.CreateDirectory(dataPath); string infile = resPath + "files.txt"; string outfile = dataPath + "files.txt"; if (File.Exists(outfile)) { File.Delete(outfile); } string message = "正在解包文件:>files.txt"; Debug.Log(infile); Debug.Log(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); //解包进度 int count = 0; CreateLoading.setTitle("正在解压缩..."); foreach (var file in files) { string[] fs = file.Split('|'); infile = resPath + fs[0]; // outfile = dataPath + fs[0]; //解包进度 count++; CreateLoading.setProgress(count / ((double)files.Length / 100)); message = "正在解包文件:>" + fs[0]; Debug.Log("正在解包文件:>" + infile); facade.SendMessageCommand(NotiConst.UPDATE_MESSAGE, message); 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); } yield return(new WaitForEndOfFrame()); } message = "解包完成!!!"; //解包进度 CreateLoading.setTitle("解包完成..."); CreateLoading.setProgress(100); facade.SendMessageCommand(NotiConst.UPDATE_MESSAGE, message); yield return(new WaitForSeconds(0.1f)); message = string.Empty; //释放完成,开始启动更新资源 StartCoroutine(OnUpdateResource()); }
/// <summary> /// 启动更新下载,这里只是个思路演示,此处可启动线程下载更新 /// </summary> IEnumerator OnUpdateResource() { if (!AppConst.UpdateMode) { OnResourceInited(); yield break; } CreateLoading.setTitle("正在更新..."); string dataPath = Util.DataPath; //数据目录 string url = AppConst.WebUrl; string message = string.Empty; string random = DateTime.Now.ToString("yyyymmddhhmmss"); string listUrl = url + "files.txt?v=" + random; Debug.LogWarning("LoadUpdate---->>>" + listUrl); WWW www = new WWW(listUrl); yield return(www); if (www.error != null) { OnUpdateFailed(string.Empty); yield break; } if (!Directory.Exists(dataPath)) { Directory.CreateDirectory(dataPath); } File.WriteAllBytes(dataPath + "files.txt", www.bytes); string filesText = www.text; string[] files = filesText.Split('\n'); for (int i = 0; i < files.Length; i++) { if (string.IsNullOrEmpty(files[i])) { continue; } string[] keyValue = files[i].Split('|'); string f = keyValue[0]; //此处显示加载状态 string localfile = (dataPath + f).Trim(); string path = Path.GetDirectoryName(localfile); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string fileUrl = url + f + "?v=" + random; bool canUpdate = !File.Exists(localfile); if (!canUpdate) { string remoteMd5 = keyValue[1].Trim(); string localMd5 = Util.md5file(localfile); canUpdate = !remoteMd5.Equals(localMd5); if (canUpdate) { File.Delete(localfile); } } if (canUpdate) //本地缺少文件 { Debug.Log(fileUrl); message = "downloading>>" + fileUrl; facade.SendMessageCommand(NotiConst.UPDATE_MESSAGE, message); CreateLoading.setProgress(0); /* * www = new WWW(fileUrl); yield return www; * if (www.error != null) { * OnUpdateFailed(path); // * yield break; * } * File.WriteAllBytes(localfile, www.bytes); */ //这里都是资源文件,用线程下载 BeginDownload(fileUrl, localfile); while (!(IsDownOK(localfile))) { yield return(new WaitForEndOfFrame()); } } } yield return(new WaitForEndOfFrame()); message = "更新完成!!"; CreateLoading.setTitle("更新完成..."); facade.SendMessageCommand(NotiConst.UPDATE_MESSAGE, message); yield return(new WaitForSeconds(0.1f)); OnResourceInited(); }