update asset bundle builder

pull/82/head
hevinci 2023-03-14 16:12:48 +08:00
parent c9b775d8ff
commit 254da59b7a
3 changed files with 70 additions and 49 deletions

View File

@ -159,12 +159,12 @@ namespace YooAsset.Editor
{ {
if (assetInfo.AllDependAssetInfos == null) if (assetInfo.AllDependAssetInfos == null)
continue; 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) if (result.Contains(dependAssetInfo.AssetPath) == false)
result.Add(depend.AssetPath); result.Add(dependAssetInfo.AssetPath);
} }
} }
} }

View File

@ -26,41 +26,28 @@ namespace YooAsset.Editor
/// </summary> /// </summary>
public BuildMapContext CreateBuildMap(EBuildMode buildMode, string packageName) public BuildMapContext CreateBuildMap(EBuildMode buildMode, string packageName)
{ {
Dictionary<string, BuildAssetInfo> buildAssetInfoDic = new Dictionary<string, BuildAssetInfo>(1000); Dictionary<string, BuildAssetInfo> allBuildAssetInfoDic = new Dictionary<string, BuildAssetInfo>(1000);
// 1. 检测配置合法性 // 1. 检测配置合法性
AssetBundleCollectorSettingData.Setting.CheckConfigError(); AssetBundleCollectorSettingData.Setting.CheckConfigError();
// 2. 获取所有收集器收集的资源 // 2. 获取所有收集器收集的资源
var collectResult = AssetBundleCollectorSettingData.Setting.GetPackageAssets(buildMode, packageName); var collectResult = AssetBundleCollectorSettingData.Setting.GetPackageAssets(buildMode, packageName);
List<CollectAssetInfo> collectAssetInfos = collectResult.CollectAssets; List<CollectAssetInfo> allCollectAssetInfos = collectResult.CollectAssets;
// 3. 剔除未被引用的依赖项资源 // 3. 剔除未被引用的依赖项资源
List<CollectAssetInfo> removeDependList = new List<CollectAssetInfo>(); RemoveZeroReferenceAssets(allCollectAssetInfos);
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);
}
// 4. 录入所有收集器收集的资源 // 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( var buildAssetInfo = new BuildAssetInfo(collectAssetInfo.CollectorType, collectAssetInfo.BundleName,
collectAssetInfo.CollectorType, collectAssetInfo.BundleName,
collectAssetInfo.Address, collectAssetInfo.AssetPath, collectAssetInfo.IsRawAsset); collectAssetInfo.Address, collectAssetInfo.AssetPath, collectAssetInfo.IsRawAsset);
buildAssetInfo.AddAssetTags(collectAssetInfo.AssetTags); buildAssetInfo.AddAssetTags(collectAssetInfo.AssetTags);
buildAssetInfo.AddBundleTags(collectAssetInfo.AssetTags); buildAssetInfo.AddBundleTags(collectAssetInfo.AssetTags);
buildAssetInfoDic.Add(collectAssetInfo.AssetPath, buildAssetInfo); allBuildAssetInfoDic.Add(collectAssetInfo.AssetPath, buildAssetInfo);
} }
else else
{ {
@ -69,69 +56,68 @@ namespace YooAsset.Editor
} }
// 5. 录入所有收集资源的依赖资源 // 5. 录入所有收集资源的依赖资源
foreach (var collectAssetInfo in collectAssetInfos) foreach (var collectAssetInfo in allCollectAssetInfos)
{ {
string collectAssetBundleName = collectAssetInfo.BundleName; string collectAssetBundleName = collectAssetInfo.BundleName;
foreach (var dependAssetPath in collectAssetInfo.DependAssets) foreach (var dependAssetPath in collectAssetInfo.DependAssets)
{ {
if (buildAssetInfoDic.ContainsKey(dependAssetPath)) if (allBuildAssetInfoDic.ContainsKey(dependAssetPath))
{ {
buildAssetInfoDic[dependAssetPath].AddBundleTags(collectAssetInfo.AssetTags); allBuildAssetInfoDic[dependAssetPath].AddBundleTags(collectAssetInfo.AssetTags);
buildAssetInfoDic[dependAssetPath].AddReferenceBundleName(collectAssetBundleName); allBuildAssetInfoDic[dependAssetPath].AddReferenceBundleName(collectAssetBundleName);
} }
else else
{ {
var buildAssetInfo = new BuildAssetInfo(dependAssetPath); var buildAssetInfo = new BuildAssetInfo(dependAssetPath);
buildAssetInfo.AddBundleTags(collectAssetInfo.AssetTags); buildAssetInfo.AddBundleTags(collectAssetInfo.AssetTags);
buildAssetInfo.AddReferenceBundleName(collectAssetBundleName); buildAssetInfo.AddReferenceBundleName(collectAssetBundleName);
buildAssetInfoDic.Add(dependAssetPath, buildAssetInfo); allBuildAssetInfoDic.Add(dependAssetPath, buildAssetInfo);
} }
} }
} }
// 6. 填充所有收集资源的依赖列表 // 6. 填充所有收集资源的依赖列表
foreach (var collectAssetInfo in collectAssetInfos) foreach (var collectAssetInfo in allCollectAssetInfos)
{ {
var dependAssetInfos = new List<BuildAssetInfo>(collectAssetInfo.DependAssets.Count); var dependAssetInfos = new List<BuildAssetInfo>(collectAssetInfo.DependAssets.Count);
foreach (var dependAssetPath in collectAssetInfo.DependAssets) foreach (var dependAssetPath in collectAssetInfo.DependAssets)
{ {
if (buildAssetInfoDic.TryGetValue(dependAssetPath, out BuildAssetInfo value)) if (allBuildAssetInfoDic.TryGetValue(dependAssetPath, out BuildAssetInfo value))
dependAssetInfos.Add(value); dependAssetInfos.Add(value);
else else
throw new Exception("Should never get here !"); throw new Exception("Should never get here !");
} }
buildAssetInfoDic[collectAssetInfo.AssetPath].SetAllDependAssetInfos(dependAssetInfos); allBuildAssetInfoDic[collectAssetInfo.AssetPath].SetAllDependAssetInfos(dependAssetInfos);
} }
// 7. 记录关键信息 // 7. 记录关键信息
BuildMapContext context = new BuildMapContext(); BuildMapContext context = new BuildMapContext();
context.AssetFileCount = buildAssetInfoDic.Count; context.AssetFileCount = allBuildAssetInfoDic.Count;
context.EnableAddressable = collectResult.Command.EnableAddressable; context.EnableAddressable = collectResult.Command.EnableAddressable;
context.UniqueBundleName = collectResult.Command.UniqueBundleName; context.UniqueBundleName = collectResult.Command.UniqueBundleName;
context.ShadersBundleName = collectResult.Command.ShadersBundleName; context.ShadersBundleName = collectResult.Command.ShadersBundleName;
// 8. 计算共享的资源包名 // 8. 计算共享的资源包名
var command = collectResult.Command; var command = collectResult.Command;
foreach (KeyValuePair<string, BuildAssetInfo> 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. 移除不参与构建的资源 // 9. 移除不参与构建的资源
List<BuildAssetInfo> removeBuildList = new List<BuildAssetInfo>(); List<BuildAssetInfo> removeBuildList = new List<BuildAssetInfo>();
foreach (KeyValuePair<string, BuildAssetInfo> pair in buildAssetInfoDic) foreach (var buildAssetInfo in allBuildAssetInfoDic.Values)
{ {
var buildAssetInfo = pair.Value;
if (buildAssetInfo.HasBundleName() == false) if (buildAssetInfo.HasBundleName() == false)
removeBuildList.Add(buildAssetInfo); removeBuildList.Add(buildAssetInfo);
} }
foreach (var removeValue in removeBuildList) foreach (var removeValue in removeBuildList)
{ {
buildAssetInfoDic.Remove(removeValue.AssetPath); allBuildAssetInfoDic.Remove(removeValue.AssetPath);
} }
// 10. 构建资源包 // 10. 构建资源包
var allPackAssets = buildAssetInfoDic.Values.ToList(); var allPackAssets = allBuildAssetInfoDic.Values.ToList();
if (allPackAssets.Count == 0) if (allPackAssets.Count == 0)
throw new Exception("构建的资源列表不能为空"); throw new Exception("构建的资源列表不能为空");
foreach (var assetInfo in allPackAssets) foreach (var assetInfo in allPackAssets)
@ -140,20 +126,55 @@ namespace YooAsset.Editor
} }
return context; return context;
} }
private bool IsRemoveDependAsset(List<CollectAssetInfo> allCollectAssets, string dependAssetPath) private void RemoveZeroReferenceAssets(List<CollectAssetInfo> 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<string> allDependAsset = new HashSet<string>();
foreach (var collectAssetInfo in allCollectAssetInfos)
{ {
var collectorType = collectAssetInfo.CollectorType; var collectorType = collectAssetInfo.CollectorType;
if (collectorType == ECollectorType.MainAssetCollector || collectorType == ECollectorType.StaticAssetCollector) if (collectorType == ECollectorType.MainAssetCollector || collectorType == ECollectorType.StaticAssetCollector)
{ {
if (collectAssetInfo.DependAssets.Contains(dependAssetPath)) foreach (var dependAsset in collectAssetInfo.DependAssets)
return false; {
if (allDependAsset.Contains(dependAsset) == false)
allDependAsset.Add(dependAsset);
}
} }
} }
BuildLogger.Log($"发现未被依赖的资源并自动移除 : {dependAssetPath}"); // 3. 找出所有零引用的依赖资源集合
return true; List<CollectAssetInfo> removeList = new List<CollectAssetInfo>();
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);
}
} }
/// <summary> /// <summary>

View File

@ -226,7 +226,7 @@ namespace YooAsset.Editor
if (command.BuildMode == EBuildMode.SimulateBuild) if (command.BuildMode == EBuildMode.SimulateBuild)
collectAssetInfo.DependAssets = new List<string>(); collectAssetInfo.DependAssets = new List<string>();
else else
collectAssetInfo.DependAssets = GetAllDependencies(assetPath, isRawFilePackRule); collectAssetInfo.DependAssets = GetAllDependencies(assetPath);
return collectAssetInfo; return collectAssetInfo;
} }
@ -331,13 +331,13 @@ namespace YooAsset.Editor
tags.AddRange(temper); tags.AddRange(temper);
return tags; return tags;
} }
private List<string> GetAllDependencies(string mainAssetPath, bool isRawFilePackRule) private List<string> GetAllDependencies(string mainAssetPath)
{ {
List<string> result = new List<string>();
string[] depends = AssetDatabase.GetDependencies(mainAssetPath, true); string[] depends = AssetDatabase.GetDependencies(mainAssetPath, true);
List<string> result = new List<string>(depends.Length);
foreach (string assetPath in depends) foreach (string assetPath in depends)
{ {
if (IsValidateAsset(assetPath, isRawFilePackRule)) if (IsValidateAsset(assetPath, false))
{ {
// 注意:排除主资源对象 // 注意:排除主资源对象
if (assetPath != mainAssetPath) if (assetPath != mainAssetPath)