protected static string setFileAssetBundleName(string file, bool forceSingle = false) { // .asset文件和.meta不打包 if (file.EndsWith(".meta") || file.EndsWith(".asset")) { return(""); } string bundleName = ""; AssetImporter importer = AssetImporter.GetAtPath(file); if (importer != null) { string fileName = file.ToLower(); rightToLeft(ref fileName); bundleName = getFileAssetBundleName(fileName, forceSingle); importer.assetBundleName = bundleName; EditorUtility.UnloadUnusedAssetsImmediate(); // 存储bundleInfo AssetBuildBundleInfo info = new AssetBuildBundleInfo(); info.assetName = fileName.Substring(CommonDefine.P_RESOURCE_PATH.Length); // 去除Asset/Resources/前缀,只保留Resources下相对路径 info.bundleName = bundleName; if (!mAssetBundleMap.ContainsKey(info.bundleName)) { mAssetBundleMap.Add(info.bundleName, new List <AssetBuildBundleInfo>()); } mAssetBundleMap[info.bundleName].Add(info); } else { Debug.LogFormat("Set AssetName Fail, File:{0}, Msg:Importer is null", file); } return(bundleName); }
// 刷新指定文件的所属AssetBundle名字 protected static string refreshFileAssetBundleName(string file, bool forceSingle = false) { if (endWith(file, ".meta")) { return(EMPTY); } AssetImporter importer = AssetImporter.GetAtPath(file); if (importer == null) { Debug.LogError("Set AssetName Fail, File:" + file); return(EMPTY); } // tpsheet文件不打包 // LightingData.asset文件不能打包AB,这是一个特殊文件,只用于编辑器 if (endWith(file, ".tpsheet") || endWith(file, "LightingData.asset")) { importer.assetBundleName = EMPTY; return(EMPTY); } string fileName = rightToLeft(file.ToLower()); string bundleName = getFileAssetBundleName(fileName, forceSingle); if (importer.assetBundleName != bundleName) { importer.assetBundleName = bundleName; } // 存储bundleInfo // 去除Asset/GameResources/前缀,只保留GameResources下相对路径 string assetName = removeStartString(fileName, FrameDefine.P_GAME_RESOURCES_PATH, false); if (!mAssetBundleMap.TryGetValue(bundleName, out AssetBuildBundleInfo bundleInfo)) { bundleInfo = new AssetBuildBundleInfo(bundleName); mAssetBundleMap.Add(bundleName, bundleInfo); } bundleInfo.mAssetNames.Add(assetName); return(bundleName); }
// subPath为以Asset开头的相对路径 public static void packAssetBundle(BuildTarget target, string subPath = EMPTY) { if (isEmpty(subPath)) { subPath = AssetDatabase.GetAssetPath(Selection.activeObject); } AssetBundleBuild[] buildList = null; if (!isEmpty(subPath)) { if (!EditorUtility.DisplayDialog("打包", "确认打包" + subPath + "?", "确认", "取消")) { return; } findAssetBundleBuild(subPath, ref buildList); } else { Debug.Log("打包全部AssetBundle"); } DateTime time0 = DateTime.Now; if (buildList != null) { // 部分打包,仅重新生成资源包文件,清单文件也会一起生成,但是由于只是部分打包,所以依赖项可能没有打包,需要手动还原清单文件 int count = buildList.Length; for (int i = 0; i < count; ++i) { string bundleFileName = FrameDefine.P_STREAMING_ASSETS_PATH + buildList[i].assetBundleName; if (File.Exists(bundleFileName)) { File.Delete(bundleFileName); } } BuildPipeline.BuildAssetBundles(FrameDefine.P_STREAMING_ASSETS_PATH, buildList, BuildAssetBundleOptions.ChunkBasedCompression, target); AssetDatabase.Refresh(); } else { // 清理输出目录 createOrClearOutPath(); // 清理不打包的AssetBundle名 List <string> allFiles = new List <string>(); findFiles(FrameDefine.F_GAME_RESOURCES_PATH, allFiles); clearUnPackAssetBundleName(allFiles, GameDefine.mUnPackFolder); // 设置bunderName mAssetBundleMap.Clear(); List <string> resList = new List <string>(); getAllSubResDirs(FrameDefine.P_GAME_RESOURCES_PATH, resList); foreach (string dir in resList) { if (!setAssetBundleName(dir)) { return; } } // 打包 BuildPipeline.BuildAssetBundles(FrameDefine.P_STREAMING_ASSETS_PATH, BuildAssetBundleOptions.ChunkBasedCompression, target); AssetDatabase.Refresh(); // 构建依赖关系 mDependencyList.Clear(); AssetBundle assetBundle = AssetBundle.LoadFromFile(FrameDefine.F_STREAMING_ASSETS_PATH + "StreamingAssets"); AssetBundleManifest manifest = assetBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); string[] assetBundleNameList = manifest.GetAllAssetBundles(); // 遍历所有AB foreach (string bundle in assetBundleNameList) { string bundleName = bundle; if (!mAssetBundleMap.TryGetValue(bundleName, out AssetBuildBundleInfo bundleInfo)) { continue; } rightToLeft(ref bundleName); string[] deps = manifest.GetAllDependencies(bundleName); HashSet <string> dependencySet = new HashSet <string>(); // 遍历当前AB的所有依赖项 foreach (string dep in deps) { string depName = rightToLeft(dep); bundleInfo.AddDependence(depName); dependencySet.Add(depName); // 查找依赖项中是否有依赖当前AssetBundle的 if (mDependencyList.TryGetValue(depName, out HashSet <string> depList) && depList.Contains(bundleName)) { messageBox("AssetBundle dependency error! " + depName + ", " + bundleName, true); } } if (mDependencyList.ContainsKey(bundleName)) { messageBox("已经存在一个名为:" + bundleName + "的AssetBundle", true); } mDependencyList.Add(bundleName, dependencySet); } // 生成配置文件 Serializer serializer = new Serializer(); serializer.write(mAssetBundleMap.Count); foreach (var item in mAssetBundleMap) { AssetBuildBundleInfo bundleInfo = item.Value; // AssetBundle名字 serializer.writeString(bundleInfo.mBundleName); // AssetBundle所包含的所有Asset名字 int assetCount = bundleInfo.mAssetNames.Count; serializer.write(assetCount); for (int i = 0; i < assetCount; ++i) { serializer.writeString(bundleInfo.mAssetNames[i]); } // AssetBundle依赖的所有AssetBundle int depCount = bundleInfo.mDependencies.Count; serializer.write(depCount); for (int j = 0; j < depCount; ++j) { serializer.writeString(bundleInfo.mDependencies[j]); } } string filePath = FrameDefine.F_STREAMING_ASSETS_PATH + "StreamingAssets.bytes"; writeFile(filePath, serializer.getBuffer(), serializer.getDataSize()); } messageBox("资源打包结束! 耗时 : " + (DateTime.Now - time0), false); }
public static void Pack() { // 清理输出目录 CreateOrClearOutPath(); // 清理之前设置过的bundleName ClearAssetBundleName(); // 设置bunderName bundleMap.Clear(); List <string> resList = new List <string>(); for (int i = 0; i < RES_DIRS.Length; i++) { resList.AddRange(GetAllResDirs(RES_DIRS[i])); } foreach (string dir in resList) { setAssetBundleName(dir); } // 打包 BuildPipeline.BuildAssetBundles(RES_OUTPUT_PATH, BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows64); AssetDatabase.Refresh(); // 构建依赖关系 AssetBundle assetBundle = AssetBundle.LoadFromFile(FileUtils.getPath("StreamingAssets")); AssetBundleManifest mainfest = assetBundle.LoadAsset <AssetBundleManifest> ("AssetBundleManifest"); string[] bundleNames = mainfest.GetAllAssetBundles(); foreach (string bundleName in bundleNames) { string[] deps = mainfest.GetAllDependencies(bundleName); foreach (string dep in deps) { List <AssetBuildBundleInfo> infoList = null; bundleMap.TryGetValue(bundleName, out infoList); if (null != infoList) { foreach (AssetBuildBundleInfo info in infoList) { info.AddDependence(dep); } } } } assetBundle.Unload(true); assetBundle = null; // 生成XML doc = new XMLDocment(); doc.startObject("files"); foreach (KeyValuePair <string, AssetBuildBundleInfo> pair in fileMap) { AssetBuildBundleInfo info = pair.Value; doc.startObject("file"); doc.createElement("bundleName", info.bundleName); doc.createElement("fileName", info.fileName); doc.createElement("assetName", info.assetName); if (null != info.dependencies) { doc.startObject("deps"); foreach (string dep in info.dependencies) { doc.createElement("dep", dep); } doc.endObject("deps"); } doc.endObject("file"); } doc.endObject("files"); FileStream fs = new FileStream(Path.Combine(RES_OUTPUT_PATH, "StreamingAssets.xml"), FileMode.Create); byte[] data = System.Text.Encoding.UTF8.GetBytes(doc.ToString()); fs.Write(data, 0, data.Length); fs.Flush(); fs.Close(); // 打包后的清理 ClearOutPath(); }
/// <summary> /// 设置AssetBundleName /// </summary> /// <param name="fullpath">Fullpath.</param> public static void setAssetBundleName(string fullPath) { string[] files = System.IO.Directory.GetFiles(fullPath); if (files == null || files.Length == 0) { return; } Debug.Log("Set AssetBundleName Start......"); string dirBundleName = fullPath.Substring(BASE_PATH.Length); dirBundleName = dirBundleName.Replace("/", "@") + ASSET_BUNDLE_SUFFIX; foreach (string file in files) { if (file.EndsWith(".meta")) { continue; } AssetImporter importer = AssetImporter.GetAtPath(file); if (importer != null) { string ext = System.IO.Path.GetExtension(file); string bundleName = dirBundleName; if (null != ext && (ext.Equals(".prefab") || ext.Equals(".unity"))) { // prefab单个文件打包 bundleName = file.Substring(BASE_PATH.Length); bundleName = bundleName.Replace("/", "@"); if (null != ext) { bundleName = bundleName.Replace(ext, ext + ASSET_BUNDLE_SUFFIX); } else { bundleName += ASSET_BUNDLE_SUFFIX; } } bundleName = bundleName.ToLower(); Debug.LogFormat("Set AssetName Succ, File:{0}, AssetName:{1}", file, bundleName); importer.assetBundleName = bundleName; EditorUtility.UnloadUnusedAssetsImmediate(); // 存储bundleInfo AssetBuildBundleInfo info = new AssetBuildBundleInfo(); info.assetName = file; info.fileName = file; info.bundleName = bundleName; /*if (null != ext) { * info.fileName = file.Substring (0, file.IndexOf (ext)); * }*/ fileMap.Add(file, info); List <AssetBuildBundleInfo> infoList = null; bundleMap.TryGetValue(info.bundleName, out infoList); if (null == infoList) { infoList = new List <AssetBuildBundleInfo> (); bundleMap.Add(info.bundleName, infoList); } infoList.Add(info); } else { Debug.LogFormat("Set AssetName Fail, File:{0}, Msg:Importer is null", file); } } Debug.Log("Set AssetBundleName End......"); }
public static void packAssetBundle() { DateTime time0 = DateTime.Now; // 清理输出目录 CreateOrClearOutPath(); // 清理之前设置过的bundleName ClearAssetBundleName(); // 设置bunderName bundleMap.Clear(); List <string> resList = new List <string>(); GetAllSubResDirs(RES_SRC_PATH, resList); foreach (string dir in resList) { setAssetBundleName(dir); } // 打包 BuildPipeline.BuildAssetBundles(RES_OUTPUT_PATH, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows); AssetDatabase.Refresh(); // 构建依赖关系 AssetBundle assetBundle = AssetBundle.LoadFromFile(CommonDefine.F_STREAMING_ASSETS_PATH + "StreamingAssets"); AssetBundleManifest mainfest = assetBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); string[] assetBundleNameList = mainfest.GetAllAssetBundles(); foreach (string bundle in assetBundleNameList) { string bundleName = bundle; string[] deps = mainfest.GetAllDependencies(bundleName); StringUtility.rightToLeft(ref bundleName); foreach (string dep in deps) { string depName = dep; StringUtility.rightToLeft(ref depName); if (bundleMap.ContainsKey(dep)) { List <AssetBuildBundleInfo> infoList = bundleMap[bundleName]; foreach (AssetBuildBundleInfo info in infoList) { info.AddDependence(depName); } } } } // 生成XML doc = new XMLDocument(); doc.startObject("files"); foreach (KeyValuePair <string, AssetBuildBundleInfo> pair in fileMap) { AssetBuildBundleInfo info = pair.Value; doc.startObject("file"); doc.createElement("bundleName", info.bundleName); doc.createElement("fileName", info.fileName); doc.createElement("assetName", info.assetName); if (info.dependencies != null) { doc.startObject("deps"); foreach (string dep in info.dependencies) { doc.createElement("dep", dep); } doc.endObject("deps"); } doc.endObject("file"); } doc.endObject("files"); FileStream fs = new FileStream(Path.Combine(RES_OUTPUT_PATH, "StreamingAssets.xml"), FileMode.Create); byte[] data = System.Text.Encoding.UTF8.GetBytes(doc.ToString()); fs.Write(data, 0, data.Length); fs.Flush(); fs.Close(); UnityUtility.messageBox("资源打包结束! 耗时 : " + (DateTime.Now - time0), false); }
protected static void setAssetBundleName(string fullPath) { string[] files = Directory.GetFiles(fullPath); if (files == null || files.Length == 0) { return; } string pathUnderResources = fullPath.Substring(RES_SRC_PATH.Length); int unpackCount = mUnPackFolder.Length; for (int i = 0; i < unpackCount; ++i) { // 如果该文件夹是不打包的文件夹,则直接返回 if (StringUtility.startWith(pathUnderResources, mUnPackFolder[i], false)) { return; } } Debug.Log("Set AssetBundleName Start......"); string dirBundleName = pathUnderResources.Replace("/", "@") + ASSET_BUNDLE_SUFFIX; foreach (string file in files) { // .asset文件和.meta不打包 if (file.EndsWith(".meta") || file.EndsWith(".asset")) { continue; } AssetImporter importer = AssetImporter.GetAtPath(file); if (importer != null) { string ext = Path.GetExtension(file); string bundleName = dirBundleName; // prefab和unity(但是一般情况下unity场景文件不打包)单个文件打包 if (ext != null && (ext.Equals(".prefab") || ext.Equals(".unity"))) { bundleName = file.Substring(RES_SRC_PATH.Length); bundleName = bundleName.Replace("/", "@"); if (null != ext) { bundleName = bundleName.Replace(ext, ASSET_BUNDLE_SUFFIX); } else { bundleName += ASSET_BUNDLE_SUFFIX; } } StringUtility.rightToLeft(ref bundleName); bundleName = bundleName.ToLower(); Debug.LogFormat("Set AssetName Succ, File:{0}, AssetName:{1}", file, bundleName); importer.assetBundleName = bundleName; EditorUtility.UnloadUnusedAssetsImmediate(); string fileName = file; fileName = fileName.ToLower(); StringUtility.rightToLeft(ref fileName); // 存储bundleInfo AssetBuildBundleInfo info = new AssetBuildBundleInfo(); info.assetName = fileName; info.bundleName = bundleName; if (ext != null) { int index = fileName.IndexOf(ext.ToLower()); info.fileName = fileName.Substring(0, index); } else { info.fileName = fileName; } fileMap.Add(fileName, info); List <AssetBuildBundleInfo> infoList = null; bundleMap.TryGetValue(info.bundleName, out infoList); if (null == infoList) { infoList = new List <AssetBuildBundleInfo>(); bundleMap.Add(info.bundleName, infoList); } infoList.Add(info); } else { Debug.LogFormat("Set AssetName Fail, File:{0}, Msg:Importer is null", file); } } Debug.Log("Set AssetBundleName End......"); }
public static void Pack() { // 清理输出目录 CreateOrClearOutPath(); // 清理之前设置过的bundleName ClearAssetBundleName(); // 设置bunderName bundleMap.Clear(); fileMap.Clear(); List <string> resList = GetAllResDirs(RES_SRC_PATH); foreach (string dir in resList) { setAssetBundleName(dir); } // 打包 BuildPipeline.BuildAssetBundles(RES_OUTPUT_PATH, BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows64); AssetDatabase.Refresh(); // 构建依赖关系 if (!System.IO.File.Exists(FileUtils.getPath("StreamingAssets"))) { return; } AssetBundle assetBundle = AssetBundle.LoadFromFile(FileUtils.getPath("StreamingAssets")); AssetBundleManifest mainfest = assetBundle.LoadAsset <AssetBundleManifest> ("AssetBundleManifest"); string[] bundleNames = mainfest.GetAllAssetBundles(); foreach (string bundleName in bundleNames) { string[] deps = mainfest.GetAllDependencies(bundleName); foreach (string dep in deps) { List <AssetBuildBundleInfo> infoList = null; bundleMap.TryGetValue(bundleName, out infoList); if (null != infoList) { foreach (AssetBuildBundleInfo info in infoList) { info.AddDependence(dep); } } } } // 生成XML doc = new XMLDocment(); doc.startObject("files"); foreach (KeyValuePair <string, AssetBuildBundleInfo> pair in fileMap) { AssetBuildBundleInfo info = pair.Value; doc.startObject("file"); doc.createElement("bundleName", info.bundleName); doc.createElement("fileName", info.fileName); doc.createElement("assetName", info.assetName); if (null != info.dependencies) { doc.startObject("deps"); foreach (string dep in info.dependencies) { doc.createElement("dep", dep); } doc.endObject("deps"); } doc.endObject("file"); } doc.endObject("files"); FileStream fs = new FileStream(Path.Combine(RES_OUTPUT_PATH, "StreamingAssets.xml"), FileMode.Create); byte[] data = System.Text.Encoding.UTF8.GetBytes(doc.ToString()); fs.Write(data, 0, data.Length); fs.Flush(); fs.Close(); if (ResourceManager.GetInstance().ManagerType == ResManagerType.ABB) { //创建压缩文件的MD5 CreateMD5File(".unity3d"); // 打包后的清理 ClearOutPath(".xxx"); } else if (ResourceManager.GetInstance().ManagerType == ResManagerType.ABB_UPDATE) { //压缩文件 CompressFile(); //创建压缩文件的MD5 CreateMD5File(".zip"); //复制到指定目录 CopyFileForUpdate(); // 打包后的清理 ClearOutPath(".unity3d"); } }
// subPath为以Asset开头的相对路径 public static void packAssetBundle(BuildTarget target, string subPath = "") { if (subPath == "") { subPath = AssetDatabase.GetAssetPath(Selection.activeObject); } string[] files = Directory.GetFiles(CommonDefine.P_RESOURCE_PATH); AssetBundleBuild[] buildList = null; if (subPath != "") { bool isPack = EditorUtility.DisplayDialog("打包", "确认打包" + subPath + "?", "确认", "取消"); if (!isPack) { return; } findAssetBundleBuild(subPath, ref buildList); } else { logInfo("打包全部AssetBundle", LOG_LEVEL.LL_FORCE); } DateTime time0 = DateTime.Now; if (buildList != null) { // 部分打包,仅重新生成资源包文件,清单文件也会一起生成,但是由于只是部分打包,所以依赖项可能没有打包,需要手动还原清单文件 int count = buildList.Length; for (int i = 0; i < count; ++i) { string bundleFileName = CommonDefine.P_STREAMING_ASSETS_PATH + buildList[i].assetBundleName; if (File.Exists(bundleFileName)) { File.Delete(bundleFileName); } } BuildPipeline.BuildAssetBundles(CommonDefine.P_STREAMING_ASSETS_PATH, buildList, BuildAssetBundleOptions.ChunkBasedCompression, target); AssetDatabase.Refresh(); } else { // 清理输出目录 createOrClearOutPath(); // 清理之前设置过的bundleName clearAssetBundleName(); // 设置bunderName mAssetBundleMap.Clear(); List <string> resList = new List <string>(); getAllSubResDirs(CommonDefine.P_RESOURCE_PATH, resList); foreach (string dir in resList) { setAssetBundleName(dir); } // 打包 BuildPipeline.BuildAssetBundles(CommonDefine.P_STREAMING_ASSETS_PATH, BuildAssetBundleOptions.ChunkBasedCompression, target); AssetDatabase.Refresh(); // 构建依赖关系 AssetBundle assetBundle = AssetBundle.LoadFromFile(CommonDefine.F_STREAMING_ASSETS_PATH + "StreamingAssets"); AssetBundleManifest manifest = assetBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); string[] assetBundleNameList = manifest.GetAllAssetBundles(); // 遍历所有AB foreach (string bundle in assetBundleNameList) { string bundleName = bundle; string[] deps = manifest.GetAllDependencies(bundleName); if (mAssetBundleMap.ContainsKey(bundleName)) { rightToLeft(ref bundleName); // 遍历当前AB的所有依赖项 foreach (string dep in deps) { string depName = dep; rightToLeft(ref depName); List <AssetBuildBundleInfo> infoList = mAssetBundleMap[bundleName]; foreach (AssetBuildBundleInfo info in infoList) { info.AddDependence(depName); } } } } // 生成XML XMLDocument doc = new XMLDocument(); doc.startObject("files", true); foreach (var item in mAssetBundleMap) { int count = item.Value.Count; for (int i = 0; i < count; ++i) { AssetBuildBundleInfo info = item.Value[i]; doc.startObject("file"); doc.createElement("bundleName", info.bundleName); doc.createElement("assetName", info.assetName); if (info.dependencies != null) { doc.startObject("deps"); foreach (string dep in info.dependencies) { doc.createElement("dep", dep); } doc.endObject("deps"); } doc.endObject("file", true); } } doc.endObject("files"); FileStream fs = new FileStream(CommonDefine.P_STREAMING_ASSETS_PATH + "StreamingAssets.xml", FileMode.Create); byte[] data = Encoding.UTF8.GetBytes(doc.ToString()); fs.Write(data, 0, data.Length); fs.Flush(); fs.Close(); } UnityUtility.messageBox("资源打包结束! 耗时 : " + (DateTime.Now - time0), false); }