#if UNITY_EDITOR // ============================================================================ // WebGLBuildOptimizer.cs — WebGL 构建优化设置 // US5 T048: 配置 IL2CPP 裁剪、压缩等以确保首包 ≤ 4MB // ============================================================================ using UnityEditor; using UnityEngine; namespace ProjSG.Resource.Editor { /// /// WebGL 构建优化配置工具。 /// 通过菜单 "ProjSG/Build/Apply WebGL Optimizations" 应用。 /// public static class WebGLBuildOptimizer { [MenuItem("ProjSG/Build/Apply WebGL Optimizations")] public static void ApplyOptimizations() { // IL2CPP code stripping PlayerSettings.stripEngineCode = true; // Managed stripping level — High for smallest build PlayerSettings.SetManagedStrippingLevel(BuildTargetGroup.WebGL, ManagedStrippingLevel.High); // WebGL compression — Brotli for smallest size PlayerSettings.WebGL.compressionFormat = WebGLCompressionFormat.Brotli; // Decompression fallback — enable for broader server compatibility PlayerSettings.WebGL.decompressionFallback = true; // Data caching — enable for faster subsequent loads PlayerSettings.WebGL.dataCaching = true; // Exception support — minimal for smaller build PlayerSettings.WebGL.exceptionSupport = WebGLExceptionSupport.None; // WebGL template — Minimal PlayerSettings.WebGL.template = "APPLICATION:Minimal"; // Memory size — reasonable default (MB) PlayerSettings.WebGL.memorySize = 256; Debug.Log("[WebGLBuildOptimizer] Applied WebGL build optimizations for ≤ 4MB first package."); } [MenuItem("ProjSG/Build/Validate First Package Size")] public static void ValidateFirstPackageSize() { string buildPath = "Builds/WebGL"; if (!System.IO.Directory.Exists(buildPath)) { Debug.LogWarning($"[WebGLBuildOptimizer] Build directory not found: {buildPath}. Build first, then validate."); return; } long totalBytes = 0; var files = System.IO.Directory.GetFiles(buildPath, "*", System.IO.SearchOption.AllDirectories); foreach (var file in files) { var info = new System.IO.FileInfo(file); totalBytes += info.Length; // Log large files if (info.Length > 500 * 1024) // > 500KB { Debug.Log($" Large file: {info.Name} = {info.Length / 1024f / 1024f:F2} MB"); } } float totalMB = totalBytes / 1024f / 1024f; string status = totalMB <= 4f ? "PASS" : "FAIL"; Debug.Log($"[WebGLBuildOptimizer] First package total: {totalMB:F2} MB — {status} (target ≤ 4MB)"); } } } #endif