private static void BuildAssetBundleWithBuildMap() { string prefix = "Assets"; AssetBundleBuild[] buildMapArray = new AssetBundleBuild[s_buildMap.Count]; for (int i = 0; i < s_buildMap.Count; i++) { BuildNode build = s_buildMap[i]; string bundlePath = build.path.Substring(prefix.Length + 1); string bundleBaseDir = "dep"; if (!build.isDependcy) { string[] folderArr = bundlePath.Split(new char[] { '/', '\\' }); if (folderArr.Length < 2) { throw new System.Exception("Build assetbundle error, res can't store root folder."); } bundleBaseDir = string.Format("{0}/{1}", folderArr[0], folderArr[1]); } string fileName = Path.GetFileNameWithoutExtension(bundlePath); string bundleName = string.Format("{0}/{1}.ab", bundleBaseDir, fileName); buildMapArray[i].assetBundleName = bundleName; buildMapArray[i].assetNames = new string[] { build.path }; } if (!Directory.Exists(s_assetBundlePath)) { Directory.CreateDirectory(s_assetBundlePath); } BuildPipeline.BuildAssetBundles(s_assetBundlePath, buildMapArray, BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle, EditorUserBuildSettings.activeBuildTarget); }
/// <summary> /// 按照依赖关系的深度,从最底层往上遍历打包,如果一个叶子节点有多个父节点,则该叶子节点被多个资源依赖,该叶子节点需要打包,如果一个节点没有 /// 父节点,则该叶子节点是最顶层的文件(prefab,lua...),需要打包。 /// </summary> private static void BuildResourceBuildMap() { int maxDepth = GetMaxDepthOfLeafNodes(); while (s_leafAssetNodes.Count > 0) { List <AssetNode> curDepthNodesList = new List <AssetNode>(); for (int i = 0; i < s_leafAssetNodes.Count; i++) { if (s_leafAssetNodes[i].depth == maxDepth) { // 如果叶子节点有多个父节点或者没有父节点,打包该叶子节点 int parentCount = s_leafAssetNodes[i].parents.Count; if (parentCount != 1) { if (!ShouldIgnoreFile(s_leafAssetNodes[i].path)) { BuildNode build = new BuildNode(); build.path = s_leafAssetNodes[i].path; if (parentCount < 1) { build.isDependcy = false; } else { build.isDependcy = true; } s_buildMap.Add(build); } } curDepthNodesList.Add(s_leafAssetNodes[i]); } } //删除已经遍历过的叶子节点,并把这些叶子节点的父节点添加到新一轮的叶子节点中 for (int i = 0; i < curDepthNodesList.Count; i++) { s_leafAssetNodes.Remove(curDepthNodesList[i]); foreach (AssetNode node in curDepthNodesList[i].parents) { if (!s_leafAssetNodes.Contains(node)) { s_leafAssetNodes.Add(node); } } } maxDepth -= 1; } }