public DevResourceMgr() { willdoTaskSet = new HashSet <int>(); allTaskList = new List <LoaderTaskGroup>(); objsMap = new Dictionary <string, UnityEngine.Object>(); // allResourceList = BApplication.GetAllAssetsPath(); }
// /// <summary> /// 生成AssetBundle /// </summary> /// <param name="resRootPath"></param> /// <param name="outPath"></param> /// <param name="target"></param> public static void GenAssetBundle( string outPath, BuildTarget target, BuildAssetBundleOptions options = BuildAssetBundleOptions.ChunkBasedCompression, bool isClearAssets = true) { //0.cache path的路径 cachePath = IPath.Combine(outPath, "Art/Cache.json"); configPath = IPath.Combine(outPath, "Art/Config.json"); // var fileList = BApplication.GetAllAssetsPath(); var artOutpath = IPath.Combine(outPath, "Art"); //2.分析ab包 AnalyzeResource(fileList.ToArray(), target, artOutpath); //3.生成AssetBundle BuildAssetBundle(target, outPath, options); //assetBundle 转 hash foreach (var item in allfileHashMap) { var sub = item.Key.Replace(BApplication.ProjectRoot + "/", "").ToLower(); var source = IPath.Combine(artOutpath, sub); var copyto = IPath.Combine(artOutpath, item.Value); if (File.Exists(source) && !File.Exists(copyto)) { File.Copy(source, copyto); } } Directory.Delete(IPath.Combine(artOutpath, "assets"), true); //保存配置 FileHelper.WriteAllText(configPath, CurManifestConfig.ToString()); //保存Cache.json FileHelper.WriteAllText(cachePath, JsonMapper.ToJson(allfileHashMap, true)); //4.清除AB Name if (isClearAssets) { RemoveAllAssetbundleName(); } //删除无用文件 var delFiles = Directory.GetFiles(artOutpath, "*", SearchOption.AllDirectories); foreach (var df in delFiles) { var ext = Path.GetExtension(df); if (ext == ".meta" || ext == ".manifest") { File.Delete(df); } //避免删除配置 if (df.EndsWith("Cache.json") || df.EndsWith("Config.json")) { continue; } // var fn = Path.GetFileName(df); var item = CurManifestConfig.GetManifestItemByHash(fn); if (item == null) { File.Delete(df); } } }
/// <summary> /// 生成AssetBundle /// </summary> /// <param name="outPath">导出目录</param> /// <param name="target">平台</param> /// <param name="options">打包参数</param> /// <param name="isHashName">是否为hash name</param> public static void GenAssetBundle(string outPath, BuildTarget target, BuildAssetBundleOptions options = BuildAssetBundleOptions.ChunkBasedCompression, bool isHashName = false, string AES = "") { // var artOutpath = IPath.Combine(outPath, "Art"); var builinfoPath = IPath.Combine(outPath, "BuildInfo.json"); //初始化 allfileHashMap = new Dictionary <string, string>(); var assetPaths = BApplication.GetAllAssetsPath(); for (int i = 0; i < assetPaths.Count; i++) { assetPaths[i] = assetPaths[i].ToLower(); } /***********************新老资源依赖生成************************/ //获取老的配置 BuildInfo lastBuildInfo = new BuildInfo(); if (File.Exists(builinfoPath)) { var content = File.ReadAllText(builinfoPath); lastBuildInfo = JsonMapper.ToObject <BuildInfo>(content); } //获取当前配置 var newbuildInfo = GetAssetsInfo(assetPaths); //获取变动的数据 var changedAssetList = GetChangedAssets(lastBuildInfo, newbuildInfo); if (File.Exists(builinfoPath)) { string targetPath = outPath + "/BuildInfo.old.json"; File.Delete(targetPath); File.Move(builinfoPath, targetPath); } FileHelper.WriteAllText(builinfoPath, JsonMapper.ToJson(newbuildInfo)); /***********************整理依赖关系 减少消耗************************/ //保存buildinfo后, //整理runtime路径,减少加载时候的消耗 var runtimeStr = "/runtime/"; foreach (var asset in newbuildInfo.AssetDataMaps) { if (asset.Value.Name.Contains(runtimeStr)) { var newName = asset.Value.Name; //移除runtime之前的路径 var index = newName.IndexOf(runtimeStr); newName = newName.Substring(index + 1); //runtimeStr.Length); //去除后缀 newName = newName.Replace(Path.GetExtension(newName), ""); //刷新整个列表替换 foreach (var _asset in newbuildInfo.AssetDataMaps) { var oldName = asset.Key.ToLower(); //name替换 if (_asset.Value.Name == oldName) { _asset.Value.Name = newName; } //ab替换 if (_asset.Value.AB == oldName) { _asset.Value.AB = newName; } //依赖替换 for (int i = 0; i < _asset.Value.DependList.Count; i++) { if (_asset.Value.DependList[i] == oldName) { _asset.Value.DependList[i] = newName; } } } } } /***********************生成Config************************/ //根据buildinfo 生成ArtConfig ManifestConfig manifest = new ManifestConfig(); manifest.AES = AES; if (isHashName) { // foreach (var item in newbuildInfo.AssetDataMaps) // { // var dependlist = new List<string>(item.Value.DependList.Count); // for (int i = 0; i < dependlist.Count; i++) // { // var assetName = item.Value.DependList[i]; // // var asset = newbuildInfo.AssetDataMaps[assetName]; // dependlist[i] = asset.Hash; // } // // //添加manifest // var path = !string.IsNullOrEmpty(item.Value.AB) ? item.Value.AB : item.Key; // var mi = new ManifestItem(path, (ManifestItem.AssetTypeEnum) item.Value.Type, dependlist); // configMap[item.Key] = mi; // } } else { foreach (var item in newbuildInfo.AssetDataMaps) { //添加manifest var path = !string.IsNullOrEmpty(item.Value.AB) ? item.Value.AB : item.Value.Name; var mi = new ManifestItem(path, (ManifestItem.AssetTypeEnum)item.Value.Type, new List <string>(item.Value.DependList)); //runtime路径下,改成用Resources加载规则命名的key if (path.StartsWith("runtime/")) { manifest.AddManifest(item.Value.Name, mi); } else { manifest.AddManifest(item.Key, mi); } } } //hash命名 //写入 FileHelper.WriteAllText(artOutpath + "/Config.json", JsonMapper.ToJson(manifest)); /***********************开始设置build ab************************/ //设置AB name foreach (var asset in changedAssetList.AssetDataMaps) { string abname = ""; if (!string.IsNullOrEmpty(asset.Value.AB)) { abname = asset.Value.AB; } else { abname = asset.Value.Name; } var ai = GetAssetImporter(asset.Key); if (ai) { ai.assetBundleName = abname; } } //3.生成AssetBundle BuildAssetBundle(target, outPath, options); //4.清除AB Name RemoveAllAssetbundleName(); AssetImpoterCacheMap.Clear(); //the end.删除无用文件 var delFiles = Directory.GetFiles(artOutpath, "*", SearchOption.AllDirectories); foreach (var df in delFiles) { var ext = Path.GetExtension(df); if (ext == ".meta" || ext == ".manifest") { File.Delete(df); } } }