Skip to content

Instantly share code, notes, and snippets.

@mminer
Created July 23, 2025 04:07
Show Gist options
  • Save mminer/c8709e4bb0c96ae19482704b780e9971 to your computer and use it in GitHub Desktop.
Save mminer/c8709e4bb0c96ae19482704b780e9971 to your computer and use it in GitHub Desktop.
Unity build preprocessing script to enable Disk Size with LTO code optimization for WebGL builds.
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
public class PreprocessBuild : IPreprocessBuildWithReport
{
public int callbackOrder { get; }
public void OnPreprocessBuild(BuildReport report)
{
Debug.Log("Preprocessing build.");
#if UNITY_WEBGL
// We only want to change user build settings for automated builds, not for local development.
// If we didn't check for batch mode, this would override the developer's preferred code optimization setting
// (which is probably "Shorter Build Time").
if (Application.isBatchMode)
{
Debug.Log("Setting WebGL code optimization to Disk Size with LTO.");
UnityEditor.WebGL.UserBuildSettings.codeOptimization = UnityEditor.WebGL.WasmCodeOptimization.DiskSizeLTO;
}
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment