#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 { /// 检查实际构建产物,防止 SBP 的旧 Prefab 依赖缓存重新带入原图像素。 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("BuildPackageName"); bool uniqueNames = report["Summary"].Value("UniqueBundleName"); var bundles = ((JArray)report["BundleInfos"]).ToDictionary(b => b.Value("BundleName")); var checks = new List(); foreach (string guid in AssetDatabase.FindAssets("l:" + UiSpriteAtlasGenerator.GeneratedLabel, new[] { UiAtlasPaths.AtlasRoot })) { string atlasPath = AssetDatabase.GUIDToAssetPath(guid); if (AssetDatabase.LoadAssetAtPath(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("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().Length }); } finally { bundle.Unload(true); } } if (checks.Count == 0) throw new InvalidOperationException("No generated UI atlas bundles validated."); return checks.ToArray(); } } } #endif