Version: 2023.2
Language : English
Recommended Quality settings to optimize your Web build
Optimize Web platform for mobile

Use C# code to enable optimization settings

You can use code to enable some of the optimizations recommended in Optimize your Web build. If you use code to configure these settings it can save you time having to manually set each of them individually.

Note: This script only works in Editor, not in builds.

Create a C# script to optimize your Web build

To use code to enable most of these optimizations at once in your Unity project settingsA broad collection of settings which allow you to configure how Physics, Audio, Networking, Graphics, Input and many other areas of your project behave. More info
See in Glossary
:

  1. Go to Assets > Create > C# Script.
  2. Paste the following code into the script:
    var namedBuildTarget = NamedBuildTarget.WebGL;
    var buildOptions = BuildOptions.CompressWithLz4HC;
    // 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;        
    // Set Platform Settings to optimize for disk size (LTO)
    EditorUserBuildSettings.SetPlatformSettings(namedBuildTarget.TargetName, 
                                                "CodeOptimization",  
                                                "disksizelto"); 
    
  3. Change the script to suit your project.
  4. Attach your script to a GameObject.
  5. Enter Play mode. Your various settings are updated.

Additional resources

Recommended Quality settings to optimize your Web build
Optimize Web platform for mobile