From b443a1c3080118a9ce7ba7fcd366ea5f76daf64c Mon Sep 17 00:00:00 2001 From: hevinci Date: Sat, 30 Apr 2022 19:10:52 +0800 Subject: [PATCH] Optimize packaging bundle core logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化了打包的核心逻辑,对依赖资源进行自动划分。 --- .../AssetBundleBuilder/BuildAssetInfo.cs | 139 ++++++++++++------ .../AssetBundleBuilder/BuildBundleInfo.cs | 2 +- .../AssetBundleBuilder/BuildMapContext.cs | 8 +- .../AssetBundleBuilder/BuildMapHelper.cs | 76 ++++------ .../BuildSystem/BuildRunner.cs | 2 +- .../BuildTasks/TaskCreatePatchManifest.cs | 15 +- .../AssetBundleCollector.cs | 112 +++++++------- .../AssetBundleGrouper/AssetBundleGrouper.cs | 24 ++- .../AssetBundleGrouperConfig.cs | 23 ++- ...cs => AssetBundleGrouperRuntimeSupport.cs} | 47 +----- ... AssetBundleGrouperRuntimeSupport.cs.meta} | 0 .../AssetBundleGrouperSetting.cs | 26 ++-- .../AssetBundleGrouperWindow.cs | 57 +++---- .../AssetBundleGrouper/CollectAssetInfo.cs | 16 +- .../AssetBundleGrouper/ECollectorType.cs | 23 +++ .../AssetBundleGrouper/ECollectorType.cs.meta | 11 ++ .../LocationServicesHelper.cs | 5 +- 17 files changed, 308 insertions(+), 278 deletions(-) rename Assets/YooAsset/Editor/AssetBundleGrouper/{AssetBundleGrouperHelper.cs => AssetBundleGrouperRuntimeSupport.cs} (67%) rename Assets/YooAsset/Editor/AssetBundleGrouper/{AssetBundleGrouperHelper.cs.meta => AssetBundleGrouperRuntimeSupport.cs.meta} (100%) create mode 100644 Assets/YooAsset/Editor/AssetBundleGrouper/ECollectorType.cs create mode 100644 Assets/YooAsset/Editor/AssetBundleGrouper/ECollectorType.cs.meta diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildAssetInfo.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildAssetInfo.cs index 3e1f53e..cc891c3 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildAssetInfo.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildAssetInfo.cs @@ -1,15 +1,20 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Linq; namespace YooAsset.Editor { public class BuildAssetInfo { + private string _mainBundleName; + private string _shareBundleName; + private readonly HashSet _dependBundleNames = new HashSet(); + /// - /// 资源包名称 + /// 收集器类型 /// - public string BundleName { private set; get; } + public ECollectorType CollectorType { private set; get; } /// /// 可寻址地址 @@ -26,26 +31,11 @@ namespace YooAsset.Editor /// public bool IsRawAsset { private set; get; } - /// - /// 不写入资源列表 - /// - public bool NotWriteToAssetList { private set; get; } - - /// - /// 是否为主动收集资源 - /// - public bool IsCollectAsset { private set; get; } - /// /// 是否为着色器资源 /// public bool IsShaderAsset { private set; get; } - /// - /// 被依赖次数 - /// - public int DependCount = 0; - /// /// 资源分类标签列表 /// @@ -58,13 +48,13 @@ namespace YooAsset.Editor public List AllDependAssetInfos { private set; get; } - public BuildAssetInfo(string address, string assetPath, bool isRawAsset, bool notWriteToAssetList) + public BuildAssetInfo(ECollectorType collectorType, string mainBundleName, string address, string assetPath, bool isRawAsset) { + _mainBundleName = mainBundleName; + CollectorType = collectorType; Address = address; AssetPath = assetPath; IsRawAsset = isRawAsset; - NotWriteToAssetList = notWriteToAssetList; - IsCollectAsset = true; System.Type assetType = UnityEditor.AssetDatabase.GetMainAssetTypeAtPath(assetPath); if (assetType == typeof(UnityEngine.Shader)) @@ -72,12 +62,12 @@ namespace YooAsset.Editor else IsShaderAsset = false; } - public BuildAssetInfo(string assetPath) + public BuildAssetInfo(ECollectorType collectorType, string assetPath) { + CollectorType = collectorType; + Address = string.Empty; AssetPath = assetPath; IsRawAsset = false; - NotWriteToAssetList = true; - IsCollectAsset = false; System.Type assetType = UnityEditor.AssetDatabase.GetMainAssetTypeAtPath(assetPath); if (assetType == typeof(UnityEngine.Shader)) @@ -86,6 +76,7 @@ namespace YooAsset.Editor IsShaderAsset = false; } + /// /// 设置所有依赖的资源 /// @@ -97,17 +88,6 @@ namespace YooAsset.Editor AllDependAssetInfos = dependAssetInfos; } - /// - /// 设置资源包名称 - /// - public void SetBundleName(string bundleName) - { - if (string.IsNullOrEmpty(BundleName) == false) - throw new System.Exception("Should never get here !"); - - BundleName = bundleName; - } - /// /// 添加资源分类标签 /// @@ -115,30 +95,91 @@ namespace YooAsset.Editor { foreach (var tag in tags) { - AddAssetTag(tag); + if (AssetTags.Contains(tag) == false) + { + AssetTags.Add(tag); + } } } /// - /// 添加资源分类标签 + /// 资源包名是否存在 /// - public void AddAssetTag(string tag) + public bool HasBundleName() { - if (AssetTags.Contains(tag) == false) - { - AssetTags.Add(tag); - } - } - - /// - /// 资源包名称是否有效 - /// - public bool BundleNameIsValid() - { - if (string.IsNullOrEmpty(BundleName)) + string bundleName = GetBundleName(); + if (string.IsNullOrEmpty(bundleName)) return false; else return true; } + + /// + /// 获取资源包名称 + /// + public string GetBundleName() + { + if (CollectorType == ECollectorType.None) + return _shareBundleName; + else + return _mainBundleName; + } + + /// + /// 设置依赖资源包名称 + /// + public void AddDependBundleName(string bundleName) + { + if (string.IsNullOrEmpty(bundleName)) + throw new Exception("Should never get here !"); + + if (_dependBundleNames.Contains(bundleName) == false) + _dependBundleNames.Add(bundleName); + } + + /// + /// 计算主资源或共享资源的完整包名 + /// + public void CalculateFullBundleName() + { + if (CollectorType == ECollectorType.None) + { + if (IsRawAsset) + throw new Exception("Should never get here !"); + + if (AssetBundleGrouperSettingData.Setting.AutoCollectShaders) + { + if (IsShaderAsset) + { + string shareBundleName = $"{AssetBundleGrouperSettingData.Setting.ShadersBundleName}.{YooAssetSettingsData.Setting.AssetBundleFileVariant}"; + _shareBundleName = EditorTools.GetRegularPath(shareBundleName).ToLower(); + return; + } + } + + if (_dependBundleNames.Count > 1) + { + var bundleNameList = _dependBundleNames.ToList(); + bundleNameList.Sort(); + string combineName = string.Join("|", bundleNameList); + var combineNameHash = HashUtility.StringSHA1(combineName); + var shareBundleName = $"share_{combineNameHash}.{YooAssetSettingsData.Setting.AssetBundleFileVariant}"; + _shareBundleName = EditorTools.GetRegularPath(shareBundleName).ToLower(); + } + } + else + { + if (IsRawAsset) + { + string mainBundleName = $"{_mainBundleName}.{YooAssetSettingsData.Setting.RawFileVariant}"; + _mainBundleName = EditorTools.GetRegularPath(mainBundleName).ToLower(); + } + else + { + string mainBundleName = $"{_mainBundleName}.{YooAssetSettingsData.Setting.AssetBundleFileVariant}"; + _mainBundleName = EditorTools.GetRegularPath(mainBundleName).ToLower(); ; + } + } + } } } \ No newline at end of file diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildBundleInfo.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildBundleInfo.cs index e43df92..901f189 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildBundleInfo.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildBundleInfo.cs @@ -105,7 +105,7 @@ namespace YooAsset.Editor /// public BuildAssetInfo[] GetAllPatchAssetInfos() { - return BuildinAssets.Where(t => t.IsCollectAsset && t.NotWriteToAssetList == false).ToArray(); + return BuildinAssets.Where(t => t.CollectorType == ECollectorType.MainCollector).ToArray(); } /// diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildMapContext.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildMapContext.cs index ea1363f..44e4995 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildMapContext.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildMapContext.cs @@ -25,13 +25,17 @@ namespace YooAsset.Editor /// public void PackAsset(BuildAssetInfo assetInfo) { - if (TryGetBundleInfo(assetInfo.BundleName, out BuildBundleInfo bundleInfo)) + string bundleName = assetInfo.GetBundleName(); + if (string.IsNullOrEmpty(bundleName)) + throw new Exception("Should never get here !"); + + if (TryGetBundleInfo(bundleName, out BuildBundleInfo bundleInfo)) { bundleInfo.PackAsset(assetInfo); } else { - BuildBundleInfo newBundleInfo = new BuildBundleInfo(assetInfo.BundleName); + BuildBundleInfo newBundleInfo = new BuildBundleInfo(bundleName); newBundleInfo.PackAsset(assetInfo); BundleInfos.Add(newBundleInfo); } diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildMapHelper.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildMapHelper.cs index aba40d6..a16b4be 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildMapHelper.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildMapHelper.cs @@ -13,21 +13,20 @@ namespace YooAsset.Editor public static BuildMapContext SetupBuildMap() { BuildMapContext context = new BuildMapContext(); - Dictionary buildAssetDic = new Dictionary(); + Dictionary buildAssetDic = new Dictionary(1000); - // 0. 检测配置合法性 + // 1. 检测配置合法性 AssetBundleGrouperSettingData.Setting.CheckConfigError(); - // 1. 获取主动收集的资源 - List collectAssetInfos = AssetBundleGrouperSettingData.Setting.GetAllCollectAssets(); + // 2. 获取所有主动收集的资源 + List allCollectAssets = AssetBundleGrouperSettingData.Setting.GetAllCollectAssets(); - // 2. 录入主动收集的资源 - foreach (var collectAssetInfo in collectAssetInfos) + // 3. 录入主动收集的资源 + foreach (var collectAssetInfo in allCollectAssets) { if (buildAssetDic.ContainsKey(collectAssetInfo.AssetPath) == false) { - var buildAssetInfo = new BuildAssetInfo(collectAssetInfo.Address, collectAssetInfo.AssetPath, collectAssetInfo.IsRawAsset, collectAssetInfo.NotWriteToAssetList); - buildAssetInfo.SetBundleName(collectAssetInfo.BundleName); + var buildAssetInfo = new BuildAssetInfo(collectAssetInfo.CollectorType, collectAssetInfo.BundleName, collectAssetInfo.Address, collectAssetInfo.AssetPath, collectAssetInfo.IsRawAsset); buildAssetInfo.AddAssetTags(collectAssetInfo.AssetTags); buildAssetDic.Add(collectAssetInfo.AssetPath, buildAssetInfo); } @@ -37,28 +36,29 @@ namespace YooAsset.Editor } } - // 3. 录入并分析依赖资源 - foreach (var collectAssetInfo in collectAssetInfos) + // 4. 录入相关依赖的资源 + foreach (var collectAssetInfo in allCollectAssets) { foreach (var dependAssetPath in collectAssetInfo.DependAssets) { if (buildAssetDic.ContainsKey(dependAssetPath)) { - buildAssetDic[dependAssetPath].DependCount++; buildAssetDic[dependAssetPath].AddAssetTags(collectAssetInfo.AssetTags); + buildAssetDic[dependAssetPath].AddDependBundleName(collectAssetInfo.BundleName); } else { - var buildAssetInfo = new BuildAssetInfo(dependAssetPath); + var buildAssetInfo = new BuildAssetInfo(ECollectorType.None, dependAssetPath); buildAssetInfo.AddAssetTags(collectAssetInfo.AssetTags); + buildAssetInfo.AddDependBundleName(collectAssetInfo.BundleName); buildAssetDic.Add(dependAssetPath, buildAssetInfo); } } } context.AssetFileCount = buildAssetDic.Count; - // 4. 设置主动收集资源的依赖列表 - foreach (var collectAssetInfo in collectAssetInfos) + // 5. 填充主动收集资源的依赖列表 + foreach (var collectAssetInfo in allCollectAssets) { var dependAssetInfos = new List(collectAssetInfo.DependAssets.Count); foreach (var dependAssetPath in collectAssetInfo.DependAssets) @@ -71,21 +71,18 @@ namespace YooAsset.Editor buildAssetDic[collectAssetInfo.AssetPath].SetAllDependAssetInfos(dependAssetInfos); } - // 5. 移除零依赖的资源 + // 6. 计算完整的资源包名 + foreach (KeyValuePair pair in buildAssetDic) + { + pair.Value.CalculateFullBundleName(); + } + + // 7. 移除未参与构建的资源 List removeList = new List(); foreach (KeyValuePair pair in buildAssetDic) { var buildAssetInfo = pair.Value; - if (buildAssetInfo.IsCollectAsset) - continue; - - if (AssetBundleGrouperSettingData.Setting.AutoCollectShaders) - { - if (buildAssetInfo.IsShaderAsset) - continue; - } - - if (buildAssetInfo.DependCount == 0) + if (buildAssetInfo.HasBundleName() == false) removeList.Add(buildAssetInfo); } foreach (var removeValue in removeList) @@ -93,32 +90,11 @@ namespace YooAsset.Editor buildAssetDic.Remove(removeValue.AssetPath); } - // 6. 设置未命名的资源包 - IPackRule defaultPackRule = new PackDirectory(); - foreach (KeyValuePair pair in buildAssetDic) - { - var buildAssetInfo = pair.Value; - if (buildAssetInfo.BundleNameIsValid() == false) - { - string shaderBundleName = AssetBundleGrouperHelper.CollectShaderBundleName(buildAssetInfo.AssetPath); - if (string.IsNullOrEmpty(shaderBundleName) == false) - { - buildAssetInfo.SetBundleName(shaderBundleName); - } - else - { - string bundleName = defaultPackRule.GetBundleName(new PackRuleData(buildAssetInfo.AssetPath)); - bundleName = AssetBundleGrouperHelper.CorrectBundleName(bundleName, false); - buildAssetInfo.SetBundleName(bundleName); - } - } - } - - // 7. 构建资源包 - var allBuildAssets = buildAssetDic.Values.ToList(); - if (allBuildAssets.Count == 0) + // 8. 构建资源包 + var allBuildinAssets = buildAssetDic.Values.ToList(); + if (allBuildinAssets.Count == 0) throw new Exception("构建的资源列表不能为空"); - foreach (var assetInfo in allBuildAssets) + foreach (var assetInfo in allBuildinAssets) { context.PackAsset(assetInfo); } diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildSystem/BuildRunner.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildSystem/BuildRunner.cs index 9c2ef75..1f129f9 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildSystem/BuildRunner.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildSystem/BuildRunner.cs @@ -29,7 +29,7 @@ namespace YooAsset.Editor catch (Exception e) { Debug.LogError($"Build task {task.GetType().Name} failed !"); - Debug.LogError($"Detail error : {e}"); + Debug.LogError($"Build error : {e}"); succeed = false; break; } diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreatePatchManifest.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreatePatchManifest.cs index 7d4d9bd..188fc69 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreatePatchManifest.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreatePatchManifest.cs @@ -142,7 +142,7 @@ namespace YooAsset.Editor else patchAsset.Address = string.Empty; patchAsset.AssetPath = assetInfo.AssetPath; - patchAsset.BundleID = GetAssetBundleID(assetInfo.BundleName, patchManifest); + patchAsset.BundleID = GetAssetBundleID(assetInfo.GetBundleName(), patchManifest); patchAsset.DependIDs = GetAssetBundleDependIDs(patchAsset.BundleID, assetInfo, patchManifest); result.Add(patchAsset); } @@ -154,13 +154,14 @@ namespace YooAsset.Editor List result = new List(); foreach (var dependAssetInfo in assetInfo.AllDependAssetInfos) { - if (dependAssetInfo.BundleNameIsValid() == false) - continue; - int bundleID = GetAssetBundleID(dependAssetInfo.BundleName, patchManifest); - if (mainBundleID != bundleID) + if (dependAssetInfo.HasBundleName()) { - if (result.Contains(bundleID) == false) - result.Add(bundleID); + int bundleID = GetAssetBundleID(dependAssetInfo.GetBundleName(), patchManifest); + if (mainBundleID != bundleID) + { + if (result.Contains(bundleID) == false) + result.Add(bundleID); + } } } return result.ToArray(); diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleCollector.cs b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleCollector.cs index 3afda69..72f1b09 100644 --- a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleCollector.cs +++ b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleCollector.cs @@ -14,6 +14,11 @@ namespace YooAsset.Editor /// public string CollectPath = string.Empty; + /// + /// 收集器类型 + /// + public ECollectorType CollectorType = ECollectorType.MainCollector; + /// /// 寻址规则类名 /// @@ -29,12 +34,6 @@ namespace YooAsset.Editor /// public string FilterRuleName = nameof(CollectAll); - - /// - /// 不写入资源列表 - /// - public bool NotWriteToAssetList = false; - /// /// 资源分类标签 /// @@ -65,14 +64,17 @@ namespace YooAsset.Editor if (AssetDatabase.LoadAssetAtPath(CollectPath) == null) throw new Exception($"Invalid collect path : {CollectPath}"); + if (CollectorType == ECollectorType.None) + throw new Exception($"{nameof(ECollectorType)}.{ECollectorType.None} is invalid in collector : {CollectPath}"); + if (AssetBundleGrouperSettingData.HasPackRuleName(PackRuleName) == false) - throw new Exception($"Invalid {nameof(IPackRule)} class type : {PackRuleName}"); + throw new Exception($"Invalid {nameof(IPackRule)} class type : {PackRuleName} in collector : {CollectPath}"); if (AssetBundleGrouperSettingData.HasFilterRuleName(FilterRuleName) == false) - throw new Exception($"Invalid {nameof(IFilterRule)} class type : {FilterRuleName}"); + throw new Exception($"Invalid {nameof(IFilterRule)} class type : {FilterRuleName} in collector : {CollectPath}"); if (AssetBundleGrouperSettingData.HasAddressRuleName(AddressRuleName) == false) - throw new Exception($"Invalid {nameof(IAddressRule)} class type : {AddressRuleName}"); + throw new Exception($"Invalid {nameof(IAddressRule)} class type : {AddressRuleName} in collector : {CollectPath}"); } /// @@ -80,70 +82,61 @@ namespace YooAsset.Editor /// public List GetAllCollectAssets(AssetBundleGrouper grouper) { - Dictionary adressTemper = new Dictionary(1000); Dictionary result = new Dictionary(1000); bool isRawAsset = PackRuleName == nameof(PackRawFile); - // 如果是文件夹 + // 检测原生资源包的收集器类型 + if (isRawAsset && CollectorType != ECollectorType.MainCollector) + throw new Exception($"The raw file must be set to {nameof(ECollectorType)}.{ECollectorType.MainCollector} : {CollectPath}"); + + // 收集打包资源 if (AssetDatabase.IsValidFolder(CollectPath)) { string collectDirectory = CollectPath; string[] findAssets = EditorTools.FindAssets(EAssetSearchType.All, collectDirectory); foreach (string assetPath in findAssets) { - if (IsValidateAsset(assetPath) == false) - continue; - if (IsCollectAsset(assetPath) == false) - continue; - if (result.ContainsKey(assetPath) == false) + if (IsValidateAsset(assetPath) && IsCollectAsset(assetPath)) { - string address = GetAddress(grouper, assetPath); - string bundleName = GetBundleName(grouper, assetPath, isRawAsset); - List assetTags = GetAssetTags(grouper); - var collectAssetInfo = new CollectAssetInfo(bundleName, address, assetPath, assetTags, isRawAsset, NotWriteToAssetList); - collectAssetInfo.DependAssets = GetAllDependencies(assetPath); - result.Add(assetPath, collectAssetInfo); - } - else - { - throw new Exception($"The collecting asset file is existed : {assetPath} in collector : {CollectPath}"); + if (result.ContainsKey(assetPath) == false) + { + var collectAssetInfo = CreateCollectAssetInfo(grouper, assetPath, isRawAsset); + result.Add(assetPath, collectAssetInfo); + } + else + { + throw new Exception($"The collecting asset file is existed : {assetPath} in collector : {CollectPath}"); + } } } } else { string assetPath = CollectPath; - if (result.ContainsKey(assetPath) == false) + if (IsValidateAsset(assetPath) && IsCollectAsset(assetPath)) { - if (isRawAsset && NotWriteToAssetList) - UnityEngine.Debug.LogWarning($"Are you sure raw file are not write to asset list : {assetPath}"); - - string address = GetAddress(grouper, assetPath); - string bundleName = GetBundleName(grouper, assetPath, isRawAsset); - List assetTags = GetAssetTags(grouper); - var collectAssetInfo = new CollectAssetInfo(bundleName, address, assetPath, assetTags, isRawAsset, NotWriteToAssetList); - collectAssetInfo.DependAssets = GetAllDependencies(assetPath); + var collectAssetInfo = CreateCollectAssetInfo(grouper, assetPath, isRawAsset); result.Add(assetPath, collectAssetInfo); } else { - throw new Exception($"The collecting asset file is existed : {assetPath} in collector : {CollectPath}"); + throw new Exception($"The collecting single asset file is invalid : {assetPath} in collector : {CollectPath}"); } } // 检测可寻址地址是否重复 if (AssetBundleGrouperSettingData.Setting.EnableAddressable) { - foreach (var collectInfo in result) + HashSet adressTemper = new HashSet(); + foreach (var collectInfoPair in result) { - string address = collectInfo.Value.Address; - if (adressTemper.ContainsKey(address) == false) + if (collectInfoPair.Value.CollectorType == ECollectorType.MainCollector) { - adressTemper.Add(address, address); - } - else - { - throw new Exception($"The address is existed : {address} in collector : {CollectPath}"); + string address = collectInfoPair.Value.Address; + if (adressTemper.Contains(address) == false) + adressTemper.Add(address); + else + throw new Exception($"The address is existed : {address} in collector : {CollectPath}"); } } } @@ -152,7 +145,15 @@ namespace YooAsset.Editor return result.Values.ToList(); } - + private CollectAssetInfo CreateCollectAssetInfo(AssetBundleGrouper grouper, string assetPath, bool isRawAsset) + { + string address = GetAddress(grouper, assetPath); + string bundleName = GetBundleName(grouper, assetPath); + List assetTags = GetAssetTags(grouper); + CollectAssetInfo collectAssetInfo = new CollectAssetInfo(CollectorType, bundleName, address, assetPath, assetTags, isRawAsset); + collectAssetInfo.DependAssets = GetAllDependencies(assetPath); + return collectAssetInfo; + } private bool IsValidateAsset(string assetPath) { if (assetPath.StartsWith("Assets/") == false && assetPath.StartsWith("Packages/") == false) @@ -188,24 +189,31 @@ namespace YooAsset.Editor } private string GetAddress(AssetBundleGrouper grouper, string assetPath) { - if (NotWriteToAssetList) - return assetPath; + if (CollectorType != ECollectorType.MainCollector) + return string.Empty; IAddressRule addressRuleInstance = AssetBundleGrouperSettingData.GetAddressRuleInstance(AddressRuleName); string adressValue = addressRuleInstance.GetAssetAddress(new AddressRuleData(assetPath, CollectPath, grouper.GrouperName)); return adressValue; } - private string GetBundleName(AssetBundleGrouper grouper, string assetPath, bool isRawAsset) + private string GetBundleName(AssetBundleGrouper grouper, string assetPath) { - string shaderBundleName = AssetBundleGrouperHelper.CollectShaderBundleName(assetPath); - if (string.IsNullOrEmpty(shaderBundleName) == false) - return shaderBundleName; + // 如果自动收集所有的着色器 + if (AssetBundleGrouperSettingData.Setting.AutoCollectShaders) + { + System.Type assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath); + if (assetType == typeof(UnityEngine.Shader)) + { + string bundleName = AssetBundleGrouperSettingData.Setting.ShadersBundleName; + return EditorTools.GetRegularPath(bundleName).ToLower(); + } + } // 根据规则设置获取资源包名称 { IPackRule packRuleInstance = AssetBundleGrouperSettingData.GetPackRuleInstance(PackRuleName); string bundleName = packRuleInstance.GetBundleName(new PackRuleData(assetPath, CollectPath, grouper.GrouperName)); - return AssetBundleGrouperHelper.CorrectBundleName(bundleName, isRawAsset); + return EditorTools.GetRegularPath(bundleName).ToLower(); } } private List GetAssetTags(AssetBundleGrouper grouper) diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouper.cs b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouper.cs index 7d9e3fd..18f16e7 100644 --- a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouper.cs +++ b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouper.cs @@ -47,41 +47,39 @@ namespace YooAsset.Editor /// public List GetAllCollectAssets() { - Dictionary adressTemper = new Dictionary(10000); Dictionary result = new Dictionary(10000); + + // 收集打包资源 foreach (var collector in Collectors) { var temper = collector.GetAllCollectAssets(this); foreach (var assetInfo in temper) { if (result.ContainsKey(assetInfo.AssetPath) == false) - { result.Add(assetInfo.AssetPath, assetInfo); - } else - { throw new Exception($"The collecting asset file is existed : {assetInfo.AssetPath} in grouper : {GrouperName}"); - } } } // 检测可寻址地址是否重复 if (AssetBundleGrouperSettingData.Setting.EnableAddressable) { - foreach (var collectInfo in result) + HashSet adressTemper = new HashSet(); + foreach (var collectInfoPair in result) { - string address = collectInfo.Value.Address; - if (adressTemper.ContainsKey(address) == false) + if (collectInfoPair.Value.CollectorType == ECollectorType.MainCollector) { - adressTemper.Add(address, address); - } - else - { - throw new Exception($"The address is existed : {address} in grouper : {GrouperName}"); + string address = collectInfoPair.Value.Address; + if (adressTemper.Contains(address) == false) + adressTemper.Add(address); + else + throw new Exception($"The address is existed : {address} in grouper : {GrouperName}"); } } } + // 返回列表 return result.Values.ToList(); } } diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfig.cs b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfig.cs index ffd4ccf..80d3e3c 100644 --- a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfig.cs +++ b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperConfig.cs @@ -17,14 +17,13 @@ namespace YooAsset.Editor public const string XmlGrouperName = "GrouperName"; public const string XmlGrouperDesc = "GrouperDesc"; public const string XmlCollector = "Collector"; - public const string XmlDirectory = "CollectPath"; + public const string XmlCollectPath = "CollectPath"; + public const string XmlCollectorType = "CollectType"; public const string XmlAddressRule = "AddressRule"; public const string XmlPackRule = "PackRule"; public const string XmlFilterRule = "FilterRule"; - public const string XmlNotWriteToAssetList = "NotWriteToAssetList"; public const string XmlAssetTags = "AssetTags"; - - + /// /// 导入XML配置表 /// @@ -81,25 +80,25 @@ namespace YooAsset.Editor foreach (var collectorNode in collectorNodeList) { XmlElement collectorElement = collectorNode as XmlElement; - if (collectorElement.HasAttribute(XmlDirectory) == false) - throw new Exception($"Not found attribute {XmlDirectory} in {XmlCollector}"); + if (collectorElement.HasAttribute(XmlCollectPath) == false) + throw new Exception($"Not found attribute {XmlCollectPath} in {XmlCollector}"); + if (collectorElement.HasAttribute(XmlCollectorType) == false) + throw new Exception($"Not found attribute {XmlCollectorType} in {XmlCollector}"); if (collectorElement.HasAttribute(XmlAddressRule) == false) throw new Exception($"Not found attribute {XmlAddressRule} in {XmlCollector}"); if (collectorElement.HasAttribute(XmlPackRule) == false) throw new Exception($"Not found attribute {XmlPackRule} in {XmlCollector}"); if (collectorElement.HasAttribute(XmlFilterRule) == false) throw new Exception($"Not found attribute {XmlFilterRule} in {XmlCollector}"); - if (collectorElement.HasAttribute(XmlNotWriteToAssetList) == false) - throw new Exception($"Not found attribute {XmlNotWriteToAssetList} in {XmlCollector}"); if (collectorElement.HasAttribute(XmlAssetTags) == false) throw new Exception($"Not found attribute {XmlAssetTags} in {XmlCollector}"); AssetBundleCollector collector = new AssetBundleCollector(); - collector.CollectPath = collectorElement.GetAttribute(XmlDirectory); + collector.CollectPath = collectorElement.GetAttribute(XmlCollectPath); + collector.CollectorType = StringUtility.NameToEnum(collectorElement.GetAttribute(XmlCollectorType)); collector.AddressRuleName = collectorElement.GetAttribute(XmlAddressRule); collector.PackRuleName = collectorElement.GetAttribute(XmlPackRule); collector.FilterRuleName = collectorElement.GetAttribute(XmlFilterRule); - collector.NotWriteToAssetList = collectorElement.GetAttribute(XmlNotWriteToAssetList) == "True" ? true : false; collector.AssetTags = collectorElement.GetAttribute(XmlAssetTags); ; grouper.Collectors.Add(collector); } @@ -149,11 +148,11 @@ namespace YooAsset.Editor foreach (var collector in grouper.Collectors) { var collectorElement = xmlDoc.CreateElement(XmlCollector); - collectorElement.SetAttribute(XmlDirectory, collector.CollectPath); + collectorElement.SetAttribute(XmlCollectPath, collector.CollectPath); + collectorElement.SetAttribute(XmlCollectorType, collector.CollectorType.ToString()); collectorElement.SetAttribute(XmlAddressRule, collector.AddressRuleName); collectorElement.SetAttribute(XmlPackRule, collector.PackRuleName); collectorElement.SetAttribute(XmlFilterRule, collector.FilterRuleName); - collectorElement.SetAttribute(XmlNotWriteToAssetList, collector.NotWriteToAssetList.ToString()); collectorElement.SetAttribute(XmlAssetTags, collector.AssetTags); grouperElement.AppendChild(collectorElement); } diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperHelper.cs b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperRuntimeSupport.cs similarity index 67% rename from Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperHelper.cs rename to Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperRuntimeSupport.cs index 0f6581c..4b585a5 100644 --- a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperHelper.cs +++ b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperRuntimeSupport.cs @@ -6,45 +6,11 @@ using UnityEditor; namespace YooAsset.Editor { - public static class AssetBundleGrouperHelper + /// + /// 编辑器下运行时支持 + /// + public static class AssetBundleGrouperRuntimeSupport { - /// - /// 收集着色器的资源包名称 - /// - public static string CollectShaderBundleName(string assetPath) - { - // 如果自动收集所有的着色器 - if (AssetBundleGrouperSettingData.Setting.AutoCollectShaders) - { - System.Type assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath); - if (assetType == typeof(UnityEngine.Shader)) - { - string bundleName = AssetBundleGrouperSettingData.Setting.ShadersBundleName; - return CorrectBundleName(bundleName, false); - } - } - return null; - } - - /// - /// 修正资源包名称 - /// - public static string CorrectBundleName(string bundleName, bool isRawBundle) - { - if (isRawBundle) - { - string fullName = $"{bundleName}.{YooAssetSettingsData.Setting.RawFileVariant}"; - return EditorTools.GetRegularPath(fullName).ToLower(); - } - else - { - string fullName = $"{bundleName}.{YooAssetSettingsData.Setting.AssetBundleFileVariant}"; - return EditorTools.GetRegularPath(fullName).ToLower(); ; - } - } - - - #region 编辑器下运行时支持 private static readonly Dictionary _locationDic = new Dictionary(1000); public static void InitEditorPlayMode(bool enableAddressable) @@ -56,7 +22,7 @@ namespace YooAsset.Editor var collectAssetList = AssetBundleGrouperSettingData.Setting.GetAllCollectAssets(); foreach (var collectAsset in collectAssetList) { - if (collectAsset.NotWriteToAssetList) + if(collectAsset.CollectorType != ECollectorType.MainCollector) continue; string address = collectAsset.Address; @@ -71,7 +37,7 @@ namespace YooAsset.Editor var collectAssetList = AssetBundleGrouperSettingData.Setting.GetAllCollectAssets(); foreach (var collectAsset in collectAssetList) { - if (collectAsset.NotWriteToAssetList) + if (collectAsset.CollectorType != ECollectorType.MainCollector) continue; // 添加原始路径 @@ -128,6 +94,5 @@ namespace YooAsset.Editor UnityEngine.Debug.LogWarning($"Found illegal character in location : \"{location}\""); } } - #endregion } } \ No newline at end of file diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperHelper.cs.meta b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperRuntimeSupport.cs.meta similarity index 100% rename from Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperHelper.cs.meta rename to Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperRuntimeSupport.cs.meta diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperSetting.cs b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperSetting.cs index de8e5af..864afd8 100644 --- a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperSetting.cs +++ b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperSetting.cs @@ -45,41 +45,39 @@ namespace YooAsset.Editor /// public List GetAllCollectAssets() { - Dictionary adressTemper = new Dictionary(10000); Dictionary result = new Dictionary(10000); + + // 收集打包资源 foreach (var grouper in Groupers) { var temper = grouper.GetAllCollectAssets(); foreach (var assetInfo in temper) { if (result.ContainsKey(assetInfo.AssetPath) == false) - { result.Add(assetInfo.AssetPath, assetInfo); - } else - { - throw new Exception($"The collecting asset file is existed : {assetInfo.AssetPath}"); - } + throw new Exception($"The collecting asset file is existed : {assetInfo.AssetPath} in grouper setting."); } } // 检测可寻址地址是否重复 if (EnableAddressable) { - foreach (var collectInfo in result) + HashSet adressTemper = new HashSet(); + foreach (var collectInfoPair in result) { - string address = collectInfo.Value.Address; - if (adressTemper.ContainsKey(address) == false) + if (collectInfoPair.Value.CollectorType == ECollectorType.MainCollector) { - adressTemper.Add(address, address); - } - else - { - throw new Exception($"The address is existed : {address}"); + string address = collectInfoPair.Value.Address; + if (adressTemper.Contains(address) == false) + adressTemper.Add(address); + else + throw new Exception($"The address is existed : {address} in grouper setting."); } } } + // 返回列表 return result.Values.ToList(); } } diff --git a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperWindow.cs b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperWindow.cs index 1846cbb..a841a6f 100644 --- a/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperWindow.cs +++ b/Assets/YooAsset/Editor/AssetBundleGrouper/AssetBundleGrouperWindow.cs @@ -18,6 +18,7 @@ namespace YooAsset.Editor window.minSize = new Vector2(800, 600); } + private List _collectorTypeList; private List _addressRuleList; private List _packRuleList; private List _filterRuleList; @@ -38,6 +39,7 @@ namespace YooAsset.Editor VisualElement root = this.rootVisualElement; + _collectorTypeList = new List() { $"{nameof(ECollectorType.MainCollector)}", $"{nameof(ECollectorType.StaticCollector)}"}; _addressRuleList = AssetBundleGrouperSettingData.GetAddressRuleNames(); _packRuleList = AssetBundleGrouperSettingData.GetPackRuleNames(); _filterRuleList = AssetBundleGrouperSettingData.GetFilterRuleNames(); @@ -318,6 +320,13 @@ namespace YooAsset.Editor label.style.width = 90; elementBottom.Add(label); } + { + var popupField = new PopupField(_collectorTypeList, 0); + popupField.name = "PopupField0"; + popupField.style.unityTextAlign = TextAnchor.MiddleLeft; + popupField.style.width = 150; + elementBottom.Add(popupField); + } if (_enableAddressableToogle.value) { var popupField = new PopupField(_addressRuleList, 0); @@ -340,17 +349,6 @@ namespace YooAsset.Editor popupField.style.width = 150; elementBottom.Add(popupField); } - { - var toggle = new Toggle(); - toggle.name = "Toggle1"; - toggle.label = "NotWriteToAssetList"; - toggle.style.unityTextAlign = TextAnchor.MiddleLeft; - toggle.style.width = 150; - toggle.style.marginLeft = 20; - elementBottom.Add(toggle); - var label = toggle.Q