yyl
2026-02-11 3f2cd27c5dfb3b450245bf1a37fc1b3414031c7c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#if UNITY_EDITOR
// ============================================================================
// WebGLBuildOptimizer.cs — WebGL 构建优化设置
// US5 T048: 配置 IL2CPP 裁剪、压缩等以确保首包 ≤ 4MB
// ============================================================================
 
using UnityEditor;
using UnityEngine;
 
namespace ProjSG.Resource.Editor
{
    /// <summary>
    /// WebGL 构建优化配置工具。
    /// 通过菜单 "ProjSG/Build/Apply WebGL Optimizations" 应用。
    /// </summary>
    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