/// <summary> /// 拷贝初始目录所有文件 /// </summary> IEnumerator CopyAllInitialFiles() { //拷贝所有配置文件 yield return(DownLoadCommon.StartCopyInitialFile(DownLoadCommon.MAIN_MANIFEST_FILE_NAME)); string mainifestFullName = DownLoadCommon.GetFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME); AssetBundleManifest initial = DownLoadCommon.LoadMainManifestByPath(mainifestFullName); if (initial == null) { yield break; } //拷贝AssetBundle文件 string[] all_assetbundle = initial.GetAllAssetBundles(); this.InitTotal = all_assetbundle.Length; for (int i = 0; i < all_assetbundle.Length; ++i) { this.InitCurrent = i; string name = all_assetbundle[i]; yield return(DownLoadCommon.StartCopyInitialFile(name)); } //拷贝结束资源 保证安装包拷贝结束 yield return(DownLoadCommon.StartCopyInitialFile(DownLoadCommon.END_RESOUCES_FILE_NAME)); }
/// <summary> /// 更新AssetBundle /// </summary> IEnumerator StartUpdateAssetBundle() { Debug.Log("AssetUpdater:StartUpdateAssetBundle"); if (ErrorCode != EmErrorCode.None) { yield break; } UpdateCompleteValue(0f, 0f); ////载入MainManifest AssetBundleManifest manifest = ZTAssetBundleManager.GetInstance().MainManifest; //载入新的ResourcesManifest string file = DownLoadCommon.GetCacheFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME); AssetBundleManifest new_manifest = DownLoadCommon.LoadMainManifestByPath(file); if (new_manifest == null) { Error(EmErrorCode.LoadNewMainManifestFailed , "Can't find new version MainManifest!"); yield break; } ////获取需下载的资源列表与删除的资源的列表 List <string> download_files = new List <string>(); List <string> delete_files = new List <string>(); CompareAssetBundleDifference(ref download_files, ref delete_files , manifest, new_manifest); //删除已废弃的文件 if (delete_files.Count > 0) { for (int i = 0; i < delete_files.Count; ++i) { string full_name = DownLoadCommon.GetFileFullName(delete_files[i]); if (File.Exists(full_name)) { File.Delete(full_name); yield return(0); } } } //更新所有需下载的资源 _ab_download = new AssetBundleDownloader(_current_ab_url); _ab_download.Start(DownLoadCommon.PATH, download_files); while (!_ab_download.IsDone) { UpdateCompleteValue(_ab_download.CompletedSize, _ab_download.TotalSize); yield return(0); } if (_ab_download.IsFailed) { Error(EmErrorCode.DownloadAssetBundleFailed); yield break; } }
/// <summary> /// 初始化 /// </summary> IEnumerator Preprocess() { //创建资源根目录 if (!Directory.Exists(DownLoadCommon.PATH)) { Directory.CreateDirectory(DownLoadCommon.PATH); } //判断主资源文件是否存在,不存在则拷贝备份资源至资源根目录 bool do_initial_copy = false; string resources_manifest_file = DownLoadCommon.GetFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME); if (!File.Exists(resources_manifest_file)) { do_initial_copy = true; } else { // 拷贝安装包初始化目录中的ResourcesManifest,并判断是否重新拷贝初始化目录下的所有文件 //string initial_full_name = DownLoadCommon.GetInitialFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME); //string cache_full_name = DownLoadCommon.GetCacheFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME); //yield return DownLoadCommon.StartCopyFile(initial_full_name, cache_full_name); ////判断安装包初始目录是否完整 //ResourcesManifest initial = DownLoadCommon.LoadResourcesManifestByPath(cache_full_name); //if (initial == null) //{ // Error(EmErrorCode.PreprocessError // , "Initial path don't contains " // + DownLoadCommon.RESOURCES_MANIFEST_FILE_NAME + "!"); // yield break; //} //ResourcesManifest current = DownLoadCommon.LoadResourcesManifestByPath(full_name); //if (current == null) // do_initial_copy = true; //else if (current.Data.Version < initial.Data.Version) // do_initial_copy = true; //删除缓存中的文件 //if (File.Exists(cache_full_name)) // File.Delete(cache_full_name); } if (do_initial_copy) { yield return(CopyAllInitialFiles()); } PreprocessFinished(); }
/// <summary> /// 判断一个AssetBundle是否存在缓存 /// </summary> public bool IsExist(string assetbundlename) { if (!IsReady) { return(false); } if (string.IsNullOrEmpty(assetbundlename)) { return(false); } return(File.Exists(DownLoadCommon.GetFileFullName(assetbundlename))); }
/// <summary> /// 拷贝文件并覆盖旧数据文件 /// </summary> IEnumerator StartCopyCacheFile() { Debug.Log("AssetUpdater:StartCopyCacheFile"); if (ErrorCode != EmErrorCode.None) { yield break; } string str = DownLoadCommon.GetCacheFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME); string dest = DownLoadCommon.GetFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME); UpdateCompleteValue(1, 1); yield return(DownLoadCommon.StartCopyFile(str, dest)); }
IEnumerator LoadAsyncCoroutine(string assetbundlename) { //临时 if (!IsExist(assetbundlename)) { _loadSyncAbNames.RemoveAt(0); if (_loadSyncAbList.ContainsKey(assetbundlename)) { List <Action <AssetBundle> > callbackList = _loadSyncAbList[assetbundlename]; for (int i = 0; i < callbackList.Count; i++) { Action <AssetBundle> callback = callbackList[i]; callback(null); } _loadSyncAbList.Remove(assetbundlename); } yield break; } string assetbundle_path = DownLoadCommon.GetFileFullName(assetbundlename); AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync(assetbundle_path); //Debug.LogWarning(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + assetbundlename); while (!abcr.isDone) { yield return(null); } //Debug.LogWarning("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" + assetbundlename); if (null != abcr.assetBundle) { SaveInCahce(assetbundlename, abcr.assetBundle); _loadSyncAbNames.RemoveAt(0); } if (_loadSyncAbList.ContainsKey(assetbundlename)) { List <Action <AssetBundle> > callbackList = _loadSyncAbList[assetbundlename]; for (int i = 0; i < callbackList.Count; i++) { Action <AssetBundle> callback = callbackList[i]; callback(abcr.assetBundle); } _loadSyncAbList.Remove(assetbundlename); } }
/// <summary> /// 拷贝初始目录所有文件 /// </summary> IEnumerator CopyAllInitialFiles() { //拷贝所有配置文件 yield return(DownLoadCommon.StartCopyInitialFile(DownLoadCommon.MAIN_MANIFEST_FILE_NAME)); string initial_full_name = DownLoadCommon.GetFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME); AssetBundleManifest initial = DownLoadCommon.LoadMainManifestByPath(initial_full_name); if (initial == null) { yield break; } //拷贝AssetBundle文件 string[] all_assetbundle = initial.GetAllAssetBundles(); for (int i = 0; i < all_assetbundle.Length; ++i) { string name = all_assetbundle[i]; yield return(DownLoadCommon.StartCopyInitialFile(name)); } //ResourcesManifest resources_manifest = DownLoadCommon.LoadResourcesManifest(); //if (resources_manifest == null) //{ // Debug.LogWarning("Can't load ResourcesManifest file!"); // yield break; //} //var itr = resources_manifest.Data.AssetBundles.GetEnumerator(); //while (itr.MoveNext()) //{ // if (itr.Current.Value.IsNative) // { // string assetbundlename = itr.Current.Value.AssetBundleName; // string dest = DownLoadCommon.GetFileFullName(assetbundlename); // //保证路径存在 // string directory = Path.GetDirectoryName(dest); // if (!Directory.Exists(directory)) // Directory.CreateDirectory(directory); // //拷贝数据 // yield return DownLoadCommon.StartCopyInitialFile(assetbundlename); // } //} //itr.Dispose(); }
/// <summary> /// 初始化(检测资源完整性) /// </summary> IEnumerator Preprocess() { //创建资源根目录 if (!Directory.Exists(DownLoadCommon.PATH)) { Directory.CreateDirectory(DownLoadCommon.PATH); } //判断主资源文件 和 初始化结束资源 是否存在,不存在则拷贝备份资源至资源根目录 string resources_manifest_file = DownLoadCommon.GetFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME); string resources_end_File = DownLoadCommon.GetFileFullName(DownLoadCommon.END_RESOUCES_FILE_NAME); if (!File.Exists(resources_manifest_file) || !File.Exists(resources_end_File)) { yield return(CopyAllInitialFiles()); } PreprocessFinished(); }
/// <summary> /// 加载AssetBundle /// </summary> AssetBundle LoadAssetBundle(string assetbundlename) { if (assetbundlename == null || MainManifest == null) { return(null); } AssetBundle ab = LoadInCahce(assetbundlename); if (null != ab) { return(ab); } string assetbundle_path = DownLoadCommon.GetFileFullName(assetbundlename); if (System.IO.File.Exists(assetbundle_path)) { ab = AssetBundle.LoadFromFile(assetbundle_path); SaveInCahce(assetbundlename, ab); } return(ab); }
/// <summary> /// 载入Manifest /// </summary> public static AssetBundleManifest LoadMainManifest() { string file = DownLoadCommon.GetFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME); return(LoadMainManifestByPath(file)); }
//<summary> // 比较AssetBundle差异,获得下载列表与删除列表 //</summary> static void CompareAssetBundleDifference(ref List <string> download_files , ref List <string> delete_files , AssetBundleManifest old_manifest , AssetBundleManifest new_manifest ) { if (download_files != null) { download_files.Clear(); } if (delete_files != null) { delete_files.Clear(); } if (old_manifest == null) { return; } if (new_manifest == null) { return; } //采用位标记的方式判断资源 //位标记: 0: 存在旧资源中 1: 存在新资源中 2:本地资源标记 int old_version_bit = 0x1; // 存在旧资源中 int new_version_bit = 0x2; // 存在新资源中 Dictionary <string, int> temp_dic = new Dictionary <string, int>(); //标记旧资源 string[] all_assetbundle = old_manifest.GetAllAssetBundles(); for (int i = 0; i < all_assetbundle.Length; ++i) { string name = all_assetbundle[i]; _SetDictionaryBit(ref temp_dic, name, old_version_bit); } //标记新资源 string[] new_all_assetbundle = new_manifest.GetAllAssetBundles(); for (int i = 0; i < new_all_assetbundle.Length; ++i) { string name = new_all_assetbundle[i]; _SetDictionaryBit(ref temp_dic, name, new_version_bit); } //获得对应需操作的文件名, 优先级: both > add > delete //both: 第0位与第1位都被标记的 //delete: 仅第0位被标记的 //add: 第2位未标记,且第3位被标记的 int both_bit = old_version_bit | new_version_bit; // 二个版本资源都存在 List <string> add_files = new List <string>(); List <string> both_files = new List <string>(); var itr = temp_dic.GetEnumerator(); while (itr.MoveNext()) { string name = itr.Current.Key; int mask = itr.Current.Value; if ((mask & new_version_bit) == new_version_bit && (mask & old_version_bit) == 0) { add_files.Add(name); } else if ((mask & both_bit) == both_bit) { both_files.Add(name); } else if ((mask & old_version_bit) == old_version_bit) { delete_files.Add(name); } } itr.Dispose(); //载入下载缓存数据 DownloadCache download_cache = new DownloadCache(); download_cache.Load(DownLoadCommon.DOWNLOADCACHE_FILE_PATH); if (!download_cache.HasData()) { download_cache = null; } //记录需下载的文件 { //加入新增的文件 download_files.AddRange(add_files); //比较所有同时存在的文件,判断哪些需要更新 for (int i = 0; i < both_files.Count; ++i) { string name = both_files[i]; string full_name = DownLoadCommon.GetFileFullName(name); if (File.Exists(full_name)) { //判断哈希值是否相等 string old_hash = old_manifest.GetAssetBundleHash(name).ToString(); string new_hash = new_manifest.GetAssetBundleHash(name).ToString(); if (old_hash.CompareTo(new_hash) == 0) { continue; } download_files.Add(name); } else { download_files.Add(name); } } //过滤缓存中已下载的文件 if (download_cache != null) { var cache_itr = download_cache.Data.AssetBundles.GetEnumerator(); while (cache_itr.MoveNext()) { DownloadCacheData.AssetBundle elem = cache_itr.Current.Value; string name = elem.AssetBundleName; string full_name = DownLoadCommon.GetFileFullName(name); if (File.Exists(full_name)) { string cache_hash = elem.Hash; string new_hash = new_manifest.GetAssetBundleHash(name).ToString(); if (!string.IsNullOrEmpty(cache_hash) && cache_hash.CompareTo(new_hash) == 0) { download_files.Remove(name); } } } } } }
private Dictionary <string, Object> _assetDic; //已经读取资源 /// <summary> /// 初始化 /// </summary> /// <param name="abName"></param> public ZTAssetBundle(string abName) { this.AbPath = DownLoadCommon.GetFileFullName(abName);; this._assetDic = new Dictionary <string, Object>(); this.Load(); }