com.alicizax.unity.tuyoogam.../Editor/AssetBundleBuilder/BuildBundleInfo.cs

189 lines
6.0 KiB
C#
Raw Normal View History

2025-01-09 11:31:04 +08:00
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
namespace YooAsset.Editor
{
public class BuildBundleInfo
{
#region
/// <summary>
/// Unity引擎生成的哈希值构建内容的哈希值
/// </summary>
public string PackageUnityHash { set; get; }
/// <summary>
/// Unity引擎生成的CRC
/// </summary>
public uint PackageUnityCRC { set; get; }
/// <summary>
/// 文件哈希值
/// </summary>
public string PackageFileHash { set; get; }
/// <summary>
/// 文件哈希值
/// </summary>
public string PackageFileCRC { set; get; }
/// <summary>
/// 文件哈希值
/// </summary>
public long PackageFileSize { set; get; }
/// <summary>
/// 构建输出的文件路径
/// </summary>
public string BuildOutputFilePath { set; get; }
/// <summary>
/// 补丁包的源文件路径
/// </summary>
public string PackageSourceFilePath { set; get; }
/// <summary>
/// 补丁包的目标文件路径
/// </summary>
public string PackageDestFilePath { set; get; }
/// <summary>
/// 加密生成文件的路径
/// 注意:如果未加密该路径为空
/// </summary>
public string EncryptedFilePath { set; get; }
#endregion
2025-02-28 16:11:01 +08:00
private readonly Dictionary<string, BuildAssetInfo> _packAssetDic = new Dictionary<string, BuildAssetInfo>(100);
2025-01-09 11:31:04 +08:00
/// <summary>
/// 参与构建的资源列表
/// 注意:不包含零依赖资源和冗余资源
/// </summary>
2025-02-28 16:11:01 +08:00
public readonly List<BuildAssetInfo> AllPackAssets = new List<BuildAssetInfo>(100);
2025-01-09 11:31:04 +08:00
/// <summary>
/// 资源包名称
/// </summary>
public string BundleName { private set; get; }
/// <summary>
/// 加密文件
/// </summary>
public bool Encrypted { set; get; }
public BuildBundleInfo(string bundleName)
{
BundleName = bundleName;
}
/// <summary>
/// 添加一个打包资源
/// </summary>
public void PackAsset(BuildAssetInfo buildAsset)
{
string assetPath = buildAsset.AssetInfo.AssetPath;
2025-02-28 16:11:01 +08:00
if (_packAssetDic.ContainsKey(assetPath))
2025-01-09 11:31:04 +08:00
throw new System.Exception($"Should never get here ! Asset is existed : {assetPath}");
2025-02-28 16:11:01 +08:00
_packAssetDic.Add(assetPath, buildAsset);
AllPackAssets.Add(buildAsset);
2025-01-09 11:31:04 +08:00
}
/// <summary>
/// 是否包含指定资源
/// </summary>
2025-02-28 16:11:01 +08:00
public bool IsContainsPackAsset(string assetPath)
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
return _packAssetDic.ContainsKey(assetPath);
2025-01-09 11:31:04 +08:00
}
/// <summary>
/// 获取构建的资源路径列表
/// </summary>
2025-02-28 16:11:01 +08:00
public string[] GetAllPackAssetPaths()
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
return AllPackAssets.Select(t => t.AssetInfo.AssetPath).ToArray();
2025-01-09 11:31:04 +08:00
}
/// <summary>
2025-02-28 16:11:01 +08:00
/// 获取构建的主资源信息
2025-01-09 11:31:04 +08:00
/// </summary>
2025-02-28 16:11:01 +08:00
public BuildAssetInfo GetPackAssetInfo(string assetPath)
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
if (_packAssetDic.TryGetValue(assetPath, out BuildAssetInfo value))
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
return value;
}
else
{
throw new Exception($"Can not found pack asset info {assetPath} in bundle : {BundleName}");
}
}
/// <summary>
/// 获取资源包内部所有资产
/// </summary>
public List<AssetInfo> GetBundleContents()
{
Dictionary<string, AssetInfo> result = new Dictionary<string, AssetInfo>(AllPackAssets.Count);
foreach (var packAsset in AllPackAssets)
{
result.Add(packAsset.AssetInfo.AssetPath, packAsset.AssetInfo);
if (packAsset.AllDependAssetInfos != null)
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
foreach (var dependAssetInfo in packAsset.AllDependAssetInfos)
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
// 注意:依赖资源里只添加零依赖资源和冗余资源
if (dependAssetInfo.HasBundleName() == false)
{
string dependAssetPath = dependAssetInfo.AssetInfo.AssetPath;
if (result.ContainsKey(dependAssetPath) == false)
result.Add(dependAssetPath, dependAssetInfo.AssetInfo);
}
2025-01-09 11:31:04 +08:00
}
}
}
2025-02-28 16:11:01 +08:00
return result.Values.ToList();
2025-01-09 11:31:04 +08:00
}
/// <summary>
/// 创建AssetBundleBuild类
/// </summary>
public UnityEditor.AssetBundleBuild CreatePipelineBuild()
{
// 注意我们不再支持AssetBundle的变种机制
AssetBundleBuild build = new AssetBundleBuild();
build.assetBundleName = BundleName;
build.assetBundleVariant = string.Empty;
2025-02-28 16:11:01 +08:00
build.assetNames = GetAllPackAssetPaths();
2025-01-09 11:31:04 +08:00
return build;
}
/// <summary>
/// 获取所有写入补丁清单的资源
/// </summary>
public BuildAssetInfo[] GetAllManifestAssetInfos()
{
2025-02-28 16:11:01 +08:00
return AllPackAssets.Where(t => t.CollectorType == ECollectorType.MainAssetCollector).ToArray();
2025-01-09 11:31:04 +08:00
}
/// <summary>
/// 创建PackageBundle类
/// </summary>
internal PackageBundle CreatePackageBundle()
{
PackageBundle packageBundle = new PackageBundle();
packageBundle.BundleName = BundleName;
packageBundle.UnityCRC = PackageUnityCRC;
packageBundle.FileHash = PackageFileHash;
packageBundle.FileCRC = PackageFileCRC;
packageBundle.FileSize = PackageFileSize;
packageBundle.Encrypted = Encrypted;
return packageBundle;
}
}
}