unity更新资源

来源:互联网 发布:2017中国人口数据分析 编辑:程序博客网 时间:2024/04/27 21:24
using UnityEngine;using System;using System.IO;using System.Xml;using System.Collections;using System.Collections.Generic;using LuaInterface;/* *  * 说明载入界面的流程1、网上下载版本新列表xml2、读取本地版本旧列表xml,如果没有,创建一个,xml数据为新列表xml的数据3、对比新旧列表xml,查找出需要更新的对比列表xml,如果没有,则跳转界面4、根据对比列表xml下载更新的文件,下载完之后跳转界面*/public class LoadingScene : MonoBehaviour{#region 属性///本地assetbundle存放的地址public static  string LOCAL_ASSETBUNDLE_URL;///网络assetbundle存放的地址public static  string SERVER_ASSETBUNDLE_URL;/// 本地版本xml路径public static  string LOCAL_VERSION_PATH;///本地资源存放地址列表Dictionary<string , int> _dicNewVersionNum = new Dictionary<string ,int> ();///需要更新的资源存放地址Dictionary<string , int> _dicNeedUpdateRes = new Dictionary<string ,int> ();//logo界面public GameObject LogoWindow;//检查更新界面public GameObject CheckWindow;//检查更新窗口public GameObject CheckingWindow;//确认更新窗口public GameObject ConfirmWindow;//下载资源界面public GameObject DownloadWindow;//游戏加载界面public GameObject LoadingWindow;public UILabel DownLoadTipLabel;public UILabel LoadProgressLabel;public UISlider LoadProgressSlider;string Resurl; int ResVerNum;int LoadProgress;//异步对象AsyncOperation async;#endregion#region 业务流程void Start (){OnInit ();LoadLogoWindow ();}/// <summary>/// 初始化游戏/// </summary>protected void OnInit (){//debug:清空缓存//Caching.CleanCache ();//指定本地资源列表存放目录和文件名//LOCAL_VERSION_PATH = Application.persistentDataPath + "/oldVersionnum.xml";//选择加载路径#if UNITY_ANDROID//LOCAL_ASSETBUNDLE_URL = "jar:file://" + Application.dataPath + "!/assets/asset/Android/";SERVER_ASSETBUNDLE_URL = "http://mytopou.gyzj.net/Android/";#endif#if UNITY_IPHONE || UNITY_IOS//SERVER_ASSETBUNDLE_URL ="file://"+Application.dataPath + "/Raw/asset/iPhone/";SERVER_ASSETBUNDLE_URL = "http://mytopou.gyzj.net/iPhone/";#endifSERVER_ASSETBUNDLE_URL = "http://mytopou.gyzj.net/Android/";//#if UNITY_EDITOR//SERVER_ASSETBUNDLE_URL = "file://" + Application.dataPath + "/StreamingAssets/asset/Android/";//#endif//本地资源存放的地址//SERVER_ASSETBUNDLE_URL = "file://" + Application.dataPath + "/StreamingAssets/";//网络资源存放的地址//SERVER_ASSETBUNDLE_URL = "http://mytopou.gyzj.net/Android/";//删除本地资源列表文件//if (File.Exists (LOCAL_VERSION_PATH)) {//File.Delete (LOCAL_VERSION_PATH);//}}/// <summary>/// 游戏logo载入/// </summary>void LoadLogoWindow (){Invoke ("LoadCheckWindow", 3f);}/// <summary>/// 检测更新/// </summary>void LoadCheckWindow (){LogoWindow.SetActive (false);CheckWindow.SetActive (true);//检查更新Invoke ("LoadCheckingWindow", 0.5f);}/// <summary>/// 下载版本更新列表,并检测是否更新/// </summary>void LoadCheckingWindow (){CheckingWindow.SetActive (true);StartCoroutine (CheckUpdate (SERVER_ASSETBUNDLE_URL + "VersionNum.assetbundle", "VersionNum")); }/// <summary>/// 等待用户确认更新/// </summary>void LoadConfirmWindow (){CheckingWindow.SetActive (false);ConfirmWindow.SetActive (true);}/// <summary>/// 进入下载更新资源界面/// </summary>void LoadDownLoadWindow (){Debug.Log ("确认更新");DownloadWindow.SetActive (true);ConfirmWindow.SetActive (false);CheckWindow.SetActive (false);StartCoroutine ("LoadAssetCoroutine");}/// <summary>/// 游戏加载/// </summary>IEnumerator LoadLoadingWindow (){LoadingWindow.SetActive (true);DownloadWindow.SetActive (false);CheckingWindow.SetActive (false);CheckWindow.SetActive (false);yield return new WaitForSeconds (3f);int _verNum = 1;foreach (KeyValuePair<string,int> kv in Const.DicAsset) {if (kv.Key.Contains ("LoginScene")) {_verNum = kv.Value;}}WWW download = WWW.LoadFromCacheOrDownload (SERVER_ASSETBUNDLE_URL + "LoginScene.unity3d", _verNum);yield return download;Debug.Log ("name " + SERVER_ASSETBUNDLE_URL + "LoginScene.unity3d");///加载镜像资源async = Application.LoadLevelAsync ("LoginScene");async.allowSceneActivation = false;yield return async;AssetBundle myBundle = download.assetBundle;myBundle.Unload (false);}IEnumerator LoadScene(){yield return new WaitForSeconds (2f);async.allowSceneActivation = true;}void Update(){if(LoadProgressLabel.enabled==true){if(async!=null){LoadProgress = (int)(async.progress * 100);if( async.progress > 0.88f){LoadProgress = 100;StartCoroutine("LoadScene");}LoadProgressSlider.value = LoadProgress/100;LoadProgressLabel.text = LoadProgress+ "%";}}}/// <summary>/// 下载资源/// </summary> IEnumerator LoadAssetCoroutine (){Debug.Log ("还有 " + _dicNeedUpdateRes.Count + " 个资源需要下载");DownLoadTipLabel.text = "还有 " + _dicNeedUpdateRes.Count + " 个资源需要下载";//ReplaceLocalXML (LOCAL_VERSION_PATH, _dicNewVersionNum);//StartCoroutine (LoadLoadingWindow ());yield return new WaitForSeconds (1f);foreach (KeyValuePair<string,int> pair in _dicNeedUpdateRes) {Resurl = pair.Key;ResVerNum = pair.Value;}StartCoroutine ("LoadAsset");}IEnumerator LoadAsset (){Debug.Log (Resurl + "正在下载");DownLoadTipLabel.text = Resurl + "正在下载";WWW bundle = WWW.LoadFromCacheOrDownload (SERVER_ASSETBUNDLE_URL + Resurl, ResVerNum);yield return bundle;while (bundle.isDone == false)yield return null;AssetBundle myBundle = bundle.assetBundle;myBundle.Unload (false);_dicNeedUpdateRes.Remove (Resurl);Debug.Log (Resurl + "下载完毕");if(bundle.error !=null){DownLoadTipLabel.text = bundle.error;}if (_dicNeedUpdateRes.Count > 0) {foreach (KeyValuePair<string,int> pair in _dicNeedUpdateRes) {Resurl = pair.Key;ResVerNum = pair.Value;}StartCoroutine ("LoadAsset");} else {Debug.Log ("全部下载下载完毕");DownLoadTipLabel.text = "全部下载下载完毕,即将进入游戏加载界面";//更新本地版本列表数据//Debug.Log ("更新本地版本列表数据===========================》");//ReplaceLocalXML (LOCAL_VERSION_PATH, _dicNewVersionNum);yield return new WaitForSeconds (2f);StartCoroutine (LoadLoadingWindow ());}}#endregion/// <summary>///下载资源列表/// </summary>IEnumerator CheckUpdate (string path, string name){yield return new WaitForSeconds (1f);Debug.Log (" 下载版本列表数据==============================》");WWW bundle = new WWW (path);yield return bundle;while (bundle.isDone == false)yield return null;Debug.Log (" 下载完毕,解析数据=============================》");TextAsset VersionNum = bundle.assetBundle.Load (name, typeof(TextAsset)) as TextAsset;_dicNewVersionNum = ReadVersionNumFileForString (VersionNum.text);// 将新的资源列表保存到全局列表Const.DicAsset = _dicNewVersionNum;foreach (KeyValuePair<string ,int > newPair in Const.DicAsset) {Debug.Log ("新版本地址=== " + newPair.Key + "   Num=== " + newPair.Value);}Debug.Log (" 检测需要更新的资源============================》");//_dicNeedUpdateRes = CompareVersion (_dicNewVersionNum, _dicOldVersionNum);_dicNeedUpdateRes = CompareRes (_dicNewVersionNum);foreach (KeyValuePair<string, int> pair in _dicNeedUpdateRes) {Debug.Log (" 需要下载的文件地址===" + pair.Key + "  num===" + pair.Value);}if (_dicNeedUpdateRes.Count == 0) {Debug.Log (" 没有资源要更新,进入游戏加载界面===============》");//加载界面StartCoroutine (LoadLoadingWindow ());} else {Debug.Log (" 资源需要更新,玩家确认是否更新===============》");//下载界面LoadConfirmWindow ();}}#region 逻辑方法/// <summary>/// 检测需要下载的资源/// </summary>Dictionary<string ,int >CompareRes (Dictionary<string ,int > dicNewVersionNum){Dictionary<string, int> dic = new Dictionary<string, int> ();foreach (KeyValuePair<string,int> pair in dicNewVersionNum) {string url = pair.Key;int verNum = pair.Value;if (Caching.IsVersionCached (url, verNum) == false) {dic.Add (url, verNum);}}return dic;}/// <summary>/// 对比XML/// </summary>Dictionary<string ,int >CompareVersion (Dictionary<string ,int > dicNewVersionNum, Dictionary<string ,int > dicOldVersionNum){Dictionary<string, int> DicVersionNum = new Dictionary<string, int> ();foreach (KeyValuePair<string,int> pair in dicNewVersionNum) {string newFileName = pair.Key;int newNum = pair.Value;//新增文件if (!dicOldVersionNum.ContainsKey (newFileName)) {DicVersionNum.Add (newFileName, newNum);Debug.Log ("新增的文件 " + newFileName);} //对比版本else {int oldNum;dicOldVersionNum.TryGetValue (newFileName, out oldNum);if (!newNum.Equals (oldNum)) {DicVersionNum.Add (newFileName, newNum);Debug.Log ("更新的文件 " + newFileName + "  旧版本号为  " + oldNum + "   新版本号为  " + newNum);}}}return DicVersionNum;}/// <summary>/// 用最新的列表替换本地列表/// </summary>void ReplaceLocalXML (string xmlPath, Dictionary<string, int> data){if (File.Exists (xmlPath)) {File.Delete (xmlPath);}//创建一个新的xmlSaveVersionNumFile (data, xmlPath);}/// <summary>/// 读取本地的xml文件/// </summary>/// <param name="xmlPath">读取的地址</param>/// <param name="dicNewVersionNum">文件不存在时存储的内容</param>Dictionary<string ,int > ReadLocalXML (string xmlPath, Dictionary<string, int> dicNewVersionNum){  Dictionary<string, int> DicVersionNum = new Dictionary<string, int> ();if (!File.Exists (xmlPath)) {  //xml文件不存在,创建一个新的xmlSaveVersionNumFile (null, xmlPath);//Debug.Log (xmlPath + " 文件不存在,创建一个文件!");}//读取xmlDicVersionNum = ReadVersionNumFileFromPath (xmlPath);Debug.Log ("本地文件读取完毕!");return DicVersionNum;}/// <summary>/// 存储xml/// </summary>/// <param name="data">存储的内容</param>/// <param name="savePath">指定存储的地址</param>static void SaveVersionNumFile (Dictionary<string, int> data, string savePath){XmlDocument XmlDoc = new XmlDocument ();XmlElement XmlRoot = XmlDoc.CreateElement ("VersionNum");XmlDoc.AppendChild (XmlRoot);if (data != null) {foreach (KeyValuePair<string, int> pair in data) {XmlElement xmlElem = XmlDoc.CreateElement ("File");XmlRoot.AppendChild (xmlElem);xmlElem.SetAttribute ("FileName", pair.Key);xmlElem.SetAttribute ("Num", XmlConvert.ToString (pair.Value));}}XmlDoc.Save (savePath);XmlRoot = null;XmlDoc = null;}/// <summary>/// 根据内容读取xml/// </summary>Dictionary<string, int> ReadVersionNumFileForString (string contents){Dictionary<string, int> DicVersionNum = new Dictionary<string, int> ();XmlDocument XmlDoc = new XmlDocument ();XmlDoc.LoadXml (contents);XmlElement XmlRoot = XmlDoc.DocumentElement;foreach (XmlNode node in XmlRoot.ChildNodes) {if ((node is XmlElement) == false)continue;string file = (node as XmlElement).GetAttribute ("FileName");int num = XmlConvert.ToInt32 ((node as XmlElement).GetAttribute ("Num"));if (DicVersionNum.ContainsKey (file) == false) {DicVersionNum.Add (file, num);}}XmlRoot = null;XmlDoc = null;return DicVersionNum;}/// <summary>/// 根据路径读取xml/// </summary>static Dictionary<string, int> ReadVersionNumFileFromPath (string fileName){Dictionary<string, int> DicVersionNum = new Dictionary<string, int> ();// 如果文件不存在,则直接返回if (System.IO.File.Exists (fileName) == false)return DicVersionNum;XmlDocument XmlDoc = new XmlDocument ();XmlDoc.Load (fileName);XmlElement XmlRoot = XmlDoc.DocumentElement;foreach (XmlNode node in XmlRoot.ChildNodes) {if ((node is XmlElement) == false)continue;string file = (node as XmlElement).GetAttribute ("FileName");int num = XmlConvert.ToInt32 ((node as XmlElement).GetAttribute ("Num"));if (DicVersionNum.ContainsKey (file) == false) {DicVersionNum.Add (file, num);}}XmlRoot = null;XmlDoc = null;return DicVersionNum;}#endregion}

0 0
原创粉丝点击