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)
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);
}
}
}

View File

@ -26,41 +26,28 @@ namespace YooAsset.Editor
/// </summary>
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. 检测配置合法性
AssetBundleCollectorSettingData.Setting.CheckConfigError();
// 2. 获取所有收集器收集的资源
var collectResult = AssetBundleCollectorSettingData.Setting.GetPackageAssets(buildMode, packageName);
List<CollectAssetInfo> collectAssetInfos = collectResult.CollectAssets;
List<CollectAssetInfo> allCollectAssetInfos = collectResult.CollectAssets;
// 3. 剔除未被引用的依赖项资源
List<CollectAssetInfo> removeDependList = new List<CollectAssetInfo>();
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<BuildAssetInfo>(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<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. 移除不参与构建的资源
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)
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<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;
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<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>

View File

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