// 刷新对应目录的Assetbundle名字 public static void RefreshAssetBundleNames(string[] files, string[] dirs) { string title = "设置所有文件的AB名"; EditorUtility.DisplayProgressBar(title, "搜索文件及文件夹", 0.0f); UDLib.Utility.CDebugOut.Log("开始 设置ab资源名字"); DateTime startTime = System.DateTime.Now; float total = 1.0f / (float)(files.Length + dirs.Length); int p = 0; #if REFRENCE_MODE CFindReferencesInProject.Clear(); #endif for (int i = 0, n = files.Length; i < n; ++i, ++p) { if ((i & 127) == 0) { EditorUtility.DisplayProgressBar(title, string.Format("文件处理进度 {0} / {1}", i, n), (float)p * total); } //如果是引用模式,则这里处理的是查找引用关系 SetAssetBundleName(files[i].Replace("\\", "/")); } for (int i = 0, n = dirs.Length; i < n; ++i, ++p) { if ((i & 63) == 0) { EditorUtility.DisplayProgressBar(title, string.Format("文件夹处理进度 {0} / {1}", i, n), (float)p * total); } //清除文件夹的ab名字 AssetImporter importer = AssetImporter.GetAtPath(dirs[i]); if (importer != null) { if (importer.assetBundleName.Length > 0) { importer.assetBundleName = string.Empty; } } } #if REFRENCE_MODE //生成引用关系asset CFindReferencesInProject.CreateReferenceAsset(); //查找根节点 List <string> roots = CFindReferencesInProject.FindRoots(); SetAssetBundleNamesReferenceMode(roots); #endif EditorUtility.ClearProgressBar(); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); DateTime endTime = System.DateTime.Now; UDLib.Utility.CDebugOut.Log("结束 设置ab资源名字"); UDLib.Utility.CDebugOut.Log("耗时:" + (endTime - startTime).TotalSeconds + "秒"); }
private static void SetAssetBundleNameReferenceMode(string path) { if (alreadyProcessedPath.Contains(path)) { return; } alreadyProcessedPath.Add(path); ReferenceInfo info = CFindReferencesInProject.getReferenceInfoByPath(path); AssetImporter importer = AssetImporter.GetAtPath(path); Assert.AreNotEqual(importer, null, "path not found:" + path); if (info.parents.Count == 0) { //没有被引用 setAssetBundleNameFilter(importer, info.path); } else if (info.parents.Count == 1) { //只被一个对象引用了 setAssetBundleNameFilter(importer, info.parents[0]); } else { //被多个对象引用了 bool allParentsSameAbName = true; string parentAbName = null; for (int i = 0; i < info.parents.Count; i++) { AssetImporter parentImporter = AssetImporter.GetAtPath(info.parents[i]); if (parentAbName == null) { parentAbName = parentImporter.assetBundleName; } else if (parentAbName != parentImporter.assetBundleName) { allParentsSameAbName = false; break; } } if (allParentsSameAbName) { //多个对象属于同一个ab,设置为父对象的ab名字 importer.assetBundleName = parentAbName; } else { //多对象属于不同的ab,单独打包 setAssetBundleNameFilter(importer, info.path); } } //处理引用的其他资源 SetAssetBundleNameReferenceModeDependencies(info.path); }
//设置单个文件的Ab名字 public static AssetImporter SetAssetBundleName(string path) { #if REFRENCE_MODE string origin_path = path; #endif path = path.ToLower(); AssetImporter importer = AssetImporter.GetAtPath(path); if (importer != null) { // 不要排除Art目录,把AB机制扩张到Asset全目录 if (path.Contains(".") == true && path.Contains("/streamingassets/") == false && path.Contains("/editor/") == false /*&& path.StartsWith("assets/app/art") == false */ && path.IndexOf("_asset_bundle_.txt") == -1) { string[][] root_paths = ResourceDefine.GetABRootPaths(); for (int i = 0, n = root_paths.Length; i < n; ++i) { for (int ii = 0, nn = root_paths[i].Length; ii < nn; ++ii) { string _root_paths = root_paths[i][ii]; if (path.StartsWith(_root_paths) == true)//检测资源路径是否在打包文件夹内 { string ext = Path.GetExtension(path); string[] exts = ResourceDefine.GetABExts()[i]; string suffix = ResourceDefine.GetABSuffix()[i]; for (int j = 0, m = exts.Length; j < m; ++j) { if (ext == exts[j]) //检测扩展名是否在打包列表内 { string name = Path.GetFileNameWithoutExtension(path); string folder = ResourceDefine.ResourceTypeToString(i); #if REFRENCE_MODE //设置引用关系 CFindReferencesInProject.Find(origin_path); #else string ab_name = null; switch (ResourceDefine.GetABBuildTypes()[i]) { case UDLib.Utility.EBUILD_TYPE.SINGLE: ab_name = folder + "/" + name; break; case UDLib.Utility.EBUILD_TYPE.HASH256: ab_name = folder + "/" + ResourceDefine.ResourceTypeToHash256String(i, name); break; case UDLib.Utility.EBUILD_TYPE.FOLDER: { bool isSet = false; string _path = path.ToLower(); if (_path.Contains("ui/module") && _path.Contains("config/")) { // 判断是ui/module下的config目录 ,向上一级取目录名来组成ab_name isSet = SetABNameWithParentFolder(folder, _path, "ui/module", ref ab_name); } else if (_path.Contains("art/character/hero/model")) { // 判断是art下的charater Texture ,向上一级取目录名来组成ab_name isSet = SetABNameWithParentFolder(folder, _path, "art/character/hero/model", ref ab_name); } if (!isSet) { ab_name = folder + "/" + Path.GetFileName(Path.GetDirectoryName(path)); } } break; case UDLib.Utility.EBUILD_TYPE.PACK: ab_name = folder; break; } ab_name = ab_name.ToLower() + suffix + ".dat"; ab_name = ab_name.Replace(" ", ""); // set file attribute ToolUtils.RemoveMetaReadOnly(path + ".meta"); if (importer.assetBundleName != ab_name || importer.assetBundleVariant.Length > 0) { importer.assetBundleName = ab_name; if (importer.assetBundleVariant.Length > 0) { importer.assetBundleVariant = string.Empty; } UDLib.Utility.CDebugOut.Log("SetAssetBundleName\t" + importer.assetBundleName + "\t" + ab_name + "\t" + path); } #endif return(importer); } } } } } } if (importer.assetBundleName.Length > 0) { // 该资源无需ab名字了 ToolUtils.RemoveMetaReadOnly(path + ".meta"); importer.assetBundleName = string.Empty; } } return(null); }