diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildBundleInfo.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildBundleInfo.cs index ac03157..4063069 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildBundleInfo.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildBundleInfo.cs @@ -159,12 +159,12 @@ namespace YooAsset.Editor { if (assetInfo.AllDependAssetInfos == null) continue; - foreach (var depend in assetInfo.AllDependAssetInfos) + foreach (var dependAssetInfo in assetInfo.AllDependAssetInfos) { - if (depend.HasBundleName() == false) + if (dependAssetInfo.HasBundleName() == false) { - if (result.Contains(depend.AssetPath) == false) - result.Add(depend.AssetPath); + if (result.Contains(dependAssetInfo.AssetPath) == false) + result.Add(dependAssetInfo.AssetPath); } } } diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskGetBuildMap.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskGetBuildMap.cs index 1e3dbe7..f3cd9a8 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskGetBuildMap.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskGetBuildMap.cs @@ -26,41 +26,28 @@ namespace YooAsset.Editor /// public BuildMapContext CreateBuildMap(EBuildMode buildMode, string packageName) { - Dictionary buildAssetInfoDic = new Dictionary(1000); + Dictionary allBuildAssetInfoDic = new Dictionary(1000); // 1. 检测配置合法性 AssetBundleCollectorSettingData.Setting.CheckConfigError(); // 2. 获取所有收集器收集的资源 var collectResult = AssetBundleCollectorSettingData.Setting.GetPackageAssets(buildMode, packageName); - List collectAssetInfos = collectResult.CollectAssets; + List allCollectAssetInfos = collectResult.CollectAssets; // 3. 剔除未被引用的依赖项资源 - List removeDependList = new List(); - foreach (var collectAssetInfo in collectAssetInfos) - { - if (collectAssetInfo.CollectorType == ECollectorType.DependAssetCollector) - { - if (IsRemoveDependAsset(collectAssetInfos, collectAssetInfo.AssetPath)) - removeDependList.Add(collectAssetInfo); - } - } - foreach (var removeValue in removeDependList) - { - collectAssetInfos.Remove(removeValue); - } + RemoveZeroReferenceAssets(allCollectAssetInfos); // 4. 录入所有收集器收集的资源 - foreach (var collectAssetInfo in collectAssetInfos) + foreach (var collectAssetInfo in allCollectAssetInfos) { - if (buildAssetInfoDic.ContainsKey(collectAssetInfo.AssetPath) == false) + if (allBuildAssetInfoDic.ContainsKey(collectAssetInfo.AssetPath) == false) { - var buildAssetInfo = new BuildAssetInfo( - collectAssetInfo.CollectorType, collectAssetInfo.BundleName, + var buildAssetInfo = new BuildAssetInfo(collectAssetInfo.CollectorType, collectAssetInfo.BundleName, collectAssetInfo.Address, collectAssetInfo.AssetPath, collectAssetInfo.IsRawAsset); buildAssetInfo.AddAssetTags(collectAssetInfo.AssetTags); buildAssetInfo.AddBundleTags(collectAssetInfo.AssetTags); - buildAssetInfoDic.Add(collectAssetInfo.AssetPath, buildAssetInfo); + allBuildAssetInfoDic.Add(collectAssetInfo.AssetPath, buildAssetInfo); } else { @@ -69,69 +56,68 @@ namespace YooAsset.Editor } // 5. 录入所有收集资源的依赖资源 - foreach (var collectAssetInfo in collectAssetInfos) + foreach (var collectAssetInfo in allCollectAssetInfos) { string collectAssetBundleName = collectAssetInfo.BundleName; foreach (var dependAssetPath in collectAssetInfo.DependAssets) { - if (buildAssetInfoDic.ContainsKey(dependAssetPath)) + if (allBuildAssetInfoDic.ContainsKey(dependAssetPath)) { - buildAssetInfoDic[dependAssetPath].AddBundleTags(collectAssetInfo.AssetTags); - buildAssetInfoDic[dependAssetPath].AddReferenceBundleName(collectAssetBundleName); + allBuildAssetInfoDic[dependAssetPath].AddBundleTags(collectAssetInfo.AssetTags); + allBuildAssetInfoDic[dependAssetPath].AddReferenceBundleName(collectAssetBundleName); } else { var buildAssetInfo = new BuildAssetInfo(dependAssetPath); buildAssetInfo.AddBundleTags(collectAssetInfo.AssetTags); buildAssetInfo.AddReferenceBundleName(collectAssetBundleName); - buildAssetInfoDic.Add(dependAssetPath, buildAssetInfo); + allBuildAssetInfoDic.Add(dependAssetPath, buildAssetInfo); } } } // 6. 填充所有收集资源的依赖列表 - foreach (var collectAssetInfo in collectAssetInfos) + foreach (var collectAssetInfo in allCollectAssetInfos) { var dependAssetInfos = new List(collectAssetInfo.DependAssets.Count); foreach (var dependAssetPath in collectAssetInfo.DependAssets) { - if (buildAssetInfoDic.TryGetValue(dependAssetPath, out BuildAssetInfo value)) + if (allBuildAssetInfoDic.TryGetValue(dependAssetPath, out BuildAssetInfo value)) dependAssetInfos.Add(value); else throw new Exception("Should never get here !"); } - buildAssetInfoDic[collectAssetInfo.AssetPath].SetAllDependAssetInfos(dependAssetInfos); + allBuildAssetInfoDic[collectAssetInfo.AssetPath].SetAllDependAssetInfos(dependAssetInfos); } // 7. 记录关键信息 BuildMapContext context = new BuildMapContext(); - context.AssetFileCount = buildAssetInfoDic.Count; + context.AssetFileCount = allBuildAssetInfoDic.Count; context.EnableAddressable = collectResult.Command.EnableAddressable; context.UniqueBundleName = collectResult.Command.UniqueBundleName; context.ShadersBundleName = collectResult.Command.ShadersBundleName; // 8. 计算共享的资源包名 var command = collectResult.Command; - foreach (KeyValuePair pair in buildAssetInfoDic) + foreach (var buildAssetInfo in allBuildAssetInfoDic.Values) { - pair.Value.CalculateShareBundleName(command.UniqueBundleName, command.PackageName, command.ShadersBundleName); + buildAssetInfo.CalculateShareBundleName(command.UniqueBundleName, command.PackageName, command.ShadersBundleName); } // 9. 移除不参与构建的资源 List removeBuildList = new List(); - foreach (KeyValuePair pair in buildAssetInfoDic) + foreach (var buildAssetInfo in allBuildAssetInfoDic.Values) { - var buildAssetInfo = pair.Value; if (buildAssetInfo.HasBundleName() == false) removeBuildList.Add(buildAssetInfo); } foreach (var removeValue in removeBuildList) { - buildAssetInfoDic.Remove(removeValue.AssetPath); + allBuildAssetInfoDic.Remove(removeValue.AssetPath); } // 10. 构建资源包 - var allPackAssets = buildAssetInfoDic.Values.ToList(); + var allPackAssets = allBuildAssetInfoDic.Values.ToList(); if (allPackAssets.Count == 0) throw new Exception("构建的资源列表不能为空"); foreach (var assetInfo in allPackAssets) @@ -140,20 +126,55 @@ namespace YooAsset.Editor } return context; } - private bool IsRemoveDependAsset(List allCollectAssets, string dependAssetPath) + private void RemoveZeroReferenceAssets(List allCollectAssetInfos) { - foreach (var collectAssetInfo in allCollectAssets) + // 1. 检测是否任何存在依赖资源 + bool hasAnyDependAsset = false; + foreach (var collectAssetInfo in allCollectAssetInfos) + { + var collectorType = collectAssetInfo.CollectorType; + if (collectorType == ECollectorType.DependAssetCollector) + { + hasAnyDependAsset = true; + break; + } + } + if (hasAnyDependAsset == false) + return; + + // 2. 获取所有主资源的依赖资源集合 + HashSet allDependAsset = new HashSet(); + foreach (var collectAssetInfo in allCollectAssetInfos) { var collectorType = collectAssetInfo.CollectorType; if (collectorType == ECollectorType.MainAssetCollector || collectorType == ECollectorType.StaticAssetCollector) { - if (collectAssetInfo.DependAssets.Contains(dependAssetPath)) - return false; + foreach (var dependAsset in collectAssetInfo.DependAssets) + { + if (allDependAsset.Contains(dependAsset) == false) + allDependAsset.Add(dependAsset); + } } } - BuildLogger.Log($"发现未被依赖的资源并自动移除 : {dependAssetPath}"); - return true; + // 3. 找出所有零引用的依赖资源集合 + List removeList = new List(); + foreach (var collectAssetInfo in allCollectAssetInfos) + { + var collectorType = collectAssetInfo.CollectorType; + if (collectorType == ECollectorType.DependAssetCollector) + { + if (allDependAsset.Contains(collectAssetInfo.AssetPath) == false) + removeList.Add(collectAssetInfo); + } + } + + // 4. 移除所有零引用的依赖资源 + foreach (var removeValue in removeList) + { + BuildLogger.Log($"发现未被依赖的资源并自动移除 : {removeValue.AssetPath}"); + allCollectAssetInfos.Remove(removeValue); + } } /// diff --git a/Assets/YooAsset/Editor/AssetBundleCollector/AssetBundleCollector.cs b/Assets/YooAsset/Editor/AssetBundleCollector/AssetBundleCollector.cs index 8d06f20..9a65e85 100644 --- a/Assets/YooAsset/Editor/AssetBundleCollector/AssetBundleCollector.cs +++ b/Assets/YooAsset/Editor/AssetBundleCollector/AssetBundleCollector.cs @@ -226,7 +226,7 @@ namespace YooAsset.Editor if (command.BuildMode == EBuildMode.SimulateBuild) collectAssetInfo.DependAssets = new List(); else - collectAssetInfo.DependAssets = GetAllDependencies(assetPath, isRawFilePackRule); + collectAssetInfo.DependAssets = GetAllDependencies(assetPath); return collectAssetInfo; } @@ -331,13 +331,13 @@ namespace YooAsset.Editor tags.AddRange(temper); return tags; } - private List GetAllDependencies(string mainAssetPath, bool isRawFilePackRule) + private List GetAllDependencies(string mainAssetPath) { - List result = new List(); string[] depends = AssetDatabase.GetDependencies(mainAssetPath, true); + List result = new List(depends.Length); foreach (string assetPath in depends) { - if (IsValidateAsset(assetPath, isRawFilePackRule)) + if (IsValidateAsset(assetPath, false)) { // 注意:排除主资源对象 if (assetPath != mainAssetPath)