/// <summary> /// 载入AssetBundle /// </summary> /// <param name="abname"></param> /// <returns></returns> public AssetBundle LoadAssetBundle(string abname) { if (!abname.EndsWith(GameDefine.ExtName)) { abname += GameDefine.ExtName; } AssetBundle bundle = null; if (!bundles.ContainsKey(abname)) { byte[] stream = null; string uri = GameDefine.GetAbDataPath() + abname; Debug.LogWarning("LoadFile::>> " + uri); LoadDependencies(abname); stream = File.ReadAllBytes(uri); bundle = AssetBundle.LoadFromMemory(stream); //关联数据的素材绑定 bundles.Add(abname, bundle); } else { bundles.TryGetValue(abname, out bundle); } return(bundle); }
/// <summary> /// 释放资源 /// </summary> public void CheckExtractResource() { //如果游戏资源已经释放到本地 if (Directory.Exists(GameDefine.GetAbDataPath()) && File.Exists(GameDefine.GetAbDataPath() + "files.txt")) { //需检查版本号是否需要更新 StartCoroutine(OnUpdateResource()); Initialize(); } else//如果游戏资源未释放到本地就需钥执行释放资源的操作 { StartCoroutine(OnExtractResource()); //启动释放协成 } }
/// <summary> /// 启动更新下载,这里只是个思路演示,此处可启动线程下载更新 /// </summary> public IEnumerator OnUpdateResource() { string dataPath = GameDefine.GetAbDataPath(); //数据目录 string url = Directory.GetParent(Application.dataPath) + "/UpdataResources/StreamingAssets/"; Debug.Log("新版本资源路径:" + url); //string random = DateTime.Now.ToString("yyyymmddhhmmss"); string listUrl = url + "files.txt"; Debug.LogWarning("LoadUpdate---->>>" + listUrl); WWW www = new WWW(listUrl); yield return(www); if (www.error != null) { Debug.LogError("更新失败!!"); 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; bool canUpdate = !File.Exists(localfile); if (!canUpdate) { string remoteMd5 = keyValue[1].Trim(); string localMd5 = GlobalTools.Md5File(localfile); canUpdate = !remoteMd5.Equals(localMd5.Split('|')[0]); if (canUpdate) { File.Delete(localfile); } } if (canUpdate) { //本地缺少文件 Debug.LogError("正在更新文件:" + fileUrl); //facade.SendMessageCommand(NotiConst.UPDATE_MESSAGE, message); //这里都是资源文件,用线程下载 BeginDownload(fileUrl, localfile); while (!DownloadFiles.Contains(localfile)) { yield return(new WaitForEndOfFrame()); } } Debug.Log("更新进度:" + Math.Round((float)i / (files.Length - 1), 2)); } Debug.Log("更新完成!!"); yield return(new WaitForSeconds(2f)); }
//释放资源 IEnumerator OnExtractResource() { Debug.Log("开始释放资源"); string dataPath = GameDefine.GetAbDataPath(); //数据目录 string resPath = Application.streamingAssetsPath + "/"; //游戏包资源目录 if (Directory.Exists(dataPath)) { Directory.Delete(dataPath, true); } Directory.CreateDirectory(dataPath); string outfile = dataPath + "files.txt"; string infile = resPath + "files.txt"; if (File.Exists(outfile)) { File.Delete(outfile); } 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); foreach (var file in files) { string[] fs = file.Split('|'); infile = resPath + fs[0]; // outfile = dataPath + fs[0]; Debug.Log("+++正在解包文件:>" + infile); Debug.Log("---正在解包文件:>" + outfile); 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()); } yield return(new WaitForSeconds(0.1f)); Debug.Log("解包完成!!!"); Initialize(); //释放完成,开始启动更新资源 StartCoroutine(OnUpdateResource()); }