Update AssetBundleBuilder

pull/33/head
hevinci 2022-08-03 15:43:54 +08:00
parent 3e716887c9
commit 5b8a004720
9 changed files with 131 additions and 58 deletions

View File

@ -173,14 +173,11 @@ namespace YooAsset.Editor
if (IsRawAsset) if (IsRawAsset)
throw new Exception("Should never get here !"); throw new Exception("Should never get here !");
if (AssetBundleCollectorSettingData.Setting.AutoCollectShaders) if (IsShaderAsset)
{ {
if (IsShaderAsset) string shareBundleName = YooAssetSettingsData.GetUnityShadersBundleFullName();
{ _shareBundleName = EditorTools.GetRegularPath(shareBundleName).ToLower();
string shareBundleName = $"{AssetBundleCollectorSettingData.Setting.ShadersBundleName}.{YooAssetSettingsData.Setting.AssetBundleFileVariant}"; return;
_shareBundleName = EditorTools.GetRegularPath(shareBundleName).ToLower();
return;
}
} }
if (_referenceBundleNames.Count > 1) if (_referenceBundleNames.Count > 1)

View File

@ -63,16 +63,6 @@ namespace YooAsset.Editor
/// </summary> /// </summary>
public bool CopyBuildinTagFiles; public bool CopyBuildinTagFiles;
/// <summary>
/// 自动收集着色器
/// </summary>
public bool AutoCollectShaders;
/// <summary>
/// 自动收集的着色器资源包名称
/// </summary>
public string ShadersBundleName;
/// <summary> /// <summary>
/// 加密服务类名称 /// 加密服务类名称
/// </summary> /// </summary>

View File

@ -0,0 +1,52 @@
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Build.Pipeline;
using UnityEditor.Build.Pipeline.Interfaces;
namespace UnityEditor.Build.Pipeline.Tasks
{
public static class SBPBuildTasks
{
public static IList<IBuildTask> Create(string builtInShaderBundleName)
{
var buildTasks = new List<IBuildTask>();
// Setup
buildTasks.Add(new SwitchToBuildPlatform());
buildTasks.Add(new RebuildSpriteAtlasCache());
// Player Scripts
buildTasks.Add(new BuildPlayerScripts());
buildTasks.Add(new PostScriptsCallback());
// Dependency
buildTasks.Add(new CalculateSceneDependencyData());
#if UNITY_2019_3_OR_NEWER
buildTasks.Add(new CalculateCustomDependencyData());
#endif
buildTasks.Add(new CalculateAssetDependencyData());
buildTasks.Add(new StripUnusedSpriteSources());
buildTasks.Add(new CreateBuiltInShadersBundle(builtInShaderBundleName));
buildTasks.Add(new PostDependencyCallback());
// Packing
buildTasks.Add(new GenerateBundlePacking());
buildTasks.Add(new UpdateBundleObjectLayout());
buildTasks.Add(new GenerateBundleCommands());
buildTasks.Add(new GenerateSubAssetPathMaps());
buildTasks.Add(new GenerateBundleMaps());
buildTasks.Add(new PostPackingCallback());
// Writing
buildTasks.Add(new WriteSerializedFiles());
buildTasks.Add(new ArchiveAndCompressBundles());
buildTasks.Add(new AppendBundleHash());
buildTasks.Add(new GenerateLinkXml());
buildTasks.Add(new PostWritingCallback());
return buildTasks;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f38422f6a64300243af6b4fbf84644ba
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2,11 +2,10 @@
using System.Linq; using System.Linq;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEditor.Build.Pipeline; using UnityEditor.Build.Pipeline;
using UnityEditor.Build.Pipeline.Interfaces; using UnityEditor.Build.Pipeline.Interfaces;
using UnityEditor.Build.Pipeline.Tasks;
namespace YooAsset.Editor namespace YooAsset.Editor
{ {
@ -34,7 +33,8 @@ namespace YooAsset.Editor
// 开始构建 // 开始构建
IBundleBuildResults buildResults; IBundleBuildResults buildResults;
var buildParameters = buildParametersContext.GetSBPBuildParameters(); var buildParameters = buildParametersContext.GetSBPBuildParameters();
var taskList = DefaultBuildTasks.Create(DefaultBuildTasks.Preset.AssetBundleBuiltInShaderExtraction); var shadersBunldeName = YooAssetSettingsData.GetUnityShadersBundleFullName();
var taskList = SBPBuildTasks.Create(shadersBunldeName);
ReturnCode exitCode = ContentPipeline.BuildAssetBundles(buildParameters, buildContent, out buildResults, taskList); ReturnCode exitCode = ContentPipeline.BuildAssetBundles(buildParameters, buildContent, out buildResults, taskList);
if (exitCode < 0) if (exitCode < 0)
{ {
@ -46,13 +46,6 @@ namespace YooAsset.Editor
buildResultContext.Results = buildResults; buildResultContext.Results = buildResults;
context.SetContextObject(buildResultContext); context.SetContextObject(buildResultContext);
// 添加Unity内置资源包信息
if (buildResults.BundleInfos.Keys.Any(t => t == YooAssetSettings.UnityBuiltInShadersBundleName))
{
BuildBundleInfo builtInBundleInfo = new BuildBundleInfo(YooAssetSettings.UnityBuiltInShadersBundleName);
buildMapContext.BundleInfos.Add(builtInBundleInfo);
}
// 拷贝原生文件 // 拷贝原生文件
if (buildMode == EBuildMode.ForceRebuild || buildMode == EBuildMode.IncrementalBuild) if (buildMode == EBuildMode.ForceRebuild || buildMode == EBuildMode.IncrementalBuild)
{ {

View File

@ -183,24 +183,31 @@ namespace YooAsset.Editor
/// </summary> /// </summary>
private void UpdateBuiltInBundleReference(PatchManifest patchManifest, IBundleBuildResults buildResults) private void UpdateBuiltInBundleReference(PatchManifest patchManifest, IBundleBuildResults buildResults)
{ {
// 获取所有依赖内置资源包的资源包列表 // 获取所有依赖着色器资源包的资源包列表
List<string> builtInBundleReferenceList = new List<string>(); string shadersBunldeName = YooAssetSettingsData.GetUnityShadersBundleFullName();
List<string> shaderBundleReferenceList = new List<string>();
foreach (var valuePair in buildResults.BundleInfos) foreach (var valuePair in buildResults.BundleInfos)
{ {
if (valuePair.Value.Dependencies.Any(t => t == YooAssetSettings.UnityBuiltInShadersBundleName)) if (valuePair.Value.Dependencies.Any(t => t == shadersBunldeName))
builtInBundleReferenceList.Add(valuePair.Key); shaderBundleReferenceList.Add(valuePair.Key);
} }
// 获取着色器资源包索引
Predicate<PatchBundle> predicate = new Predicate<PatchBundle>(s => s.BundleName == shadersBunldeName);
int shaderBundleId = patchManifest.BundleList.FindIndex(predicate);
if (shaderBundleId == -1)
throw new Exception("没有发现着色器资源包!");
// 检测依赖交集并更新依赖ID // 检测依赖交集并更新依赖ID
int builtInBundleId = patchManifest.BundleList.Count - 1;
foreach (var patchAsset in patchManifest.AssetList) foreach (var patchAsset in patchManifest.AssetList)
{ {
List<string> dependBundles = GetPatchAssetAllDependBundles(patchManifest, patchAsset); List<string> dependBundles = GetPatchAssetAllDependBundles(patchManifest, patchAsset);
List<string> conflictAssetPathList = dependBundles.Intersect(builtInBundleReferenceList).ToList(); List<string> conflictAssetPathList = dependBundles.Intersect(shaderBundleReferenceList).ToList();
if (conflictAssetPathList.Count > 0) if (conflictAssetPathList.Count > 0)
{ {
List<int> newDependIDs = new List<int>(patchAsset.DependIDs); List<int> newDependIDs = new List<int>(patchAsset.DependIDs);
newDependIDs.Add(builtInBundleId); if(newDependIDs.Contains(shaderBundleId) == false)
newDependIDs.Add(shaderBundleId);
patchAsset.DependIDs = newDependIDs.ToArray(); patchAsset.DependIDs = newDependIDs.ToArray();
} }
} }

View File

@ -48,8 +48,6 @@ namespace YooAsset.Editor
buildReport.Summary.BuildinTags = buildParameters.Parameters.BuildinTags; buildReport.Summary.BuildinTags = buildParameters.Parameters.BuildinTags;
buildReport.Summary.EnableAddressable = buildParameters.Parameters.EnableAddressable; buildReport.Summary.EnableAddressable = buildParameters.Parameters.EnableAddressable;
buildReport.Summary.CopyBuildinTagFiles = buildParameters.Parameters.CopyBuildinTagFiles; buildReport.Summary.CopyBuildinTagFiles = buildParameters.Parameters.CopyBuildinTagFiles;
buildReport.Summary.AutoCollectShaders = AssetBundleCollectorSettingData.Setting.AutoCollectShaders;
buildReport.Summary.ShadersBundleName = AssetBundleCollectorSettingData.Setting.ShadersBundleName;
buildReport.Summary.EncryptionServicesClassName = buildParameters.Parameters.EncryptionServices == null ? buildReport.Summary.EncryptionServicesClassName = buildParameters.Parameters.EncryptionServices == null ?
"null" : buildParameters.Parameters.EncryptionServices.GetType().FullName; "null" : buildParameters.Parameters.EncryptionServices.GetType().FullName;

View File

@ -14,7 +14,7 @@ namespace YooAsset.Editor
void IBuildTask.Run(BuildContext context) void IBuildTask.Run(BuildContext context)
{ {
var buildParametersContext = context.GetContextObject<BuildParametersContext>(); var buildParametersContext = context.GetContextObject<BuildParametersContext>();
// 模拟构建模式下跳过验证 // 模拟构建模式下跳过验证
if (buildParametersContext.Parameters.BuildMode == EBuildMode.SimulateBuild) if (buildParametersContext.Parameters.BuildMode == EBuildMode.SimulateBuild)
return; return;
@ -37,20 +37,31 @@ namespace YooAsset.Editor
string[] buildedBundles = unityManifest.GetAllAssetBundles(); string[] buildedBundles = unityManifest.GetAllAssetBundles();
// 1. 过滤掉原生Bundle // 1. 过滤掉原生Bundle
string[] expectBundles = buildMapContext.BundleInfos.Where(t => t.IsRawFile == false).Select(t => t.BundleName).ToArray(); string[] mapBundles = buildMapContext.BundleInfos.Where(t => t.IsRawFile == false).Select(t => t.BundleName).ToArray();
// 2. 验证Bundle // 2. 验证Bundle
List<string> intersectBundleList = buildedBundles.Except(expectBundles).ToList(); List<string> exceptBundleList1 = buildedBundles.Except(mapBundles).ToList();
if (intersectBundleList.Count > 0) if (exceptBundleList1.Count > 0)
{ {
foreach (var intersectBundle in intersectBundleList) foreach (var exceptBundle in exceptBundleList1)
{ {
Debug.LogWarning($"差异资源包: {intersectBundle}"); Debug.LogWarning($"差异资源包: {exceptBundle}");
} }
throw new System.Exception("存在差异资源包!请查看警告信息!"); throw new System.Exception("存在差异资源包!请查看警告信息!");
} }
// 3. 验证Asset // 3. 验证Bundle
List<string> exceptBundleList2 = mapBundles.Except(buildedBundles).ToList();
if (exceptBundleList2.Count > 0)
{
foreach (var exceptBundle in exceptBundleList2)
{
Debug.LogWarning($"差异资源包: {exceptBundle}");
}
throw new System.Exception("存在差异资源包!请查看警告信息!");
}
// 4. 验证Asset
bool isPass = true; bool isPass = true;
var buildMode = buildParameters.Parameters.BuildMode; var buildMode = buildParameters.Parameters.BuildMode;
if (buildMode == EBuildMode.ForceRebuild || buildMode == EBuildMode.IncrementalBuild) if (buildMode == EBuildMode.ForceRebuild || buildMode == EBuildMode.IncrementalBuild)
@ -59,15 +70,20 @@ namespace YooAsset.Editor
foreach (var buildedBundle in buildedBundles) foreach (var buildedBundle in buildedBundles)
{ {
string filePath = $"{buildParameters.PipelineOutputDirectory}/{buildedBundle}"; string filePath = $"{buildParameters.PipelineOutputDirectory}/{buildedBundle}";
string[] allBuildinAssetPaths = GetAssetBundleAllAssets(filePath); string[] buildedAssetPaths = GetAssetBundleAllAssets(filePath);
string[] expectBuildinAssetPaths = buildMapContext.GetBuildinAssetPaths(buildedBundle); string[] mapAssetPaths = buildMapContext.GetBuildinAssetPaths(buildedBundle);
if (expectBuildinAssetPaths.Length != allBuildinAssetPaths.Length) if (mapAssetPaths.Length != buildedAssetPaths.Length)
{ {
Debug.LogWarning($"构建的Bundle文件内的资源对象数量和预期不匹配 : {buildedBundle}"); Debug.LogWarning($"构建的Bundle文件内的资源对象数量和预期不匹配 : {buildedBundle}");
var intersectAssetList = expectBuildinAssetPaths.Except(allBuildinAssetPaths).ToList(); var exceptAssetList1 = mapAssetPaths.Except(buildedAssetPaths).ToList();
foreach (var intersectAssset in intersectAssetList) foreach (var excpetAsset in exceptAssetList1)
{ {
Debug.LogWarning($"构建失败的资源对象路径为 : {intersectAssset}"); Debug.LogWarning($"构建失败的资源对象路径为 : {excpetAsset}");
}
var exceptAssetList2 = buildedAssetPaths.Except(mapAssetPaths).ToList();
foreach (var excpetAsset in exceptAssetList2)
{
Debug.LogWarning($"构建失败的资源对象路径为 : {excpetAsset}");
} }
isPass = false; isPass = false;
continue; continue;
@ -75,6 +91,7 @@ namespace YooAsset.Editor
EditorTools.DisplayProgressBar("验证构建结果", ++progressValue, buildedBundles.Length); EditorTools.DisplayProgressBar("验证构建结果", ++progressValue, buildedBundles.Length);
} }
EditorTools.ClearProgressBar(); EditorTools.ClearProgressBar();
if (isPass == false) if (isPass == false)
{ {
throw new Exception("构建结果验证没有通过,请参考警告日志!"); throw new Exception("构建结果验证没有通过,请参考警告日志!");

View File

@ -35,21 +35,29 @@ namespace YooAsset.Editor
{ {
var buildParameters = context.GetContextObject<BuildParametersContext>(); var buildParameters = context.GetContextObject<BuildParametersContext>();
var buildMapContext = context.GetContextObject<BuildMapContext>(); var buildMapContext = context.GetContextObject<BuildMapContext>();
// 1. 移除特定Bundle
List<string> buildedBundles = buildResults.BundleInfos.Keys.ToList(); List<string> buildedBundles = buildResults.BundleInfos.Keys.ToList();
buildedBundles.Remove(YooAssetSettings.UnityBuiltInShadersBundleName);
// 2. 过滤掉原生Bundle // 1. 过滤掉原生Bundle
List<string> expectBundles = buildMapContext.BundleInfos.Where(t => t.IsRawFile == false).Select(t => t.BundleName).ToList(); List<string> expectBundles = buildMapContext.BundleInfos.Where(t => t.IsRawFile == false).Select(t => t.BundleName).ToList();
// 3. 验证Bundle // 2. 验证Bundle
List<string> intersectBundleList = buildedBundles.Except(expectBundles).ToList(); List<string> exceptBundleList1 = buildedBundles.Except(expectBundles).ToList();
if (intersectBundleList.Count > 0) if (exceptBundleList1.Count > 0)
{ {
foreach (var intersectBundle in intersectBundleList) foreach (var exceptBundle in exceptBundleList1)
{ {
Debug.LogWarning($"差异资源包: {intersectBundle}"); Debug.LogWarning($"差异资源包: {exceptBundle}");
}
throw new System.Exception("存在差异资源包!请查看警告信息!");
}
// 3. 验证Bundle
List<string> exceptBundleList2 = expectBundles.Except(buildedBundles).ToList();
if (exceptBundleList2.Count > 0)
{
foreach (var exceptBundle in exceptBundleList2)
{
Debug.LogWarning($"差异资源包: {exceptBundle}");
} }
throw new System.Exception("存在差异资源包!请查看警告信息!"); throw new System.Exception("存在差异资源包!请查看警告信息!");
} }