2025-03-11 17:46:52 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using YooAsset;
|
|
|
|
|
using YooAsset.Editor;
|
|
|
|
|
|
|
|
|
|
public static class ResourceBuildHelper
|
|
|
|
|
{
|
|
|
|
|
private const bool EnableSharePack = true;
|
|
|
|
|
private const string BuildPackageName = "DefaultPackage";
|
|
|
|
|
|
|
|
|
|
private static string GeneratePackageVersion()
|
|
|
|
|
{
|
|
|
|
|
int totalMinutes = DateTime.Now.Hour * 60 + DateTime.Now.Minute;
|
|
|
|
|
return DateTime.Now.ToString("yyyy-MM-dd") + "-" + totalMinutes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void BuildResourcePackage(ResourceBuildParameter buildParameter)
|
|
|
|
|
{
|
2025-03-20 14:17:24 +08:00
|
|
|
|
if (!Directory.Exists(buildParameter.outputPath))
|
2025-03-11 17:46:52 +08:00
|
|
|
|
{
|
2025-03-20 14:17:24 +08:00
|
|
|
|
Directory.CreateDirectory(buildParameter.outputPath);
|
2025-03-11 17:46:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string copyParams = string.Empty;
|
|
|
|
|
EBuildinFileCopyOption copyOption = EBuildinFileCopyOption.None;
|
2025-03-20 14:17:24 +08:00
|
|
|
|
if (buildParameter.buildMode == ResourceBuildMode.Online)
|
2025-03-11 17:46:52 +08:00
|
|
|
|
{
|
|
|
|
|
copyOption = EBuildinFileCopyOption.ClearAndCopyByTags;
|
|
|
|
|
copyParams = "Launch";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
copyOption = EBuildinFileCopyOption.ClearAndCopyAll;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var parameters = new ScriptableBuildParameters
|
|
|
|
|
{
|
2025-03-20 14:17:24 +08:00
|
|
|
|
BuildOutputRoot = buildParameter.outputPath,
|
|
|
|
|
BuildTarget = buildParameter.resourceBuildTarget,
|
2025-03-11 17:46:52 +08:00
|
|
|
|
PackageName = BuildPackageName,
|
|
|
|
|
BuildBundleType = (int)EBuildBundleType.AssetBundle,
|
|
|
|
|
BuildPipeline = EBuildPipeline.ScriptableBuildPipeline.ToString(),
|
|
|
|
|
BuildinFileRoot = AssetBundleBuilderHelper.GetStreamingAssetsRoot(),
|
2025-03-20 14:17:24 +08:00
|
|
|
|
PackageVersion = buildParameter.useDefaultPackageVersion ? GeneratePackageVersion() : buildParameter.packageVersion,
|
|
|
|
|
CompressOption = buildParameter.compressOption,
|
|
|
|
|
FileNameStyle = buildParameter.fileNameStyle,
|
2025-03-11 17:46:52 +08:00
|
|
|
|
VerifyBuildingResult = true,
|
|
|
|
|
ClearBuildCacheFiles = false,
|
|
|
|
|
BuildinFileCopyOption = copyOption,
|
|
|
|
|
BuildinFileCopyParams = copyParams,
|
|
|
|
|
EnableSharePackRule = EnableSharePack,
|
2025-03-20 14:17:24 +08:00
|
|
|
|
EncryptionServices = CreateEncryptionInstance(buildParameter.encryptionServiceType)
|
2025-03-11 17:46:52 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ScriptableBuildPipeline pipeline = new ScriptableBuildPipeline();
|
|
|
|
|
var report = pipeline.Run(parameters, true);
|
|
|
|
|
|
|
|
|
|
if (report.Success)
|
|
|
|
|
{
|
|
|
|
|
// if (_copyAfterBuild && !string.IsNullOrEmpty(_copyDestination))
|
|
|
|
|
// {
|
|
|
|
|
// CopyFiles(report.OutputPackageDirectory, _copyDestination);
|
|
|
|
|
// }
|
|
|
|
|
Debug.Log($"AB包构建完成!\n输出目录: {report.OutputPackageDirectory}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.Log($"错误信息: {report.ErrorInfo}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void CopyFiles(string source, string destination)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(destination))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(destination);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (string file in Directory.GetFiles(source))
|
|
|
|
|
{
|
|
|
|
|
File.Copy(file, Path.Combine(destination, Path.GetFileName(file)), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Debug.Log($"文件拷贝完成: {source} -> {destination}");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"文件拷贝失败: {e.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEncryptionServices CreateEncryptionInstance(string encryptionService)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(encryptionService)) return null;
|
|
|
|
|
|
|
|
|
|
var type = Type.GetType(encryptionService);
|
|
|
|
|
if (type != null)
|
|
|
|
|
{
|
|
|
|
|
return (IEncryptionServices)Activator.CreateInstance(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|