/// 生成打包参数 EPackHandlerParam GeneratePackParam(BundlePackInfo ResInfo) { EPackHandlerParam packHandlerParam = EPackHandlerParam.None; if (ResInfo.HasFlag(EBundleFlag.UnCompress)) { packHandlerParam |= EPackHandlerParam.UnCompress; } if (ResInfo.HasFlag(EBundleFlag.LZMA)) { packHandlerParam |= EPackHandlerParam.LZMA; } if (ResInfo.HasFlag(EBundleFlag.LZ4)) { packHandlerParam |= EPackHandlerParam.LZ4; } return(packHandlerParam); }
/// <summary> /// 卸载资源列表对应bundle /// </summary> /// <param name="resourcesPath"></param> public void UnloadBundle(JWObjList <string> resourcesPath) { JWObjList <BundlePackInfo> bundleList = GetBundlePackInfoListForResources(resourcesPath, false); if (bundleList != null && bundleList.Count > 0) { for (int i = 0; i < bundleList.Count; i++) { BundlePackInfo packInfo = bundleList[i]; // unity场景 if (packInfo.HasFlag(EBundleFlag.UnityScene)) { BundleService.GetInstance().OnAssetLoaded(packInfo); } else { BundleService.GetInstance().Unload(bundleList[i]); } } } }
/// <summary> /// 获取预加载bundle列表 /// </summary> /// <returns></returns> public JWObjList <string> GetPreloadBundles() { JWObjList <string> bundleList = new JWObjList <string>(); ResPackConfig config = ResService.GetInstance().PackConfig; for (int i = 0; i < config.PackInfo.Count; i++) { BundlePackInfo p = config.PackInfo[i] as BundlePackInfo; if (p == null) { continue; } if (p.HasFlag(EBundleFlag.PreLoad)) { bundleList.Add(p.Path); } } return(bundleList); }
/// 打包AssetBundle资源, void BuildBundleResource(BundlePackInfo packInfo, string ifsBuildDir, BuildTarget target) { if (packInfo == null) { return; } // 本次打包的路径和目录 string buildBundlePath = JW.Res.FileUtil.CombinePaths(ifsBuildDir, "", packInfo.Path); string buildBundleDirectory = JW.Res.FileUtil.GetFullDirectory(buildBundlePath); if (!JW.Res.FileUtil.IsDirectoryExist(buildBundleDirectory)) { JW.Res.FileUtil.CreateDirectory(buildBundleDirectory); } //========== build ========== List <string> filePathList = new List <string>(); List <string> nameList = new List <string>(); for (int j = 0; j < packInfo.Resources.Count; j++) { ResInfo res = packInfo.Resources[j]; filePathList.Add(res.RelativePath); nameList.Add(res.Path); } bool succ = false; //场景文件 if (packInfo.HasFlag(EBundleFlag.UnityScene)) { succ = ResPackHelper.BuildScene(filePathList, buildBundlePath); } else { //全局模式只是纯粹设置bundle 名称 if (_packConfig.IsGlobalBuild) { succ = ResPackHelper.JustSetBundleName(packInfo.Path, filePathList); } else { EPackHandlerParam packHandlerParam = GeneratePackParam(packInfo); succ = ResPackHelper.Build(buildBundlePath, filePathList, packHandlerParam, true, nameList); } } if (succ) { if (_packConfig.IsGlobalBuild) { JW.Common.Log.LogD("Set BundleName Success " + buildBundlePath); } else { JW.Common.Log.LogD("Build Bundle Success Out" + buildBundlePath); } } else { if (_packConfig.IsGlobalBuild) { Terminate("Set bundleName failed, path:" + buildBundlePath); } else { Terminate("Build bundle failed, path:" + buildBundlePath); } } }
/// 解析Asset配置节点 bool ParseAsset(XmlNode node, BundlePackInfo packInfo) { // source path if (node.Attributes["SrcPath"] == null || string.IsNullOrEmpty(node.Attributes["SrcPath"].Value)) { JW.Common.Log.LogE("There is no SrcPath in Asset node."); return(false); } string srcPath = node.Attributes["SrcPath"].Value; string fullPath = JW.Res.FileUtil.CombinePaths(Application.dataPath, "Resources", srcPath); string[] files = null; try { if (File.Exists(fullPath)) { //单个文件 if (node.Attributes["pattern"] != null) { Regex regex = new Regex(node.Attributes["pattern"].Value, RegexOptions.IgnoreCase); if (regex.IsMatch(fullPath)) { files = new string[1]; files[0] = fullPath; } } else { files = new string[1]; files[0] = fullPath; } } else if (Directory.Exists(JW.Res.FileUtil.GetFullDirectory(fullPath))) { //目录 SearchOption option = SearchOption.TopDirectoryOnly; if (node.Attributes["recursive"] != null && node.Attributes["recursive"].Value.ToLower() == "true") { option = SearchOption.AllDirectories; } files = Directory.GetFiles(JW.Res.FileUtil.GetFullDirectory(fullPath), JW.Res.FileUtil.GetFullName(fullPath), option).Where(name => !name.EndsWith(".meta", true, null)).ToArray(); if (node.Attributes["pattern"] != null) { Regex regex = new Regex(node.Attributes["pattern"].Value, RegexOptions.IgnoreCase); files = files.Where(f => !string.IsNullOrEmpty(f) && regex.IsMatch(f)).ToArray(); } } } catch (System.Exception e) { JW.Common.Log.LogE(string.Format("ParseAsset(), path:{0}, exception:{1}", packInfo.Path, e.Message)); return(false); } // 创建资源信息 resource info for (int j = 0; files != null && j < files.Length; j++) { string filePathInResources = GetPathInResources(files[j]); string duplicatedFile = null; if (packInfo.IsDuplicated(filePathInResources, out duplicatedFile)) { JW.Common.Log.LogE(string.Format("Duplicated resource [{0}] and [{1}] in bundle [{2}]", filePathInResources, duplicatedFile, packInfo.Path)); return(false); } ResInfo ResInfo = new ResInfo(); if (packInfo.HasFlag(EBundleFlag.UnityScene)) { ResInfo.Path = Path.GetFileNameWithoutExtension(filePathInResources); } else { ResInfo.Path = JW.Res.FileUtil.EraseExtension(filePathInResources); } int artPos = filePathInResources.IndexOf("ArtWork"); if (artPos != -1) { ResInfo.Path = ResInfo.Path.Substring(artPos + "ArtWork/".Length); ResInfo.RelativePath = JW.Res.FileUtil.CombinePath("Assets", filePathInResources.Substring(artPos)); } else { ResInfo.RelativePath = JW.Res.FileUtil.CombinePath("Assets/Resources", filePathInResources); } //扩展名 ResInfo.Ext = JW.Res.FileUtil.GetExtension(filePathInResources); //是否保持 if (node.Attributes["keep"] != null && node.Attributes["keep"].Value.Equals("true", StringComparison.OrdinalIgnoreCase)) { ResInfo.Keep = true; } else { ResInfo.Keep = false; } packInfo.Add(ref ResInfo); } return(true); }
/// 解析AssetBundle 配置节点 bool ParseBundle(XmlNode node, BuildTarget target, bool allowEmpty = true) { BundlePackInfo info = new BundlePackInfo(); // 输出文件名 info.Path = node.Attributes["DstPath"].Value; // if (node.Attributes["flags"] != null) { info.Flags = ParseResourceFlags(node.Attributes["flags"].Value); } else { //没有也需要添加如果 全局模式 if (IsGlobalBuild) { info.Flags = ParseResourceFlags(""); } } // bundle生命周期 if (node.Attributes["life"] != null) { info.Life = ParseBundleLife(node.Attributes["life"].Value); } //包里面资产配置 for (int i = 0; i < node.ChildNodes.Count; ++i) { if (node.ChildNodes[i].Name == "Asset") { bool isOK = ParseAsset(node.ChildNodes[i], info); if (isOK == false) { return(false); } } } // 没有资源 if (info.Resources.Count == 0) { if (allowEmpty || info.HasFlag(EBundleFlag.Optional)) { JW.Common.Log.LogW("No file, but ignore." + info.Path); return(true); } else { JW.Common.Log.LogW(string.Format("No asset for bundle {0}.", info.Path)); return(false); } } // 是否有同名bundle string duplicatedBundle = null; if (IsDuplicated(info, out duplicatedBundle)) { JW.Common.Log.LogE(string.Format("Bundle {0} name duplicated with {1}.", info.Path, duplicatedBundle)); return(false); } //加入配置 _config.AddPackInfo(info); return(true); }