IEnumerator GoToUpdateResource(UpdateInfo info, AssetCheckData data) { float size = (float)info.totalSize / 1024; if (size < 0.1) { size = 0.1f; } data.msg = "更新大小:" + size.ToString("0.0") + "kb"; int downLoadIdx = 0; foreach (KeyValuePair <string, string> pair in info.dict) { downLoadIdx++; data.value = (float)(downLoadIdx - 1) / (float)info.dict.Count; data.msg = "更新文件:" + downLoadIdx + "/" + info.dict.Count + ";总大小:" + size.ToString("0.0") + "kb"; //data.msg = "更新大小:" + size.ToString("0.0") + "M" ; UIManager.Intance.SendMsg(WindowID.AssetCheckUI, WindowMsgID.ShowLoadingTips, data); BeginDownload(pair.Value, pair.Key); while (!(IsDownOK(pair.Key))) { yield return(new WaitForEndOfFrame()); } } yield return(new WaitForEndOfFrame()); data.msg = "更新完成, 准备进入游戏"; data.value = 0f; Debug.Log("更新完成!!"); UIManager.Intance.SendMsg(WindowID.AssetCheckUI, WindowMsgID.ShowLoadingTips, data); EventsMgr.Instance().TriigerEvent("EndUpdate", null); }
void OnUpdateFailed(string file) { UnityTools.LogMust("更新失败!>" + file); AssetCheckData data = new AssetCheckData(); data.msg = "下次记得在有网络下的环境玩耍约!"; data.value = 0f; UIManager.Intance.SendMsg(WindowID.AssetCheckUI, WindowMsgID.ShowLoadingTips, data); EventsMgr.Instance().TriigerEvent("EndUpdate", null); }
IEnumerator OnUpdateResource() { AssetCheckData data = new AssetCheckData(); string dataPath = PathTools.DataPath; //数据目录 url = PathTools.GetWebURL(); string random = DateTime.Now.ToString("yyyymmddhhmmss"); string listUrl = url + "files.txt?v=" + random; WWW www = new WWW(listUrl); yield return(www); if (www.error != null) { OnUpdateFailed(string.Empty); yield break; } else { if (!Directory.Exists(dataPath)) { Directory.CreateDirectory(dataPath); } File.WriteAllBytes(dataPath + "files.txt", www.bytes); string filesText = www.text; UpdateInfo info = GetUpdateInfo(filesText); if (info.totalSize > 0) { CachUpdateInfo = info; CachCheckData = data; StartCoroutine(GoToUpdateResource(CachUpdateInfo, CachCheckData)); /* * float size = (float)info.totalSize / 1024; * UIManager.Intance.ShowMessageBox(string.Format("更新大小: {0} kb", size.ToString("0.0")), MessageBoxType.OK_Cancle, OnSureUpdateClick); * yield break;*/ } else { EventsMgr.Instance().TriigerEvent("EndUpdate", null); } } }
//解压zip IEnumerator UnZip() { AssetCheckData data = new AssetCheckData(); data.msg = "解压文件中"; data.value = 0f; string bundlePath = PathTools.DataPath + "assetbundle"; if (!Directory.Exists(bundlePath)) { Directory.CreateDirectory(bundlePath); } string scenePath = PathTools.DataPath + "scene"; if (!Directory.Exists(scenePath)) { Directory.CreateDirectory(scenePath); } string[] zipFolder = ZipHelper.ZipFolders(); int tempIndex = 0; foreach (string path in zipFolder) { string zipPath = PathTools.DataPath + path; if (!Directory.Exists(zipPath)) { Directory.CreateDirectory(zipPath); } tempIndex++; data.value = (float)(tempIndex - 1) / (float)zipFolder.Length; UIManager.Intance.SendMsg(WindowID.AssetCheckUI, WindowMsgID.ShowLoadingTips, data); string[] zipName = zipPath.Split('/'); string zipLocal = PathTools.DataPath + "data_" + zipName[zipName.Length - 1] + ".zip"; UnityTools.LogMust("正在解压文件 = " + zipLocal); ZipHelper.UnZipFolder(zipLocal, zipPath); File.Delete(zipLocal); yield return(new WaitForSeconds(0.2f)); } yield return(0); }
public override void OnMsg(WindowMsgID msgId, object param) { if (msgId == WindowMsgID.ShowLoadingTips) { AssetCheckData data = (AssetCheckData)param; if (data != null) { loadingBar.fillAmount = data.value; if (loadingBar.fillAmount == 0) { tips.text = data.msg; loadingBar.transform.parent.gameObject.SetActive(false); } else { loadingBar.transform.parent.gameObject.SetActive(true); tips.text = data.msg + "<color=#DFCD9FFF>(" + (loadingBar.fillAmount * 100).ToString("0.0") + "%)</color>"; } } } }
IEnumerator OnExtractResource() { string dataPath = PathTools.DataPath; //数据目录 Debug.Log("解包存放地址:>" + dataPath); string resPath = PathTools.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); } AssetCheckData data = new AssetCheckData(); data.msg = "比较资源中"; data.value = 0f; UnityTools.LogMust("正在解包文件:>" + infile); UIManager.Intance.SendMsg(WindowID.AssetCheckUI, WindowMsgID.ShowLoadingTips, data); UnityTools.LogMust(infile); UnityTools.LogMust(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 idx = 0; foreach (var file in files) { string[] fs = file.Split('|'); infile = resPath + fs[0]; // outfile = dataPath + fs[0]; data.msg = "解压文件, 不耗流量" + idx.ToString() + "/" + files.Length.ToString(); idx++; data.value = (float)(idx - 1) / (float)files.Length; UnityTools.LogMust("正在拷贝文件:>" + data.value + " , " + infile); UIManager.Intance.SendMsg(WindowID.AssetCheckUI, WindowMsgID.ShowLoadingTips, data); 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()); } UnityTools.LogMust("拷贝完成!!!"); data.value = 0.0f; data.msg = "版本检测"; UIManager.Intance.SendMsg(WindowID.AssetCheckUI, WindowMsgID.ShowLoadingTips, data); yield return(new WaitForSeconds(0.1f)); //释放完成,开始启动更新资源 UIManager.Intance.SendMsg(WindowID.AssetCheckUI, WindowMsgID.ShowLoadingTips, data); if (GameManager.Instance.bOpenExtract_Zip) { yield return(StartCoroutine(UnZip())); data.value = 0f; data.msg = "版本检测, 请耐心等待.."; UnityTools.LogMust("解压完成!!!"); UIManager.Intance.SendMsg(WindowID.AssetCheckUI, WindowMsgID.ShowLoadingTips, data); } HandleEndExtract(); }