Skip to content

Commit

Permalink
update asset bundle builder
Browse files Browse the repository at this point in the history
优化了打包逻辑,提高构建速度。
  • Loading branch information
gmhevinci committed Mar 8, 2023
1 parent 5254fb6 commit 985b05f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
27 changes: 14 additions & 13 deletions Assets/YooAsset/Editor/AssetBundleBuilder/BuildMapContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace YooAsset.Editor
{
public class BuildMapContext : IContextObject
{
private readonly Dictionary<string, BuildBundleInfo> _bundleInfoDic = new Dictionary<string, BuildBundleInfo>(10000);

/// <summary>
/// 参与构建的资源总数
/// 说明:包括主动收集的资源以及其依赖的所有资源
Expand All @@ -32,7 +34,7 @@ public class BuildMapContext : IContextObject
/// <summary>
/// 资源包列表
/// </summary>
public readonly List<BuildBundleInfo> BundleInfos = new List<BuildBundleInfo>(1000);
public readonly List<BuildBundleInfo> BundleInfos = new List<BuildBundleInfo>(10000);


/// <summary>
Expand All @@ -44,7 +46,7 @@ public void PackAsset(BuildAssetInfo assetInfo)
if (string.IsNullOrEmpty(bundleName))
throw new Exception("Should never get here !");

if (TryGetBundleInfo(bundleName, out BuildBundleInfo bundleInfo))
if (_bundleInfoDic.TryGetValue(bundleName, out BuildBundleInfo bundleInfo))
{
bundleInfo.PackAsset(assetInfo);
}
Expand All @@ -53,6 +55,7 @@ public void PackAsset(BuildAssetInfo assetInfo)
BuildBundleInfo newBundleInfo = new BuildBundleInfo(bundleName);
newBundleInfo.PackAsset(assetInfo);
BundleInfos.Add(newBundleInfo);
_bundleInfoDic.Add(bundleName, newBundleInfo);
}
}

Expand All @@ -74,7 +77,7 @@ public List<BuildAssetInfo> GetAllAssets()
/// </summary>
public string[] GetBuildinAssetPaths(string bundleName)
{
if (TryGetBundleInfo(bundleName, out BuildBundleInfo bundleInfo))
if (_bundleInfoDic.TryGetValue(bundleName, out BuildBundleInfo bundleInfo))
{
return bundleInfo.GetBuildinAssetPaths();
}
Expand All @@ -100,21 +103,19 @@ public UnityEditor.AssetBundleBuild[] GetPipelineBuilds()
/// </summary>
public bool IsContainsBundle(string bundleName)
{
return TryGetBundleInfo(bundleName, out BuildBundleInfo bundleInfo);
return _bundleInfoDic.ContainsKey(bundleName);
}

public bool TryGetBundleInfo(string bundleName, out BuildBundleInfo result)
/// <summary>
/// 获取资源包信息,如果没找到返回NULL
/// </summary>
public BuildBundleInfo GetBundleInfo(string bundleName)
{
foreach (var bundleInfo in BundleInfos)
if (_bundleInfoDic.TryGetValue(bundleName, out BuildBundleInfo result))
{
if (bundleInfo.BundleName == bundleName)
{
result = bundleInfo;
return true;
}
return result;
}
result = null;
return false;
throw new Exception($"Not found bundle : {bundleName}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private List<string> GetDependBundles(PatchManifest patchManifest, PatchAsset pa
private List<string> GetDependAssets(BuildMapContext buildMapContext, string bundleName, string assetPath)
{
List<string> result = new List<string>();
if (buildMapContext.TryGetBundleInfo(bundleName, out BuildBundleInfo bundleInfo))
var bundleInfo = buildMapContext.GetBundleInfo(bundleName);
{
BuildAssetInfo findAssetInfo = null;
foreach (var buildinAsset in bundleInfo.BuildinAssets)
Expand All @@ -147,10 +147,6 @@ private List<string> GetDependAssets(BuildMapContext buildMapContext, string bun
result.Add(dependAssetInfo.AssetPath);
}
}
else
{
throw new Exception($"Not found bundle : {bundleName}");
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ private void VerifyingBuildingResult(BuildContext context, AssetBundleManifest u
{
var buildParametersContext = context.GetContextObject<BuildParametersContext>();
var buildMapContext = context.GetContextObject<BuildMapContext>();
string[] buildedBundles = unityManifest.GetAllAssetBundles();
string[] unityCreateBundles = unityManifest.GetAllAssetBundles();

// 1. 过滤掉原生Bundle
string[] mapBundles = buildMapContext.BundleInfos.Where(t => t.IsRawFile == false).Select(t => t.BundleName).ToArray();

// 2. 验证Bundle
List<string> exceptBundleList1 = buildedBundles.Except(mapBundles).ToList();
List<string> exceptBundleList1 = unityCreateBundles.Except(mapBundles).ToList();
if (exceptBundleList1.Count > 0)
{
foreach (var exceptBundle in exceptBundleList1)
Expand All @@ -51,7 +51,7 @@ private void VerifyingBuildingResult(BuildContext context, AssetBundleManifest u
}

// 3. 验证Bundle
List<string> exceptBundleList2 = mapBundles.Except(buildedBundles).ToList();
List<string> exceptBundleList2 = mapBundles.Except(unityCreateBundles).ToList();
if (exceptBundleList2.Count > 0)
{
foreach (var exceptBundle in exceptBundleList2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ private void VerifyingBuildingResult(BuildContext context, IBundleBuildResults b
{
var buildParameters = context.GetContextObject<BuildParametersContext>();
var buildMapContext = context.GetContextObject<BuildMapContext>();
List<string> buildedBundles = buildResults.BundleInfos.Keys.ToList();
List<string> unityCreateBundles = buildResults.BundleInfos.Keys.ToList();

// 1. 过滤掉原生Bundle
List<string> expectBundles = buildMapContext.BundleInfos.Where(t => t.IsRawFile == false).Select(t => t.BundleName).ToList();

// 2. 验证Bundle
List<string> exceptBundleList1 = buildedBundles.Except(expectBundles).ToList();
List<string> exceptBundleList1 = unityCreateBundles.Except(expectBundles).ToList();
if (exceptBundleList1.Count > 0)
{
foreach (var exceptBundle in exceptBundleList1)
Expand All @@ -52,7 +52,7 @@ private void VerifyingBuildingResult(BuildContext context, IBundleBuildResults b
}

// 3. 验证Bundle
List<string> exceptBundleList2 = expectBundles.Except(buildedBundles).ToList();
List<string> exceptBundleList2 = expectBundles.Except(unityCreateBundles).ToList();
if (exceptBundleList2.Count > 0)
{
foreach (var exceptBundle in exceptBundleList2)
Expand Down

0 comments on commit 985b05f

Please sign in to comment.