using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Text; using System.IO; using System.Xml; using UnityEngine.UI; public class AssetGame_ResourceUpdate : AssetGame_Base { /// /// 需要下载的总资源数 /// private int MaxBundleNum = 0; /// /// 需要下载的资源总大小 /// private int MaxBundleSize = 0; /// /// 废弃的变量 /// private float mProgress = 0f; /// /// 在下载更新界面显示的进度条 /// public Slider mSlider = null; /// /// 在下载更新界面显示的进度数字 /// public Text mSliderText = null; /// /// 每下载一个文件,占总额度的百分比 /// private float mPerPropress = 0f; /// ///需要下载的资源 /// private List NeedDownFiles=new List(); /// /// 增加新要下载的资源名 /// /// public void AddLoadRes(string nameString) { if (!NeedDownFiles.Contains(nameString)) { NeedDownFiles.Add(nameString); } } /// /// 废弃的功能 /// public float GetProgress { get { return mProgress ; } } /// /// 获得剩余需要下载的资源数量 /// public int GetNeedDownFilesCount { get { return NeedDownFiles.Count; } } /// /// 获得需要下载的总资源数量 /// public int GetMaxBundleNum { get { return MaxBundleNum; } } /// /// 设置需要下载的资源总数 /// public int SetMaxBundleNum { set { MaxBundleNum = value; } } /// /// 获取资源总大小 /// public int GetMaxBundleSize { get { return MaxBundleSize; } } /// /// 设置资源总大小 /// public int SetMaxBundleSize { set { MaxBundleSize = value; } } /// /// 将赋值进度条 /// /// public void SetSlider(Slider sld,Text txt) { mSlider = sld; mSlider.value = 0; mSliderText = txt; } /// /// 依次加载需要更新的资源 /// public void DownLoadRes(string serverVersion) { if (NeedDownFiles.Count == 0) { AssetGame_VersionManager.Ins.SetUpdateLocalVersionFile(); return; } string ServerMD5; string ServerResSize; string file = NeedDownFiles[0]; LogHelper.Log("还有" + NeedDownFiles .Count +"个未更新资源,总数" + MaxBundleNum + "个。"); float sizemega = 0; sizemega =(float ) MaxBundleSize / 1000000; LogHelper.Log(string .Format ("剩余{0:N1}", sizemega) + string .Format ("兆,{0:N0}" , MaxBundleSize) + "字节需要下载"); LogHelper.Log(AssetConfig_GlobalSetting.strServerUrl + file); string cdnpath = AssetConfig_GlobalSetting.strServerUrl + serverVersion + "/"; AssetSystem_CoroutineProvider.Instance.StartCoroutine(DownLoad(cdnpath + file, delegate(WWW w) { if (w.error != null) { LogHelper.Log("没有更新到资源 file=" + file); LogHelper.Log(w.error); } else { AssetGame_VersionManager.Ins.ServerResVersion.TryGetValue(file ,out ServerMD5); AssetGame_VersionManager.Ins.ServerResSizeVersion.TryGetValue(file, out ServerResSize); LogHelper.Log("将下载的资源替换本地的资源 "); //将下载的资源替换本地的资源 // AssetSystem_Log.Ins.Log("wenjian " + file); // LogHelper.Log("wenjian "+ file); MaxBundleSize -= int.Parse (ServerResSize); //LogHelper.Log("当前数据" + ServerResSize + "/"+ MaxBundleSize ); AssetSystem_AndroidFileHelper.Instance.ReWriteFileByByte(file, w.bytes); AddOrModifyXml(file,ServerMD5,ServerResSize); } NeedDownFiles.RemoveAt(0); DownLoadRes(serverVersion); })); } private void AddOrModifyXml(string strFilename,string strMD5,string strSize) { bool find = false; string savePath = AssetConfig_GlobalSetting.strLocalSavePath; XmlDocument XmlDoc = new XmlDocument(); XmlDoc.Load(savePath + "/VersionMD5.txt"); XmlNodeList nodeList = XmlDoc.SelectSingleNode("Files").ChildNodes;//获取Files节点的所有子节点 foreach (XmlNode xn in nodeList)//遍历所有子节点 { XmlElement xmlElem = (XmlElement)xn;//将子节点类型转换为XmlElement类型 if (xmlElem.GetAttribute("FileName") == strFilename)//如果genre属性值为“李赞红” { xmlElem.SetAttribute("FileName", strFilename); xmlElem.SetAttribute("MD5", strMD5); xmlElem.SetAttribute("Size", strSize); find = true; } } if (!find) { XmlNode XmlRoot = XmlDoc.SelectSingleNode("Files"); XmlElement xmlElem = XmlDoc.CreateElement("File"); xmlElem.SetAttribute("FileName", strFilename); xmlElem.SetAttribute("MD5", strMD5); xmlElem.SetAttribute("Size", strSize); XmlRoot.AppendChild(xmlElem); } XmlDoc.Save(savePath + "/VersionMD5.txt"); XmlDoc = null; } /// /// 读取string /// 逐行读入,以“,”分割 /// /// /// public void ToParseVersionFile(string content, Dictionary dict, string log) { if (content == null || content.Length == 0) { LogHelper.Log(log + " is null"); return; } string[] items = content.Split(new char[] { '\n' }); foreach (string item in items) { string[] info = item.Split(new char[] { ',' }); if (info != null && info.Length == 2) { dict.Add(info[0], info[1]); } } } /// /// 读取XmlDocument /// 固定格式node,以属性添加数据【FileName】+【MD5】 /// /// /// /// /// public void ToParseVersionFileXml(string content, Dictionary dict, string log) { if (content == null || content.Length == 0) { LogHelper.Log(log + " is null"); return; } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(content); LogHelper.Log("ParseVersionFileXml:1"); //获取根节点 XmlElement rootNodeList = xmlDoc.DocumentElement;//Response //获取FlightDatas节点 XmlNodeList xmlChildFlightNodeList = rootNodeList.GetElementsByTagName("Files"); LogHelper.Log("ParseVersionFileXml:2"); foreach (XmlNode node in rootNodeList.ChildNodes) { //node.Attributes[] string fileName = node.Attributes["FileName"].Value.ToString(); string fileMd5 = node.Attributes["MD5"].Value.ToString(); dict.Add(fileName, fileMd5); } LogHelper.Log("ParseVersionFileXml:3"); } /// /// 读取XmlDocument /// 固定格式node,以属性添加数据【FileName】+【MD5】 /// /// /// /// /// public void ToParseVersionFileSizeXml(string content, Dictionary dict, string log) { if (content == null || content.Length == 0) { LogHelper.Log(log + " is null"); return; } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(content); //获取根节点 XmlElement rootNodeList = xmlDoc.DocumentElement;//Response //获取FlightDatas节点 XmlNodeList xmlChildFlightNodeList = rootNodeList.GetElementsByTagName("Files"); foreach (XmlNode node in rootNodeList.ChildNodes) { //node.Attributes[] string fileName = node.Attributes["FileName"].Value.ToString(); string fileSize = node.Attributes["Size"].Value.ToString(); // LogHelper.Log(fileName + " ***** " + fileSize); dict.Add(fileName, fileSize); // LogHelper.LogError(fileName + "/" + fileSize ); } } /// /// 加载这里,加入了和进度条有关的,这个以前测试过,是可以用的.ljp20170323 /// /// /// /// public IEnumerator DownLoad(string url, HandleFinishDownload finishFun) { WWW www = new WWW(url); if (MaxBundleNum > 0) { mPerPropress = (float)1 / MaxBundleNum; } int displayProgress = 0; int toProgress = 0; while (!www.isDone) { // LogHelper.Log("01 "+displayProgress+" " + toProgress + " "+mPerPropress ); toProgress = (int)(www.progress * mPerPropress * 100); while (displayProgress < toProgress) { ++displayProgress; SetProgress(displayProgress); yield return null; } yield return null; } toProgress = (int)(mPerPropress * 100); while (displayProgress < toProgress) { // LogHelper.Log("02"); ++displayProgress; SetProgress(displayProgress); yield return null; } yield return www; if (finishFun != null) { finishFun(www); } www.Dispose(); } public delegate void HandleFinishDownload(WWW www); /// /// 设置进度条 /// /// public void SetProgress(int progress) { if (mSlider != null) { int xx = (int)(((1.0f) * MaxBundleNum - NeedDownFiles.Count) / MaxBundleNum * 100); float progressValue = (xx + progress) * 0.01f; if(progressValue > 1) { progressValue = 1.0f; } if(progressValue > mSlider.value) { mSlider.value = progressValue; mSliderText.text = (xx + progress) + "%"; } } } /* * * 现在上面加的都是和进度条有关的,这个以前测试过,是可以用的.ljp20170323 * * * * * * * * */ }