2022-07-15 19:34:59 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
|
|
using UnityEditor.Build.Pipeline;
|
|
|
|
|
|
using UnityEditor.Build.Pipeline.Interfaces;
|
2022-08-03 15:43:54 +08:00
|
|
|
|
using UnityEditor.Build.Pipeline.Tasks;
|
2022-07-15 19:34:59 +08:00
|
|
|
|
|
|
|
|
|
|
namespace YooAsset.Editor
|
|
|
|
|
|
{
|
|
|
|
|
|
[TaskAttribute("资源构建内容打包")]
|
|
|
|
|
|
public class TaskBuilding_SBP : IBuildTask
|
|
|
|
|
|
{
|
2022-08-03 20:47:43 +08:00
|
|
|
|
public class BuildResultContext : IContextObject
|
2022-07-15 19:34:59 +08:00
|
|
|
|
{
|
|
|
|
|
|
public IBundleBuildResults Results;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IBuildTask.Run(BuildContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
var buildParametersContext = context.GetContextObject<BuildParametersContext>();
|
|
|
|
|
|
var buildMapContext = context.GetContextObject<BuildMapContext>();
|
|
|
|
|
|
|
|
|
|
|
|
// 模拟构建模式下跳过引擎构建
|
|
|
|
|
|
var buildMode = buildParametersContext.Parameters.BuildMode;
|
|
|
|
|
|
if (buildMode == EBuildMode.SimulateBuild)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// 构建内容
|
|
|
|
|
|
var buildContent = new BundleBuildContent(buildMapContext.GetPipelineBuilds());
|
|
|
|
|
|
|
|
|
|
|
|
// 开始构建
|
|
|
|
|
|
IBundleBuildResults buildResults;
|
|
|
|
|
|
var buildParameters = buildParametersContext.GetSBPBuildParameters();
|
2022-08-03 15:43:54 +08:00
|
|
|
|
var shadersBunldeName = YooAssetSettingsData.GetUnityShadersBundleFullName();
|
|
|
|
|
|
var taskList = SBPBuildTasks.Create(shadersBunldeName);
|
2022-07-15 19:34:59 +08:00
|
|
|
|
ReturnCode exitCode = ContentPipeline.BuildAssetBundles(buildParameters, buildContent, out buildResults, taskList);
|
|
|
|
|
|
if (exitCode < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"构建过程中发生错误 : {exitCode}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BuildRunner.Log("Unity引擎打包成功!");
|
2022-08-03 20:47:43 +08:00
|
|
|
|
BuildResultContext buildResultContext = new BuildResultContext();
|
2022-07-15 19:34:59 +08:00
|
|
|
|
buildResultContext.Results = buildResults;
|
|
|
|
|
|
context.SetContextObject(buildResultContext);
|
|
|
|
|
|
|
|
|
|
|
|
if (buildMode == EBuildMode.ForceRebuild || buildMode == EBuildMode.IncrementalBuild)
|
|
|
|
|
|
{
|
|
|
|
|
|
CopyRawBundle(buildMapContext, buildParametersContext);
|
2022-08-03 20:47:43 +08:00
|
|
|
|
UpdateBuildBundleInfo(buildMapContext, buildParametersContext, buildResultContext);
|
2022-07-15 19:34:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 拷贝原生文件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void CopyRawBundle(BuildMapContext buildMapContext, BuildParametersContext buildParametersContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var bundleInfo in buildMapContext.BundleInfos)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (bundleInfo.IsRawFile)
|
|
|
|
|
|
{
|
|
|
|
|
|
string dest = $"{buildParametersContext.PipelineOutputDirectory}/{bundleInfo.BundleName}";
|
|
|
|
|
|
foreach (var buildAsset in bundleInfo.BuildinAssets)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (buildAsset.IsRawAsset)
|
|
|
|
|
|
EditorTools.CopyFile(buildAsset.AssetPath, dest, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-08-03 20:47:43 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新构建结果
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void UpdateBuildBundleInfo(BuildMapContext buildMapContext, BuildParametersContext buildParametersContext, BuildResultContext buildResult)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var bundleInfo in buildMapContext.BundleInfos)
|
|
|
|
|
|
{
|
|
|
|
|
|
string filePath = $"{buildParametersContext.PipelineOutputDirectory}/{bundleInfo.BundleName}";
|
|
|
|
|
|
bundleInfo.FileHash = HashUtility.FileMD5(filePath);
|
|
|
|
|
|
bundleInfo.FileCRC = HashUtility.FileCRC32(filePath);
|
|
|
|
|
|
bundleInfo.FileSize = FileUtility.GetFileSize(filePath);
|
|
|
|
|
|
if (bundleInfo.IsRawFile)
|
|
|
|
|
|
{
|
|
|
|
|
|
bundleInfo.ContentHash = bundleInfo.FileHash;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 注意:当资源包的依赖列表发生变化的时候,ContentHash也会发生变化!
|
|
|
|
|
|
if (buildResult.Results.BundleInfos.TryGetValue(bundleInfo.BundleName, out var value))
|
|
|
|
|
|
bundleInfo.ContentHash = value.Hash.ToString();
|
|
|
|
|
|
else
|
|
|
|
|
|
throw new Exception($"Not found bundle in build result : {bundleInfo.BundleName}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-07-15 19:34:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|