包含此页的版本:
不含此页的版本:
可以使用代码来启用优化 Web 生成中建议的一些优化。如果使用代码来配置这些设置,则可以节省手动单独设置每个设置的时间。
注意:此脚本仅适用于 Unity 编辑器,不适用于构建。
使用代码在 Unity 中一次启用大多数优化项目设置广泛的设置集合,允许您配置物理、音频、网络、图形、输入和项目的许多其他区域的行为方式。更多信息
请参阅术语表:
创建一个Assets/Editor文件夹(如果您还没有)。
在Editor文件夹。
将以下代码粘贴到脚本中:
using UnityEditor;
using UnityEditor.Build;
using UnityEngine;
public class WebOptimizer
{
[MenuItem("Example/Optimize")]
public static void Optimize()
{
var namedBuildTarget = NamedBuildTarget.WebGL;
// Set IL2CPP code generation to Optimize Size
PlayerSettings.SetIl2CppCodeGeneration(namedBuildTarget,
Il2CppCodeGeneration.OptimizeSize);
// Set the Managed Stripping Level to High
PlayerSettings.SetManagedStrippingLevel(namedBuildTarget,
ManagedStrippingLevel.High);
// Strip unused mesh components
PlayerSettings.stripUnusedMeshComponents = true;
// Enable data caching
PlayerSettings.WebGL.dataCaching = true;
// Set the compression format to Brotli
PlayerSettings.WebGL.compressionFormat = WebGLCompressionFormat.Brotli;
// Deactivate exceptions
PlayerSettings.WebGL.exceptionSupport = WebGLExceptionSupport.None;
// Deactivate debug symbols
PlayerSettings.WebGL.debugSymbolMode = WebGLDebugSymbolMode.Off;
//Enable WebAssembly 2023 features
PlayerSettings.WebGL.wasm2023 = true;
// Set Platform Settings to optimize for disk size (LTO)
UnityEditor.WebGL.UserBuildSettings.codeOptimization = UnityEditor.WebGL.WasmCodeOptimization.DiskSizeLTO;
}
}
更改脚本以适合您的项目。
从工具栏中,选择示例>优化以运行脚本。您的设置更新。
要更改资产导入覆盖的设置,请参阅构建配置文件参考。