/// <summary> /// 启动更新下载,这里只是个思路演示,此处可启动线程下载更新 /// </summary> IEnumerator OnUpdateResource() { #pragma warning disable 0162 if (!AppConst.UpdateMode) { OnResourceInited(); yield break; } string dataPath = Util.DataPath; //数据目录 string url = AppConst.WebUrl; //string message = string.Empty; string random = DateTime.Now.ToString("yyyymmddhhmmss"); #pragma warning restore 0162 #region lorry2-9,获取服务器版本,更新对应版本的资源 //TODO:这个version也可以向Server服务器请求获得 string versionUrl = url + "server_version.txt?v=" + random; UnityWebRequest verDownload = UnityWebRequest.Get(versionUrl); yield return(verDownload.Send()); if (verDownload.isError) { Debug.Log(verDownload.error); facade.SendMessageCommand(NotiConst.UPDATE_MESSAGE, "获得server_version.txt失败!热更中断:" + verDownload.error); yield break; } string version = verDownload.downloadHandler.text; url = AppConst.WebUrl + version + "/" + Util.GetPlatformName() + "/"; #endregion string listUrl = url + "files.txt?v=" + random; Debug.LogWarning("Down files.txt -->>" + listUrl); UnityWebRequest www = UnityWebRequest.Get(listUrl); yield return(www.Send()); if (www.isError) { facade.SendMessageCommand(NotiConst.UPDATE_MESSAGE, "更新files.txt失败!热更中断:" + www.error); yield break; } if (!Directory.Exists(dataPath)) { Directory.CreateDirectory(dataPath); } //首先把files.txt写到数据目录。 File.WriteAllBytes(dataPath + "files.txt", www.downloadHandler.data); string filesText = www.downloadHandler.text; string[] files = filesText.Split('\n'); Debug.LogWarning("Write files.txt To-->>" + dataPath); m_DataVersion = files[0]; //这里修改了luaFramework的原始流程,先把所有的操作动作存下来。 List <string> willDownLoadUrl = new List <string>(); //from List <string> willDownLoadFileName = new List <string>(); List <string> willDownLoadDestination = new List <string>(); //to facade.SendMessageCommand(NotiConst.UPDATE_MESSAGE, "分析需要更新的文件"); Debug.LogWarning("分析需要更新的文件:" + files.Length); int totalSize = 0; for (int i = 1; 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) { //本地存在fileUrl的这个文件,这里进行md5的对比,如果相同就不更新了。 string remoteMd5 = keyValue[1].Trim(); string localMd5 = Util.md5file(localfile); canUpdate = !remoteMd5.Equals(localMd5); if (PlayerPrefs.GetString(localfile) == version)//判断本地是否有已经下载的信息 { canUpdate = false; willDownLoadDestination.Add(localfile);//有下载信息也要下载 } if (canUpdate) { File.Delete(localfile); //md5码不同,把本地文件删除,接下来会下载这个文件。 } } if (canUpdate) { int fileSize = int.Parse(keyValue[2]); totalSize += fileSize; willDownLoadUrl.Add(fileUrl); //下载地址 willDownLoadFileName.Add(f); willDownLoadDestination.Add(localfile); //目标文件路径 //这里本来是使用了线程下载,但是我测试中总是卡在第一个下载,暂时换www来进行下载。 //等到需要在游戏中边运行边下载,再考虑线程下载。ThreadManager不成功,总是在OnDownloadFile中WebClient.DownloadFileAsync后就没有反应了 //添加的DownloadProgressChangedEventHandler(ProgressChanged)无法进入,而且异常也没捕获。 } } facade.SendMessageCommand(NotiConst.UPDATE_MESSAGE, "开始下载更新文件" + Util.FormatFileSize(totalSize)); if (willDownLoadUrl.Count > 0) { facade.SendMessageCommand(NotiConst.UPDATE_ALL_COUNT, willDownLoadUrl.Count); } else { Debug.LogWarning("分析完毕,没有文件需要更新!"); needHotFix = false; } //这里是对比后需要更新的资源文件,TODO:用线程下载 len = willDownLoadUrl.Count; num = 0; for (int i = 0; i < willDownLoadUrl.Count; i++) { Debug.Log("下载:" + willDownLoadUrl[i]); facade.SendMessageCommand(NotiConst.UPDATE_FILE_NAME, willDownLoadFileName[i]); PlayerPrefs.SetString(willDownLoadDestination[i], version);//如果已经下载设置值为版本号 HadDownLoadDestination.Add(willDownLoadDestination[i]); Thread thread = new Thread(new ParameterizedThreadStart(Down)); thread.Start(willDownLoadUrl[i] + "|" + willDownLoadDestination[i] + "|" + willDownLoadFileName[i]); } yield return(new WaitForEndOfFrame()); }