Files
AIGame/AIClient/Assets/Editor/UI/UiAtlasBuildValidation.cs
T

60 lines
2.7 KiB
C#

#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using HotUpdate.UI;
using Newtonsoft.Json.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.U2D;
using YooAsset.Editor;
namespace AIGame.EditorTools
{
/// <summary>检查实际构建产物,防止 SBP 的旧 Prefab 依赖缓存重新带入原图像素。</summary>
public static class UiAtlasBuildValidation
{
public sealed class BundleCheck
{
public string FileName;
public long Bytes;
public int SourceTextureCount;
}
public static BundleCheck[] Inspect(string packageDirectory)
{
string reportPath = Directory.GetFiles(packageDirectory, "*.report").Single();
var report = JObject.Parse(File.ReadAllText(reportPath));
string packageName = report["Summary"].Value<string>("BuildPackageName");
bool uniqueNames = report["Summary"].Value<bool>("UniqueBundleName");
var bundles = ((JArray)report["BundleInfos"]).ToDictionary(b => b.Value<string>("BundleName"));
var checks = new List<BundleCheck>();
foreach (string guid in AssetDatabase.FindAssets("l:" + UiSpriteAtlasGenerator.GeneratedLabel,
new[] { UiAtlasPaths.AtlasRoot }))
{
string atlasPath = AssetDatabase.GUIDToAssetPath(guid);
if (AssetDatabase.LoadAssetAtPath<SpriteAtlas>(atlasPath) == null) continue;
string bundleName = new PackUiAtlas().GetPackRuleResult(
new BundlePackRuleData(atlasPath, UiAtlasPaths.AtlasRoot, "UI", "")).GetBundleName(packageName, uniqueNames);
if (!bundles.TryGetValue(bundleName, out var info))
throw new InvalidOperationException("Generated UI atlas missing from build report: " + atlasPath);
string fileName = info.Value<string>("FileName");
string path = Path.Combine(packageDirectory, fileName);
var bundle = AssetBundle.LoadFromFile(path);
if (bundle == null) throw new IOException("Cannot validate UI atlas bundle: " + path);
try
{
// 已剥离的源图仍有 Sprite 元数据,但不应再有可加载的原图 Texture2D。
checks.Add(new BundleCheck { FileName = fileName, Bytes = new FileInfo(path).Length,
SourceTextureCount = bundle.LoadAllAssets<Texture2D>().Length });
}
finally { bundle.Unload(true); }
}
if (checks.Count == 0) throw new InvalidOperationException("No generated UI atlas bundles validated.");
return checks.ToArray();
}
}
}
#endif