public static void AddCollector(string directory, string packRuleName, string filterRuleName, bool dontWriteAssetPath, string assetTags, bool saveFile = true) { // 末尾添加路径分隔符号 if (directory.EndsWith("/") == false) { directory = $"{directory}/"; } // 检测收集器路径冲突 if (CheckConflict(directory)) { return; } AssetBundleCollectorSetting.Collector element = new AssetBundleCollectorSetting.Collector(); element.CollectDirectory = directory; element.PackRuleName = packRuleName; element.FilterRuleName = filterRuleName; element.DontWriteAssetPath = dontWriteAssetPath; element.AssetTags = assetTags; Setting.Collectors.Add(element); if (saveFile) { SaveFile(); } }
/// <summary> /// 获取所有收集的资源 /// </summary> /// <returns>返回资源路径列表</returns> public static List <string> GetAllCollectAssets() { List <string> result = new List <string>(10000); for (int i = 0; i < Setting.Collectors.Count; i++) { AssetBundleCollectorSetting.Collector collector = Setting.Collectors[i]; string collectDirectory = collector.CollectDirectory.TrimEnd('/'); //注意:AssetDatabase不支持末尾带分隔符的文件夹路径 string[] guids = AssetDatabase.FindAssets(string.Empty, new string[] { collectDirectory }); foreach (string guid in guids) { string assetPath = AssetDatabase.GUIDToAssetPath(guid); if (ValidateAsset(assetPath) == false) { continue; } if (FilterAsset(assetPath, collector.FilterClassName) == false) { continue; } if (result.Contains(assetPath) == false) { result.Add(assetPath); } } } return(result); }
/// <summary> /// 获取所有收集的资源 /// </summary> public static List <AssetCollectInfo> GetAllCollectAssets() { Dictionary <string, AssetCollectInfo> result = new Dictionary <string, AssetCollectInfo>(10000); for (int i = 0; i < Setting.Collectors.Count; i++) { AssetBundleCollectorSetting.Collector collector = Setting.Collectors[i]; string[] findAssets = EditorTools.FindAssets(EAssetSearchType.All, collector.CollectDirectory); foreach (string assetPath in findAssets) { if (IsValidateAsset(assetPath) == false) { continue; } if (IsCollectAsset(assetPath, collector.FilterRuleName) == false) { continue; } if (result.ContainsKey(assetPath) == false) { var assetCollectInfo = new AssetCollectInfo(assetPath, collector.GetAssetTags(), collector.DontWriteAssetPath); result.Add(assetPath, assetCollectInfo); } } } return(result.Values.ToList()); }
// 收集器相关 public static void AddCollector(string directory) { if (IsContainsCollector(directory) == false) { AssetBundleCollectorSetting.Collector element = new AssetBundleCollectorSetting.Collector(); element.CollectDirectory = directory; Setting.Collectors.Add(element); SaveFile(); } }
/// <summary> /// 获取所有的打包路径 /// </summary> public static List <string> GetAllCollectDirectory() { List <string> result = new List <string>(); for (int i = 0; i < Setting.Collectors.Count; i++) { AssetBundleCollectorSetting.Collector wrapper = Setting.Collectors[i]; result.Add(wrapper.CollectDirectory); } return(result); }
/// <summary> /// 是否收集该资源 /// </summary> public static bool IsCollectAsset(string assetPath) { for (int i = 0; i < Setting.Collectors.Count; i++) { AssetBundleCollectorSetting.Collector wrapper = Setting.Collectors[i]; if (assetPath.StartsWith(wrapper.CollectDirectory)) { return(true); } } return(false); }
/// <summary> /// 获取资源的打包信息 /// </summary> public static BundleLabelAndVariant GetBundleLabelAndVariant(string assetPath, System.Type assetType) { // 如果收集全路径着色器 if (Setting.IsCollectAllShaders) { if (assetType == typeof(UnityEngine.Shader)) { return(new BundleLabelAndVariant(Setting.ShadersBundleName, PatchDefine.AssetBundleDefaultVariant)); } } // 获取收集器 AssetBundleCollectorSetting.Collector findCollector = null; for (int i = 0; i < Setting.Collectors.Count; i++) { AssetBundleCollectorSetting.Collector collector = Setting.Collectors[i]; if (assetPath.StartsWith(collector.CollectDirectory)) { findCollector = collector; break; } } string bundleLabel; // 如果没有找到收集器 if (findCollector == null) { IPackRule defaultInstance = new PackExplicit(); bundleLabel = defaultInstance.GetAssetBundleLabel(assetPath); } else { // 根据规则设置获取标签名称 IPackRule getInstance = GetPackRuleInstance(findCollector.PackRuleClassName); bundleLabel = getInstance.GetAssetBundleLabel(assetPath); } // 注意:如果资源所在文件夹的名称包含后缀符号,则为变体资源 string assetDirectory = Path.GetDirectoryName(assetPath); // "Assets/Texture.HD/background.jpg" --> "Assets/Texture.HD" if (Path.HasExtension(assetDirectory)) { string extension = Path.GetExtension(assetDirectory); bundleLabel = bundleLabel.Replace(extension, string.Empty); string bundleVariant = extension.RemoveFirstChar(); return(new BundleLabelAndVariant(bundleLabel, bundleVariant)); } else { return(new BundleLabelAndVariant(bundleLabel, PatchDefine.AssetBundleDefaultVariant)); } }
// 收集器相关 public static void AddCollector(string directory) { // 检测收集器路径冲突 if (CheckConflict(directory)) { return; } AssetBundleCollectorSetting.Collector element = new AssetBundleCollectorSetting.Collector(); element.CollectDirectory = directory; Setting.Collectors.Add(element); SaveFile(); }
public static BundleLabelAndVariant GetBundleLabelAndVariant(string assetPath) { string label; // 获取收集器 AssetBundleCollectorSetting.Collector findWrapper = null; for (int i = 0; i < Setting.Collectors.Count; i++) { AssetBundleCollectorSetting.Collector wrapper = Setting.Collectors[i]; if (assetPath.StartsWith(wrapper.CollectDirectory)) { findWrapper = wrapper; break; } } // 如果没有找到收集器 if (findWrapper == null) { IBundleLabel defaultLabel = new LabelByFilePath(); label = defaultLabel.GetAssetBundleLabel(assetPath); } else { // 根据规则设置获取标签名称 IBundleLabel bundleLabel = GetCollectorInstance(findWrapper.LabelClassName); label = bundleLabel.GetAssetBundleLabel(assetPath); } // 注意:如果资源所在文件夹的名称包含后缀符号,则为变体资源 string folderName = Path.GetDirectoryName(assetPath); // "Assets/Texture.HD/background.jpg" --> "Assets/Texture.HD" if (Path.HasExtension(folderName)) { string extension = Path.GetExtension(folderName); BundleLabelAndVariant result = new BundleLabelAndVariant(); result.BundleLabel = EditorTools.GetRegularPath(label.Replace(extension, string.Empty)); result.BundleVariant = extension.RemoveFirstChar(); return(result); } else { BundleLabelAndVariant result = new BundleLabelAndVariant(); result.BundleLabel = EditorTools.GetRegularPath(label); result.BundleVariant = PatchDefine.AssetBundleDefaultVariant; return(result); } }
/// <summary> /// 获取资源的打包标签 /// </summary> public static string GetAssetBundleLabel(string assetPath) { // 注意:一个资源有可能被多个收集器覆盖 List <AssetBundleCollectorSetting.Collector> filterWrappers = new List <AssetBundleCollectorSetting.Collector>(); for (int i = 0; i < Setting.Collectors.Count; i++) { AssetBundleCollectorSetting.Collector wrapper = Setting.Collectors[i]; if (assetPath.StartsWith(wrapper.CollectDirectory)) { filterWrappers.Add(wrapper); } } // 我们使用路径最深层的收集器 AssetBundleCollectorSetting.Collector findWrapper = null; for (int i = 0; i < filterWrappers.Count; i++) { AssetBundleCollectorSetting.Collector wrapper = filterWrappers[i]; if (findWrapper == null) { findWrapper = wrapper; continue; } if (wrapper.CollectDirectory.Length > findWrapper.CollectDirectory.Length) { findWrapper = wrapper; } } // 如果没有找到收集器 if (findWrapper == null) { IAssetCollector defaultCollector = new LabelByFilePath(); return(defaultCollector.GetAssetBundleLabel(assetPath)); } // 根据规则设置获取标签名称 IAssetCollector collector = GetCollectorInstance(findWrapper.CollectorName); return(collector.GetAssetBundleLabel(assetPath)); }
/// <summary> /// 获取资源的打包信息 /// </summary> public static BundleLabelAndVariant GetBundleLabelAndVariant(string assetPath) { // 如果收集全路径着色器 if (Setting.IsCollectAllShaders) { System.Type assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath); if (assetType == typeof(UnityEngine.Shader)) { return(new BundleLabelAndVariant(Setting.ShadersBundleName, PatchDefine.AssetBundleDefaultVariant)); } } // 获取收集器 AssetBundleCollectorSetting.Collector findCollector = null; for (int i = 0; i < Setting.Collectors.Count; i++) { AssetBundleCollectorSetting.Collector collector = Setting.Collectors[i]; if (assetPath.StartsWith(collector.CollectDirectory)) { findCollector = collector; break; } } // 如果没有找到收集器 string bundleLabel; if (findCollector == null) { IPackRule defaultInstance = new PackExplicit(); bundleLabel = defaultInstance.GetAssetBundleLabel(assetPath); } else { // 根据规则设置获取标签名称 IPackRule getInstance = GetPackRuleInstance(findCollector.PackRuleName); bundleLabel = getInstance.GetAssetBundleLabel(assetPath); } return(new BundleLabelAndVariant(bundleLabel, PatchDefine.AssetBundleDefaultVariant)); }
// 收集器相关 public static void AddCollector(string directory) { // 末尾添加路径分隔符号 if (directory.EndsWith("/") == false) { directory = $"{directory}/"; } // 检测收集器路径冲突 if (CheckConflict(directory)) { return; } AssetBundleCollectorSetting.Collector element = new AssetBundleCollectorSetting.Collector(); element.CollectDirectory = directory; element.LabelClassName = nameof(LabelByFilePath); element.FilterClassName = nameof(SearchAll); Setting.Collectors.Add(element); SaveFile(); }
/// <summary> /// 创建Readme文件到输出目录 /// </summary> private void CreateReadmeFile(AssetBundleBuilder.BuildParametersContext buildParameters, AssetBundleBuilder.BuildOptionsContext buildOptions, AssetBundleManifest unityManifest) { string[] allAssetBundles = unityManifest.GetAllAssetBundles(); // 删除旧文件 string filePath = $"{buildParameters.OutputDirectory}/{PatchDefine.ReadmeFileName}"; if (File.Exists(filePath)) { File.Delete(filePath); } BuildLogger.Log($"创建说明文件:{filePath}"); StringBuilder content = new StringBuilder(); AppendData(content, $"构建平台:{buildParameters.BuildTarget}"); AppendData(content, $"构建版本:{buildParameters.BuildVersion}"); AppendData(content, $"构建时间:{DateTime.Now}"); AppendData(content, ""); AppendData(content, $"--着色器--"); AppendData(content, $"IsCollectAllShaders:{AssetBundleCollectorSettingData.Setting.IsCollectAllShaders}"); AppendData(content, $"ShadersBundleName:{AssetBundleCollectorSettingData.Setting.ShadersBundleName}"); AppendData(content, ""); AppendData(content, $"--配置信息--"); for (int i = 0; i < AssetBundleCollectorSettingData.Setting.Collectors.Count; i++) { AssetBundleCollectorSetting.Collector wrapper = AssetBundleCollectorSettingData.Setting.Collectors[i]; AppendData(content, $"Directory : {wrapper.CollectDirectory} | {wrapper.PackRuleClassName} | {wrapper.FilterRuleClassName}"); } AppendData(content, ""); AppendData(content, $"--构建参数--"); AppendData(content, $"CompressOption:{buildOptions.CompressOption}"); AppendData(content, $"IsForceRebuild:{buildOptions.IsForceRebuild}"); AppendData(content, $"IsDisableWriteTypeTree:{buildOptions.IsDisableWriteTypeTree}"); AppendData(content, $"IsIgnoreTypeTreeChanges:{buildOptions.IsIgnoreTypeTreeChanges}"); AppendData(content, ""); AppendData(content, $"--构建清单--"); for (int i = 0; i < allAssetBundles.Length; i++) { AppendData(content, allAssetBundles[i]); } PatchManifest patchFile = AssetBundleBuilder.LoadPatchManifestFile(buildParameters); { AppendData(content, ""); AppendData(content, $"--更新清单--"); foreach (var patchBundle in patchFile.BundleList) { if (patchBundle.Version == buildParameters.BuildVersion) { AppendData(content, patchBundle.BundleName); } } AppendData(content, ""); AppendData(content, $"--变体列表--"); foreach (var variant in patchFile.VariantList) { AppendData(content, variant.ToString()); } } // 创建新文件 File.WriteAllText(filePath, content.ToString(), Encoding.UTF8); }
/// <summary> /// 3. 创建Readme文件到输出目录 /// </summary> private void CreateReadmeFile(AssetBundleManifest unityManifest) { string[] allAssetBundles = unityManifest.GetAllAssetBundles(); // 删除旧文件 string filePath = $"{OutputDirectory}/{PatchDefine.ReadmeFileName}"; if (File.Exists(filePath)) { File.Delete(filePath); } Log($"创建说明文件:{filePath}"); StringBuilder content = new StringBuilder(); AppendData(content, $"构建平台:{BuildTarget}"); AppendData(content, $"构建版本:{BuildVersion}"); AppendData(content, $"构建时间:{DateTime.Now}"); AppendData(content, ""); AppendData(content, $"--配置信息--"); for (int i = 0; i < AssetBundleCollectorSettingData.Setting.Collectors.Count; i++) { AssetBundleCollectorSetting.Collector wrapper = AssetBundleCollectorSettingData.Setting.Collectors[i]; AppendData(content, $"Directory : {wrapper.CollectDirectory} || CollectRule : {wrapper.CollectRule} || CollectorName : {wrapper.CollectorName}"); } AppendData(content, ""); AppendData(content, $"--构建参数--"); AppendData(content, $"CompressOption:{CompressOption}"); AppendData(content, $"ForceRebuild:{IsForceRebuild}"); AppendData(content, $"DisableWriteTypeTree:{IsDisableWriteTypeTree}"); AppendData(content, $"IgnoreTypeTreeChanges:{IsIgnoreTypeTreeChanges}"); AppendData(content, ""); AppendData(content, $"--构建清单--"); for (int i = 0; i < allAssetBundles.Length; i++) { AppendData(content, allAssetBundles[i]); } PatchManifest patchFile = LoadPatchManifestFile(); { AppendData(content, ""); AppendData(content, $"--更新清单--"); foreach (var element in patchFile.ElementList) { if (element.Version == BuildVersion) { AppendData(content, element.BundleName); } } AppendData(content, ""); AppendData(content, $"--变体列表--"); foreach (var variant in patchFile.VariantList) { AppendData(content, variant.ToString()); } } // 创建新文件 File.WriteAllText(filePath, content.ToString(), Encoding.UTF8); }