Hi everyone!
It’s a long time we work with Unity WebGL, I just finished to update our online portfolio to Unity 6 ( https://funix.it/virtualexpo/ ) and STILL there are a lot of console log messages if you open the inspector window.
Is there a new way that I missed to hide those with this new Unity 6 (that, by the way seems actually better performance wise)?
I know it’s not a real problem, but these are the things that bother me… and sometimes they bother my clients, who in turn bother me…
other than telling it to remove the shaders it whines about, not really
Well actually I have very few of those (a couple). I was thinking more about really unuseful stuff like:
[Physics::Module] Initialized fallback backend.
[Physics::Module] Id: 0xdecafbad
Loading player data from data.unity3d
Initialize engine version: 6000.0.27f1 (27c554a2199c)
[Subsystems] Discovering subsystems at path UnitySubsystems
Creating WebGL 2.0 context.
Renderer: WebKit WebGL
Vendor: WebKit
Version: OpenGL ES 3.0 (WebGL 2.0 (OpenGL ES 3.0 Chromium))
I’m using following nodejs script before deploying the build to remove the noise and shader warnings (tested with Unity 6.2):
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const cwd = path.dirname(fileURLToPath(import.meta.url));
// TODO: Remove the prepend when Unity fixes https://issuetracker.unity3d.com/issues/invalid-enum-warning-is-thrown-in-webgl-player-when-building-an-empty-scene
const unityFrameFile = path.resolve(cwd, "/public/game/game.framework.js");
fs.writeFileSync(unityFrameFile, `
(function () {
const getInternalformatParameter = WebGL2RenderingContext.prototype.getInternalformatParameter;
const invalidFormats = new Set([36756, 36757, 36759, 36760, 36761, 36763]);
WebGL2RenderingContext.prototype.getInternalformatParameter = function (target, internalformat, pname) {
if (invalidFormats.has(internalformat)) return null;
return getInternalformatParameter.call(this, target, internalformat, pname);
};
})();
` + fs
.readFileSync(unityFrameFile, "utf8")
.replace(/(\W)console\.(log|warn)\([^)]*\);/g, "$1void 0;"));
const unityLoaderFile = path.resolve(cwd, "../public/game/game.loader.js");
fs.writeFileSync(unityLoaderFile, fs
.readFileSync(unityLoaderFile, "utf8")
.replace(/console\.(log|warn)\([^)]*\)/g, "void 0"));
I can confirm @Elringus’s solution works, here’s another version that Claude helped to improve, if anyone need it too:
// post-unity-build.js
// Script to suppress Unity WebGL warnings by modifying Unity-generated files
// ⚠️ WARNING: This modifies Unity files directly - use at your own risk
// Run this AFTER Unity build, BEFORE deploying
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const cwd = path.dirname(fileURLToPath(import.meta.url));
// Unity build output directory
const unityBuildDir = path.resolve(cwd, "../public/unity/Build");
const unityFrameFile = path.join(unityBuildDir, "Game.framework.js");
const unityLoaderFile = path.join(unityBuildDir, "Game.loader.js");
console.log("🔧 Post-Unity Build: Suppressing WebGL warnings...");
// Check if files exist
if (!fs.existsSync(unityFrameFile)) {
console.error("❌ Error: Game.framework.js not found at:", unityFrameFile);
console.error(" Make sure Unity build completed successfully");
process.exit(1);
}
if (!fs.existsSync(unityLoaderFile)) {
console.error("❌ Error: Game.loader.js not found at:", unityLoaderFile);
console.error(" Make sure Unity build completed successfully");
process.exit(1);
}
try {
// Backup original files
fs.copyFileSync(unityFrameFile, unityFrameFile + ".backup");
fs.copyFileSync(unityLoaderFile, unityLoaderFile + ".backup");
console.log("✅ Created backups (.backup files)");
// Modify Game.framework.js
const frameworkContent = fs.readFileSync(unityFrameFile, "utf8");
// Prepend WebGL fix
const webglFix = `
// ==========================================
// Unity WebGL Warning Suppression
// Automatically injected by post-unity-build.js
// Source: https://discussions.unity.com/t/hide-console-log-in-webgl-builds/1556838
// ==========================================
(function () {
const getInternalformatParameter = WebGL2RenderingContext.prototype.getInternalformatParameter;
const invalidFormats = new Set([36756, 36757, 36759, 36760, 36761, 36763]);
WebGL2RenderingContext.prototype.getInternalformatParameter = function (target, internalformat, pname) {
if (invalidFormats.has(internalformat)) return null;
return getInternalformatParameter.call(this, target, internalformat, pname);
};
})();
`;
// Strip console.log and console.warn (careful - this is fragile)
const modifiedFramework = webglFix + frameworkContent
.replace(/(\W)console\.(log|warn)\([^)]*\);/g, "$1void 0;");
fs.writeFileSync(unityFrameFile, modifiedFramework);
console.log("✅ Modified Game.framework.js");
// Modify Game.loader.js
const loaderContent = fs.readFileSync(unityLoaderFile, "utf8");
const modifiedLoader = loaderContent
.replace(/console\.(log|warn)\([^)]*\)/g, "void 0");
fs.writeFileSync(unityLoaderFile, modifiedLoader);
console.log("✅ Modified Game.loader.js");
console.log("🎉 Unity WebGL warning suppression complete!");
console.log("⚠️ Note: Run this script after EVERY Unity build");
} catch (error) {
console.error("❌ Error modifying Unity files:", error);
// Attempt to restore backups
try {
if (fs.existsSync(unityFrameFile + ".backup")) {
fs.copyFileSync(unityFrameFile + ".backup", unityFrameFile);
}
if (fs.existsSync(unityLoaderFile + ".backup")) {
fs.copyFileSync(unityLoaderFile + ".backup", unityLoaderFile);
}
console.log("✅ Restored backups");
} catch (restoreError) {
console.error("❌ Failed to restore backups:", restoreError);
}
process.exit(1);
}
Feature Our Approach Their Script
Effectiveness ✅ Same (uses same codes) ✅ Same
Safety ✅ Non-invasive ⚠️ Modifies Unity files
Risk ✅ Low ⚠️ Medium (file modification)