private IEnumerator Load(string path, string strSceneName) { #if DISABLE_ASSETBUNDLE yield return(null); path = path.Replace(".unity3d", ""); m_Async = SceneManager.LoadSceneAsync(strSceneName, LoadSceneMode.Additive); m_Async.allowSceneActivation = false; #else //获取资源完整路径 string fullPath = LocalFileManager.Instance.LocalFilePath + path; //如果路径不存在 则进行下载 if (!File.Exists(fullPath)) { DownloadDataEntity entity = DownloadManager.Instance.GetServerData(path); if (entity != null) { StartCoroutine(AssetBundleDownload.Instance.DownloadData(entity, (bool isSuccess) => { if (isSuccess) { StartCoroutine(LoadScene(fullPath, strSceneName)); } })); } } else { StartCoroutine(LoadScene(fullPath, strSceneName)); } yield return(null); #endif }
/// <summary> /// 从返回字符串转换下载数据列表 /// </summary> /// <param name="text"></param> /// <returns></returns> private List <DownloadDataEntity> GetDownloadDataListFromText(string text) { List <DownloadDataEntity> downloadDataList = null; if (!string.IsNullOrEmpty(text)) { string[] splitData = text.Split('\n'); if (splitData != null) { MDebug.Log(LOG_TAG, $"GetDownloadDataListFromText: DataCount:{splitData.Length}"); downloadDataList = new List <DownloadDataEntity>(splitData.Length); for (int i = 0; i < splitData.Length; i++) { MDebug.Log(LOG_TAG, $"GetDownloadDataListFromText: DataString:{splitData[i]}"); if (!string.IsNullOrEmpty(splitData[i])) { DownloadDataEntity dataEntity = GetDataEntity(splitData[i]); if (IsDownloadEntityValid(dataEntity)) { downloadDataList.Add(dataEntity); } } } } } return(downloadDataList); }
private IEnumerator Load(string path, string strSceneName) { string fullPath = LocalFileManager.Instance.LocalFilePath + path; if (!IOUtil.FileExists(fullPath)) { DownloadDataEntity entity = DownloadManager.Instance.GetServerData(path); if (entity != null) { StartCoroutine(AssetBundleDownload.Instance.DownloadData(entity, (bool isSuccess) => { if (isSuccess) { AssetBundleManager.Instance.CheckDps(path, () => { StartCoroutine(LoadScene(fullPath, strSceneName)); }); } })); } } else { AssetBundleManager.Instance.CheckDps(path, () => { StartCoroutine(LoadScene(fullPath, strSceneName)); }); } yield return(null); }
/// <summary> /// 修改更新后的资源文件 /// </summary> /// <param name="mCurrDownloadData"></param> internal void ModifyLocalData(DownloadDataEntity entity) { if (mLocalDataList == null) { return; } bool isExists = false; for (int i = 0; i < mLocalDataList.Count; i++) { //如果本地已存在entity.FullName文件名的资源了 if (mLocalDataList[i].FullName.Equals(entity.FullName, StringComparison.CurrentCultureIgnoreCase)) { mLocalDataList[i].MD5 = entity.MD5; mLocalDataList[i].Size = entity.Size; mLocalDataList[i].IsFirstData = entity.IsFirstData; isExists = true; break; } } if (!isExists) { mLocalDataList.Add(entity); } SavaLoaclVersion(); }
/// <summary> /// 修改本地文件 /// </summary> /// <param name="entity">更新的下载数据</param> public void ModifyLocalData(DownloadDataEntity entity) { if (m_LocalList == null) { return; } bool isExists = false; for (int i = 0; i < m_LocalList.Count; ++i) { if (m_LocalList[i].FullName.Equals(entity.FullName, StringComparison.CurrentCultureIgnoreCase)) { m_LocalList[i].MD5 = entity.MD5; m_LocalList[i].Size = entity.Size; m_LocalList[i].IsFirstData = entity.IsFirstData; isExists = true; break; } } if (!isExists) { m_LocalList.Add(entity); } SaveLocalVersion(); }
/// <summary> 初始化版本文件回调 </summary> /// <param name="obj"></param> private void OnInitVersionCallBack(List <DownloadDataEntity> serverDownloadData) { //得到服务端数据列表 m_ServerDataList = serverDownloadData; if (File.Exists(m_LocalVersionPath)) { //如果本地存在版本文件 则和服务器端的进行对比 //服务器端的版本文件字典 Dictionary <string, string> serverDic = PackDownloadDataDic(serverDownloadData); //获取客户端的版本文件字典 string content = IOUtil.GetFileText(m_LocalVersionPath); Dictionary <string, string> clientDic = PackDownloadDataDic(content); m_LocalDataList = PackDownloadData(content); //1、查找新加的初始资源 for (int i = 0; i < serverDownloadData.Count; i++) { if (serverDownloadData[i].IsFirstData && !clientDic.ContainsKey(serverDownloadData[i].FullName)) { //加入下载列表 m_NeedDownloadDataList.Add(serverDownloadData[i]); } } //2、对比已经下载过的 但是有更新的资源 foreach (var item in clientDic) { //如果md5不一致 if (serverDic.ContainsKey(item.Key) && serverDic[item.Key] != item.Value) { DownloadDataEntity entity = GetDownloadData(item.Key, serverDownloadData); if (entity != null) { m_NeedDownloadDataList.Add(entity); } } } } else { //如果不存在 则初始资源就是需要下载的文件 for (int i = 0; i < serverDownloadData.Count; i++) { if (serverDownloadData[i].IsFirstData) { m_NeedDownloadDataList.Add(serverDownloadData[i]); } } } if (m_NeedDownloadDataList.Count == 0) { SceneInitCtrl.Instance.SetProgress("资源更新完毕", 1); if (OnInitComplete != null) { OnInitComplete(); } return; } AssetBundleDownload.Instance.DownloadFiles(m_NeedDownloadDataList); }
private bool IsDownloadEntityValid(DownloadDataEntity dataEntity) { if (!string.IsNullOrEmpty(dataEntity.FileName) && !string.IsNullOrEmpty(dataEntity.MD5)) { return(true); } return(false); }
/// <summary> /// 检查依赖项 /// </summary> /// <param name="index"></param> /// <param name="arrDps"></param> /// <param name="onComplete"></param> private void CheckDps(int index, string[] arrDps, System.Action onComplete) { lock (this) { if (arrDps == null || arrDps.Length == 0) { if (onComplete != null) { onComplete(); } return; } string fullPath = LocalFileMgr.Instance.LocalFilePath + arrDps[index]; if (!File.Exists(fullPath)) { //如果文件不存在 需要下载 DownloadDataEntity entity = DownloadMgr.Instance.GetServerData(arrDps[index]); if (entity != null) { AssetBundleDownload.Instance.StartCoroutine(AssetBundleDownload.Instance.DownloadData(entity, (bool isSuccess) => { index++; if (index == arrDps.Length) { if (onComplete != null) { onComplete(); } return; } CheckDps(index, arrDps, onComplete); })); } } else { index++; if (index == arrDps.Length) { if (onComplete != null) { onComplete(); } return; } CheckDps(index, arrDps, onComplete); } } }
/// <summary> /// 单个资源下载完成 /// </summary> /// <param name="obj"></param> private void SingleAssetDownloadSuccessCallBack(DownloadDataEntity dataEntity) { MDebug.Log(LOG_TAG, $"SingleAssetDownloadSuccessCallBack!,FileName:{dataEntity.FileName},MD5:{dataEntity.MD5}"); ModifyLocalAssetVersionFile(dataEntity); //处理需要下载list for (int i = 0; i < m_DataNeedToDownloadList.Count; i++) { if (m_DataNeedToDownloadList[i].FileName.Equals(dataEntity.FileName)) { m_DataNeedToDownloadList.RemoveAt(i); break; } } }
/// <summary> /// 检查依赖项(递归) /// </summary> /// <param name="index">数组索引</param> /// <param name="arrDps">所有依赖项</param> /// <param name="onComplete">完成回调</param> private void CheckDpsAsync(int index, string[] arrDps, Action onComplete) { if (arrDps == null || arrDps.Length == 0) { if (onComplete != null) { onComplete(); } return; } string fullPath = LocalFileManager.Instance.LocalFilePath + arrDps[index]; if (!IOUtil.FileExists(fullPath)) { Debug.LogWarning("文件" + fullPath + "不存在"); DownloadDataEntity entity = DownloadManager.Instance.GetServerData(arrDps[index]); if (entity != null) { AssetBundleDownload.Instance.StartCoroutine(AssetBundleDownload.Instance.DownloadData(entity, (bool isSuccess) => { ++index; if (index == arrDps.Length) { if (onComplete != null) { onComplete(); } return; } CheckDpsAsync(index, arrDps, onComplete); })); } } else { ++index; if (index == arrDps.Length) { if (onComplete != null) { onComplete(); } return; } CheckDpsAsync(index, arrDps, onComplete); } }
/// <summary> 动态下载更新 </summary> /// <param name="currDownloadData">当前需要下载的文件</param> /// <param name="onComplete">下载完成回调 bool 下载成功或失败</param> /// <returns></returns> public IEnumerator DownloadData(DownloadDataEntity currDownloadData, Action <bool> onComplete) { string dataUrl = DownloadManager.Instance.DownLoadUrl + currDownloadData.FullName; //段路径 用于创建文件夹 string path = currDownloadData.FullName.Substring(0, currDownloadData.FullName.LastIndexOf('\\')); //得到本地路径 用\\移动端创建不出来文件夹 path.Replace('\\','/') string localFilePath = DownloadManager.Instance.LocalFilePath + path.Replace('\\', '/'); IOUtil.CheckDirectoryPath(localFilePath); WWW www = new WWW(dataUrl); float timeOut = Time.time; float progress = www.progress; while (www != null && !www.isDone) { if (progress < www.progress) { timeOut = Time.time; progress = www.progress; } if ((Time.time - timeOut) > AppConst.DownloadTimeOut) { Debug.Log("下载失败 超时 Path:" + dataUrl); if (onComplete != null) { onComplete(false); } yield break; } yield return(null); } if (www != null && www.error == null) { using (FileStream fs = new FileStream(DownloadManager.Instance.LocalFilePath + currDownloadData.FullName, FileMode.Open, FileAccess.ReadWrite)) { fs.Write(www.bytes, 0, www.bytes.Length); } } //写入本地文件 DownloadManager.Instance.ModifyLocalData(currDownloadData); if (onComplete != null) { onComplete(true); } }
/// <summary> /// 根据string,获取数据,但是会传值 /// </summary> /// <param name="dataText"></param> /// <returns></returns> private DownloadDataEntity GetDataEntity(string dataText) { if (!string.IsNullOrEmpty(dataText)) { string[] paramStr = dataText.Split(' '); if (paramStr != null && paramStr.Length >= m_DataParameters.Length) { MDebug.Log(LOG_TAG, $"Data FileName:{paramStr[0]},MD5:{paramStr[1]},Size:{paramStr[2]}"); DownloadDataEntity dataEntity = new DownloadDataEntity(paramStr[0], paramStr[1], long.Parse(paramStr[2])); return(dataEntity); } else { MDebug.LogError(LOG_TAG, $"Data No Enough Parameters,DataText:{dataText}"); } } return(new DownloadDataEntity()); }
/// <summary> 根据文本内容 封装下载数据列表 </summary> /// <param name="content">文本内容</param> /// <returns></returns> public List <DownloadDataEntity> PackDownloadData(string content) { List <DownloadDataEntity> list = new List <DownloadDataEntity>(); string[] arrLines = content.Split('\n'); for (int i = 0; i < arrLines.Length; i++) { string[] arrData = arrLines[i].Split(' '); if (arrData.Length == 4) { DownloadDataEntity entity = new DownloadDataEntity(); entity.FullName = arrData[0]; entity.MD5 = arrData[1]; entity.Size = arrData[2].ToInt(); entity.IsFirstData = arrData[3].ToInt() == 1; list.Add(entity); } } return(list); }
/// <summary> /// 封装下载数据 /// </summary> /// <param name="content">版本文件内容</param> /// <returns></returns> public List <DownloadDataEntity> PackDownloadData(string content) { List <DownloadDataEntity> lst = new List <DownloadDataEntity>(); string[] arrLines = content.Split('\n'); for (int i = 0; i < arrLines.Length; ++i) { string[] arrData = arrLines[i].Split(';'); if (arrData.Length == 4) { DownloadDataEntity entity = new DownloadDataEntity() { FullName = arrData[0], MD5 = arrData[1], Size = arrData[2].ToInt(), IsFirstData = arrData[3].ToBool() }; lst.Add(entity); } } return(lst); }
/// <summary> /// 修改内存以及本地版本文件 /// </summary> private void ModifyLocalAssetVersionFile(DownloadDataEntity entity) { if (m_localDataDic.ContainsKey(entity.FileName)) { for (int i = 0; i < m_LocalDataList.Count; i++) { if (m_LocalDataList[i].FileName.Equals(entity.FileName)) { m_LocalDataList[i] = entity; } } MDebug.Log(LOG_TAG, $"ModifyLocalAssetVersionFile,Update MD5! FileName:{entity.FileName},NewMD5:{entity.MD5}"); m_localDataDic[entity.FileName] = entity.MD5; } else { m_LocalDataList.Add(entity); m_localDataDic[entity.FileName] = entity.MD5; MDebug.Log(LOG_TAG, $"ModifyLocalAssetVersionFile,Add New! FileName:{entity.FileName},NewMD5:{entity.MD5}"); } SaveLocalFileFromLocalList(m_LocalDataList); }
/// <summary> /// 把版本信息内需要下载的资源详情封装成Entity实体 /// </summary> /// <param name="content"></param> /// <returns></returns> public List <DownloadDataEntity> PackDownloadData(string content) { List <DownloadDataEntity> lst = new List <DownloadDataEntity>(); string[] arrLines = content.Split('\n'); //每换一行代表一条数据 for (int i = 0; i < arrLines.Length; i++) { string[] arrData = arrLines[i].Split(' '); //自己定义的版本文件格式是空格 if (arrData.Length == 4) //自己定义的版本文件,每一条资源有4个属性 { //把资源详情读出来 DownloadDataEntity entity = new DownloadDataEntity(); entity.FullName = arrData[0]; //资源名字 entity.MD5 = arrData[1]; //资源MD5 entity.Size = arrData[2].ToInt(); //资源大小 entity.IsFirstData = arrData[3].ToInt() == 1; //1:IsFirstData=true lst.Add(entity); } } return(lst); }
private VersionData PackDownloadData(string content) { string version = string.Empty; DownloadDataEntity gamePack = null; string[] arrLines = content.Split('\n'); if (arrLines.Length >= 2) { version = arrLines[0]; string[] arrData = arrLines[1].Split(' '); if (arrData.Length == 4) { gamePack = new DownloadDataEntity() { FullName = arrData[0], MD5 = arrData[1], Size = arrData[2].ToInt(), IsFirstData = arrData[3].ToBool() }; } } return(new VersionData(version, gamePack)); }
/// <summary> /// 版本文件下载完毕后执行此回调,功能是对比版本文件,确定是否有版本更新 /// 把需要更新的文件添加到mNeedDownloadDataList /// </summary> /// <param name="serverDownloadedEntity"></param> private void OnInitVersionCallback(List <DownloadDataEntity> serverDownloadedEntity) { string mLocalVersionFile = LocalFilePath + mVersionFileName; if (File.Exists(mLocalVersionFile)) { //本地有版本文件,则开始比对 //服务器端数据的dic<文件名,MD5> Dictionary <string, string> serverDic = PackDownloadDataDic(serverDownloadedEntity); Debug.Log("正在比对服务器版本文件..."); //读取本地版本信息 string content = IOUtil.GetFileText(mLocalVersionFile); Dictionary <string, string> clientDic = PackDownloadDataDic(content); //把本地的版本文件改装成dic mLocalDataList = PackDownloadData(content); //把本地的版本信息文件改装成list //1.开始比对,比对本地没有的文件 for (int i = 0; i < serverDownloadedEntity.Count; i++) { if (serverDownloadedEntity[i].IsFirstData && !clientDic.ContainsKey(serverDownloadedEntity[i].FullName)) //是初始资源并且本地没有这个文件 { mNeedDownloadDataList.Add(serverDownloadedEntity[i]); //加入下载列表 } } //2.对比本地存在的,但是有更新的资源 foreach (var item in clientDic) { //如果MD5不一致 if (serverDic.ContainsKey(item.Key) && serverDic[item.Key] != item.Value) { // DownloadDataEntity entity = GetDownloadData(item.Key, serverDownloadedEntity); if (entity != null) { mNeedDownloadDataList.Add(entity); //把资源加入需要下载的列表 } } } } else { //本地没有版本文件,则从服务器上下载全部更新内容 for (int i = 0; i < serverDownloadedEntity.Count; i++) { if (serverDownloadedEntity[i].IsFirstData) { mNeedDownloadDataList.Add(serverDownloadedEntity[i]); } } } //拿到下载列表 mNeedDownloadDataList 进行下载,列表=0,所有资源都下载完毕 if (mNeedDownloadDataList.Count == 0) { UIRootStartGameView.Instance.SetProgress("资源更新完毕", 1); if (OnInitComplete != null) { OnInitComplete(); } return; } //进行下载 DownloadAssetBundle.Instance.DownloadFiles(mNeedDownloadDataList); }
private IEnumerator DownloadData() { if (NeedDownloadCount == 0) { yield break; } m_CurrDownloadData = m_List[0]; string dataUrl = DownloadMgr.DownloadUrl + m_CurrDownloadData.FullName; //资源下载路径 AppDebug.Log("dataUrl=" + dataUrl); int lastIndex = m_CurrDownloadData.FullName.LastIndexOf('\\'); if (lastIndex > -1) { //短路径 用于创建文件夹 string path = m_CurrDownloadData.FullName.Substring(0, lastIndex); //得到本地路径 string localFilePath = DownloadMgr.Instance.LocalFilePath + path; if (!Directory.Exists(localFilePath)) { Directory.CreateDirectory(localFilePath); } } WWW www = new WWW(dataUrl); float timeout = Time.time; float progress = www.progress; while (www != null && !www.isDone) { if (progress < www.progress) { timeout = Time.time; progress = www.progress; m_CurrDownloadSize = (int)(m_CurrDownloadData.Size * progress); } if ((Time.time - timeout) > DownloadMgr.DownloadTimeOut) { AppDebug.LogError("下载失败 超时"); yield break; } yield return(null); //一定要等一帧 } yield return(www); if (www != null && www.error == null) { using (FileStream fs = new FileStream(DownloadMgr.Instance.LocalFilePath + m_CurrDownloadData.FullName, FileMode.Create, FileAccess.ReadWrite)) { fs.Write(www.bytes, 0, www.bytes.Length); } } //下载成功 m_CurrDownloadSize = 0; m_DownloadSize += m_CurrDownloadData.Size; //写入本地文件 DownloadMgr.Instance.ModifyLocalData(m_CurrDownloadData); m_List.RemoveAt(0); CompleteCount++; if (m_List.Count == 0) { m_List.Clear(); } else { IsStartDownload = true; } }
/// <summary> /// 添加下载对象 /// </summary> /// <param name="entity"></param> public void AddDownload(DownloadDataEntity entity) { m_List.Add(entity); }
private IEnumerator DownloadData() { if (NeedDownloadCount == 0) { yield break; } mCurrDownloadData = mList[0]; //每次总是取列表里第一个去下载,类似栈结构 string dataUrl = DownloadManager.DownloadUrl + mCurrDownloadData.FullName; //短路径 用于创建文件夹 string shortPath = mCurrDownloadData.FullName.Substring(0, mCurrDownloadData.FullName.LastIndexOf('\\')); //得到本地路径 string localFilePath = DownloadManager.Instance.LocalFilePath + shortPath; if (!Directory.Exists(localFilePath)) { Directory.CreateDirectory(localFilePath); } WWW www = new WWW(dataUrl); float timeout = Time.time; float progress = www.progress; while (www != null && !www.isDone) { if (progress < www.progress) { timeout = Time.time; progress = www.progress; mCurrDownloadSize = (int)(mCurrDownloadData.Size * progress); } if ((Time.time - timeout) > DownloadManager.DownloadTimeOut) { Debug.LogError("下载失败 超时"); yield break; } yield return(null); //一定要等一帧,防止卡死 } yield return(www); if (www != null && www.error == null) { using (FileStream fs = new FileStream(DownloadManager.Instance.LocalFilePath + mCurrDownloadData.FullName, FileMode.Create, FileAccess.ReadWrite)) { fs.Write(www.bytes, 0, www.bytes.Length); //把文件写到本地路径中 } } yield return(new WaitForSeconds(0.5f)); //每下载完成一个资源,就等待0.5秒 //下载成功 mCurrDownloadSize = 0; //重置当前下载文件的大小 mDownloadTotalSize += mCurrDownloadData.Size; //加一下总大小 //把新资源覆盖到本地 DownloadManager.Instance.ModifyLocalData(mCurrDownloadData); mList.RemoveAt(0); //下载完毕的文件移出待下载的列表 CompleteCount++; if (mList.Count == 0) { //列表没有文件了,资源下载完毕 mList.Clear(); } else { IsStartDownload = true; } }
private IEnumerator DownloadData() { if (NeedDownloadCount == 0) { Debug.Log("需要下载的数量是0"); yield break; } m_CurrentDownloadData = m_List[0]; string fullName = m_CurrentDownloadData.FullName.Replace('\\', '/'); string dataUrl = DownloadManager.Instance.DownloadUrl + fullName; int lastIndex = fullName.LastIndexOf('/'); string path = ""; if (lastIndex > 0) { path = fullName.Substring(0, lastIndex); } string localFilePath = DownloadManager.Instance.LocalFilePath + path; if (!IOUtil.DirectoryExists(localFilePath)) { IOUtil.CreateDirectory(localFilePath); } WWW www = new WWW(dataUrl + "?t=" + TimeUtil.GetTimestampMS().ToString()); float timeOut = Time.time; float progress = www.progress; while (www != null && !www.isDone) { if (progress < www.progress) { timeOut = Time.time; progress = www.progress; m_CurrentDownloadSize = (int)(m_CurrentDownloadData.Size * progress); } if (Time.time - timeOut > DownloadManager.DOWNLOAD_TIME_OUT) { www.Dispose(); AppDebug.LogWarning("下载超时"); StartCoroutine(DownloadData()); yield break; } yield return(null); } yield return(www); if (www != null && www.error == null) { using (FileStream fs = new FileStream(DownloadManager.Instance.LocalFilePath + fullName, FileMode.Create, FileAccess.Write)) { fs.Write(www.bytes, 0, www.bytes.Length); } www.Dispose(); } else { AppDebug.LogWarning(www.url + "下载失败" + www.error); yield break; } m_CurrentDownloadSize = 0; m_DownloadSize += m_CurrentDownloadData.Size; DownloadManager.Instance.ModifyLocalData(m_CurrentDownloadData); m_List.RemoveAt(0); ++CompleteCount; if (m_List.Count == 0) { m_List.Clear(); } else { StartDownload(); } }
/// <summary> /// 添加下载对象 /// </summary> /// <param name="entity"></param> public void AddDownload(DownloadDataEntity entity) { m_List.Add(entity); NeedDownloadCount = m_List.Count; }
public VersionData(string version, DownloadDataEntity gamePackData) { Version = new Version(version); GamePackData = gamePackData; }
public IEnumerator DownloadData(DownloadDataEntity currDownloadData, Action <bool> onComplete) { string dataUrl = DownloadMgr.DownloadUrl + currDownloadData.FullName; //资源下载路径 AppDebug.Log("dataUrl=" + dataUrl); //短路径 用于创建文件夹 string path = currDownloadData.FullName.Substring(0, currDownloadData.FullName.LastIndexOf('\\')); //得到本地路径 string localFilePath = DownloadMgr.Instance.LocalFilePath + path; if (!Directory.Exists(localFilePath)) { Directory.CreateDirectory(localFilePath); } WWW www = new WWW(dataUrl); float timeout = Time.time; float progress = www.progress; while (www != null && !www.isDone) { if (progress < www.progress) { timeout = Time.time; progress = www.progress; } if ((Time.time - timeout) > DownloadMgr.DownloadTimeOut) { AppDebug.LogError("下载失败 超时"); if (onComplete != null) { onComplete(false); } yield break; } yield return(null); //一定要等一帧 } yield return(www); if (www != null && www.error == null) { using (FileStream fs = new FileStream(DownloadMgr.Instance.LocalFilePath + currDownloadData.FullName, FileMode.Create, FileAccess.ReadWrite)) { fs.Write(www.bytes, 0, www.bytes.Length); } } //写入本地文件 DownloadMgr.Instance.ModifyLocalData(currDownloadData); if (onComplete != null) { onComplete(true); } }
public IEnumerator DownloadData(DownloadDataEntity entity, Action <bool> onComplete) { string dataUrl = DownloadManager.Instance.DownloadUrl + entity.FullName; Debug.Log("下载资源:" + dataUrl); int lastIndex = entity.FullName.LastIndexOf('/'); string path = ""; if (lastIndex > 0) { path = entity.FullName.Substring(0, lastIndex); } string localFilePath = DownloadManager.Instance.LocalFilePath + path; if (!IOUtil.DirectoryExists(localFilePath)) { IOUtil.CreateDirectory(localFilePath); } WWW www = new WWW(dataUrl + "?t=" + TimeUtil.GetTimestampMS().ToString()); float timeOut = Time.time; float progress = www.progress; while (www != null && !www.isDone) { if (progress < www.progress) { timeOut = Time.time; progress = www.progress; } if (Time.time - timeOut > DownloadManager.DOWNLOAD_TIME_OUT) { www.Dispose(); AppDebug.LogWarning("下载超时"); if (onComplete != null) { onComplete(false); } yield break; } yield return(null); } yield return(www); if (www != null && www.error == null) { using (FileStream fs = new FileStream(DownloadManager.Instance.LocalFilePath + entity.FullName, FileMode.Create, FileAccess.Write)) { fs.Write(www.bytes, 0, www.bytes.Length); } www.Dispose(); } else { AppDebug.LogWarning("下载失败" + www.url + " " + www.error); if (onComplete != null) { onComplete(false); } www.Dispose(); yield break; } DownloadManager.Instance.ModifyLocalData(entity); if (onComplete != null) { onComplete(true); } }
/// <summary> 加载或下载资源 </summary> /// <typeparam name="T">类型</typeparam> /// <param name="path">段路径</param> /// <param name="name">加载对象名</param> /// <param name="onComplete">C#加载完成回调</param> /// <param name="type">0=prefab 1=png</param> public void LoadOrDownload <T>(string path, string name, Action <T> onComplete, byte type = 0) where T : UnityEngine.Object { lock (this) { #if DISABLE_ASSETBUNDLE string newPath = string.Empty; switch (type) { case 0: newPath = string.Format("Assets/{0}", path.Replace("assetbundle", "prefab")); break; case 1: newPath = string.Format("Assets/{0}", path.Replace("assetbundle", "png")); break; } if (onComplete != null) { UnityEngine.Object obj = UnityEditor.AssetDatabase.LoadAssetAtPath <T>(newPath); onComplete(obj as T); } #else //1、加载依赖文件配置 LoadManifestBundle(); //2、开始加载依赖项 string[] arrDps = m_Manifest.GetAllDependencies(path); //3、检查依赖项是否已经下载 若没下载则下载 CheckDps(0, arrDps, () => { string fullPath = (LocalFileManager.Instance.LocalFilePath + path).ToLower(); #region ------ 下载或者加载主资源 ------ if (!File.Exists(fullPath)) { //文件不存在 需要下载 DownloadDataEntity entity = DownloadManager.Instance.GetServerData(path); if (entity != null) { AssetBundleDownload.Instance.StartCoroutine(AssetBundleDownload.Instance.DownloadData(entity, (bool isSuccess) => { if (isSuccess) { //下载成功 if (m_AssetDic.ContainsKey(fullPath)) { //文件已存在镜像中 if (onComplete != null) { onComplete(m_AssetDic[fullPath] as T); } return; } //先加载依赖项 for (int i = 0; i < arrDps.Length; i++) { if (!m_AssetDic.ContainsKey((LocalFileManager.Instance.LocalFilePath + arrDps[i]).ToLower())) { AssetBundleLoader loader = new AssetBundleLoader(arrDps[i]); UnityEngine.Object obj = loader.LoadAsset(GameUtil.GetFileName(arrDps[i])); m_AssetBundleDic[(LocalFileManager.Instance.LocalFilePath + arrDps[i]).ToLower()] = loader; m_AssetDic[(LocalFileManager.Instance.LocalFilePath + arrDps[i]).ToLower()] = obj; } } //直接加载 using (AssetBundleLoader loader = new AssetBundleLoader(fullPath, isFullPath: true)) { if (onComplete != null) { UnityEngine.Object obj = loader.LoadAsset <T>(name); m_AssetDic[fullPath] = obj; onComplete(obj as T); } //TODO lua回调 } } })); } else { Debuger.LogError("The fullPath is error:" + fullPath); } } else { if (m_AssetDic.ContainsKey(fullPath)) { if (onComplete != null) { onComplete(m_AssetDic[fullPath] as T); } //TODO lua回调 return; } for (int i = 0; i < arrDps.Length; i++) { if (!m_AssetDic.ContainsKey((LocalFileManager.Instance.LocalFilePath + arrDps[i]).ToLower())) { AssetBundleLoader loader = new AssetBundleLoader(arrDps[i]); UnityEngine.Object obj = loader.LoadAsset(GameUtil.GetFileName(arrDps[i])); m_AssetBundleDic[(LocalFileManager.Instance.LocalFilePath + arrDps[i]).ToLower()] = loader; m_AssetDic[(LocalFileManager.Instance.LocalFilePath + arrDps[i]).ToLower()] = obj; } } //直接加载 using (AssetBundleLoader loader = new AssetBundleLoader(fullPath, isFullPath: true)) { UnityEngine.Object obj = loader.LoadAsset <T>(name); m_AssetDic[fullPath] = obj; if (onComplete != null) { //进行回调 onComplete(obj as T); } //TODO 进行xlua的回调 } } #endregion }); #endif } }
public void LoadOrDownload <T>(string path, string name, System.Action <T> onComplete, XLuaCustomExport.OnCreate OnCreate = null, byte type = 0) where T : Object { lock (this) { #if DISABLE_ASSETBUNDLE string newPath = string.Empty; switch (type) { case 0: newPath = string.Format("Assets/{0}", path.Replace("assetbundle", "prefab")); break; case 1: newPath = string.Format("Assets/{0}", path.Replace("assetbundle", "png")); break; } if (onComplete != null) { Object obj = UnityEditor.AssetDatabase.LoadAssetAtPath <T>(newPath); onComplete(obj as T); } if (OnCreate != null) { Object obj = UnityEditor.AssetDatabase.LoadAssetAtPath <GameObject>(newPath); OnCreate(obj as GameObject); } #else //1.加载依赖文件配置 LoadManifestBundle(); //2.加载依赖项开始 string[] arrDps = m_Manifest.GetAllDependencies(path); //先检查所有依赖项 是否已经下载 没下载的就下载 CheckDps(0, arrDps, () => { //=============下载主资源开始=================== string fullPath = (LocalFileMgr.Instance.LocalFilePath + path).ToLower(); //AppDebug.Log("fullPath=" + fullPath); #region 载或者加载主资源 if (!File.Exists(fullPath)) { #region 如果文件不存在 需要下载 DownloadDataEntity entity = DownloadMgr.Instance.GetServerData(path); if (entity != null) { AssetBundleDownload.Instance.StartCoroutine(AssetBundleDownload.Instance.DownloadData(entity, (bool isSuccess) => { if (isSuccess) { if (m_AssetDic.ContainsKey(fullPath)) { if (onComplete != null) { onComplete(m_AssetDic[fullPath] as T); } return; } for (int i = 0; i < arrDps.Length; i++) { if (!m_AssetDic.ContainsKey((LocalFileMgr.Instance.LocalFilePath + arrDps[i]).ToLower())) { AssetBundleLoader loader = new AssetBundleLoader(arrDps[i]); Object obj = loader.LoadAsset(GameUtil.GetFileName(arrDps[i])); m_AssetBundleDic[(LocalFileMgr.Instance.LocalFilePath + arrDps[i]).ToLower()] = loader; m_AssetDic[(LocalFileMgr.Instance.LocalFilePath + arrDps[i]).ToLower()] = obj; } } //直接加载 using (AssetBundleLoader loader = new AssetBundleLoader(fullPath, isFullPath: true)) { if (onComplete != null) { Object obj = loader.LoadAsset <T>(name); m_AssetDic[fullPath] = obj; //进行回调 onComplete(obj as T); } //todu 进行xlua的回调 } } })); } #endregion } else { if (m_AssetDic.ContainsKey(fullPath)) { if (onComplete != null) { onComplete(m_AssetDic[fullPath] as T); } return; } //=================================== for (int i = 0; i < arrDps.Length; i++) { if (!m_AssetDic.ContainsKey((LocalFileMgr.Instance.LocalFilePath + arrDps[i]).ToLower())) { AssetBundleLoader loader = new AssetBundleLoader(arrDps[i]); Object obj = loader.LoadAsset(GameUtil.GetFileName(arrDps[i])); m_AssetBundleDic[(LocalFileMgr.Instance.LocalFilePath + arrDps[i]).ToLower()] = loader; m_AssetDic[(LocalFileMgr.Instance.LocalFilePath + arrDps[i]).ToLower()] = obj; } } //=================================== //直接加载 using (AssetBundleLoader loader = new AssetBundleLoader(fullPath, isFullPath: true)) { if (onComplete != null) { Object obj = loader.LoadAsset <T>(name); m_AssetDic[fullPath] = obj; //进行回调 onComplete(obj as T); } //todu 进行xlua的回调 } } #endregion //=============下载主资源结束=================== }); #endif } }
/// <summary> /// 下载或加载资源 /// </summary> /// <param name="path">短路径</param> /// <param name="name"></param> /// <param name="onComplete"></param> /// <param name="type">0=prefab 1=png</param> public void LoadOrDownload <T>(string path, string name, Action <T> onComplete, byte type) where T : UnityEngine.Object { path = path.ToLower(); #if UNITY_EDITOR && DISABLE_ASSETBUNDLE if (path.Contains("/scene/")) { path = path.Replace(".drb", ".unity"); } else if (path.Contains("/bgm/")) { path = path.Replace(".drb", ".mp3"); } else if (path.Contains("/soundeffect/")) { path = path.Replace(".drb", ".wav"); } else if (path.Contains("/uisource/")) { path = path.Replace(".drb", ".png"); } else if (path.Contains("/material")) { path = path.Replace(".drb", ".mat"); } else if (path.Contains("/font/")) { path = path.Replace(".drb", ".ttf"); } else { path = path.Replace(".drb", ".prefab"); } T asset = UnityEditor.AssetDatabase.LoadAssetAtPath <T>("Assets/" + path); if (asset == null) { int firstIndex = path.IndexOf('/'); string overplusStr = path.Substring(firstIndex + 1, path.Length - firstIndex - 1); int secondIndex = overplusStr.IndexOf('/'); path = path.Replace(path.Substring(firstIndex + 1, secondIndex), "CommonAsset"); asset = UnityEditor.AssetDatabase.LoadAssetAtPath <T>("Assets/" + path); } if (onComplete != null) { onComplete(asset); } #else string fullPath = LocalFileManager.Instance.LocalFilePath + path; if (m_AssetDic.ContainsKey(fullPath + name)) { if (onComplete != null) { onComplete(m_AssetDic[fullPath + name] as T); } return; } LoadManifestFile(); if (m_ManiFest == null) { return; } string[] arrDps = m_ManiFest.GetAllDependencies(path); CheckDpsAsync(0, arrDps, () => { if (!IOUtil.FileExists(fullPath)) { Debug.Log("文件" + fullPath + "不存在"); DownloadDataEntity entity = DownloadManager.Instance.GetServerData(path); if (entity != null) { AssetBundleDownload.Instance.StartCoroutine(AssetBundleDownload.Instance.DownloadData(entity, (bool isSuccess) => { if (isSuccess) { //加载依赖项 for (int i = 0; i < arrDps.Length; ++i) { string dpsPath = LocalFileManager.Instance.LocalFilePath + arrDps[i]; if (!m_DpsAssetBundleLoaderDic.ContainsKey(dpsPath)) { AssetBundleLoader loader = new AssetBundleLoader(arrDps[i]); m_DpsAssetBundleLoaderDic[dpsPath] = loader; } } if (!m_DpsAssetBundleLoaderDic.ContainsKey(fullPath)) { AssetBundleLoader loader = new AssetBundleLoader(path); T obj = loader.LoadAsset <T>(name); m_DpsAssetBundleLoaderDic[fullPath] = loader; m_AssetDic[fullPath + name] = obj; if (onComplete != null) { onComplete(obj); } } else { T obj = m_DpsAssetBundleLoaderDic[fullPath].LoadAsset <T>(name); m_AssetDic[fullPath + name] = obj; if (onComplete != null) { onComplete(obj); } } } })); } else { Debug.LogWarning("资源不存在" + fullPath); } } else { //加载依赖项 for (int i = 0; i < arrDps.Length; ++i) { string dpsPath = LocalFileManager.Instance.LocalFilePath + arrDps[i]; if (!m_DpsAssetBundleLoaderDic.ContainsKey(dpsPath)) { AssetBundleLoader loader = new AssetBundleLoader(arrDps[i]); m_DpsAssetBundleLoaderDic[dpsPath] = loader; } } if (!m_DpsAssetBundleLoaderDic.ContainsKey(fullPath)) { AssetBundleLoader loader = new AssetBundleLoader(path); T obj = loader.LoadAsset <T>(name); m_DpsAssetBundleLoaderDic[fullPath] = loader; m_AssetDic[fullPath + name] = obj; if (onComplete != null) { onComplete(obj); } } else { T obj = m_DpsAssetBundleLoaderDic[fullPath].LoadAsset <T>(name); m_AssetDic[fullPath + name] = obj; if (onComplete != null) { onComplete(obj); } } } }); #endif }