name: unity-webgl-pruning description: Optimizes and prunes Unity WebGL exports to meet strict playable ad file size limits. Removes unused modules and enforces compression.
Unity WebGL Pruning
This skill reduces the footprint of Unity WebGL builds for playable ads, ensuring compliance with strict ad network file size limits (often <3MB or <5MB) and Green Software Engineering principles.
Core Rules
- Module Stripping: Strip unused Unity engine code (e.g., Physics2D/3D if not used, UI modules) via Unity Player Settings (Strip Engine Code).
- Asset Compression: Enforce Crunch compression or ASTC for textures, and force Mono over IL2CPP if it yields a smaller uncompressed size (or vice versa depending on the Unity version).
- Cross-Platform Tooling: If using post-build scripts to remove
.bror.gzfiles or inline the WebGL loader, use OS-agnostic tools (e.g., Node.jsfsmodule) rather than Unix shell scripts. - Green Software Engineering: Optimize wire payloads. Smaller builds reduce energy consumption over millions of ad impressions.
Reference Example
An OS-agnostic Node.js script to inline the Unity framework .js into the index.html:
const fs = require('fs');
const path = require('path');
const buildDir = path.join(__dirname, 'Build');
const indexHtmlPath = path.join(__dirname, 'index.html');
let htmlContent = fs.readFileSync(indexHtmlPath, 'utf8');
const frameworkPath = path.join(buildDir, 'game.framework.js');
if (fs.existsSync(frameworkPath)) {
const frameworkCode = fs.readFileSync(frameworkPath, 'utf8');
htmlContent = htmlContent.replace('<script src="Build/game.framework.js"></script>', `<script>${frameworkCode}</script>`);
fs.writeFileSync(indexHtmlPath, htmlContent);
}