Search Unity

Disable console outputs on WebGL release build

Discussion in 'Web' started by s1gh7, Jul 15, 2022.

  1. s1gh7

    s1gh7

    Joined:
    Oct 13, 2016
    Posts:
    12
    Hi everybody,

    I need to disable the complete console outputs in my WebGL release build. How do I get rid of the startup outputs?

    I'm using Unity 2020.3 and for the WebGL build there is no "Use Player Log" like in the Standalone settings.

    Thank's for your help!


    Code (JavaScript):
    1. [Log] Loading player data from data.unity3d (MyGame_WebGL.framework.js, line 2)
    2.  
    3. [Log] Initialize engine version: 2020.3.35f1 (18e4db7a9996) (MyGame_WebGL.framework.js, line 2)
    4.  
    5. [Log] Creating WebGL 2.0 context. (MyGame_WebGL.loader.js, line 1)
    6. [Log] Renderer: WebKit WebGL (MyGame_WebGL.framework.js, line 2)
    7.  
    8. [Log] Vendor:   WebKit (MyGame_WebGL.framework.js, line 2)
    9.  
    10. [Log] Version:  OpenGL ES 3.0 (WebGL 2.0) (MyGame_WebGL.framework.js, line 2)
    11.  
    12. [Log] GLES:     3 (MyGame_WebGL.framework.js, line 2)
    13.  
    14. [Log]  OES_texture_float_linear GL_OES_texture_float_linear EXT_texture_compression_rgtc GL_EXT_texture_compression_rgtc EXT_texture_filter_anisotropic GL_EXT_texture_filter_anisotropic WEBGL_compressed_texture_s3tc GL_WEBGL_compressed_texture_s3tc WEBGL_compressed_texture_s3tc_srgb GL_WEBGL_compressed_texture_s3tc_srgb WEBGL_lose_context GL_WEBGL_lose_context WEBGL_debug_shaders GL_WEBGL_debug_shaders WEBGL_debug_renderer_info GL_WEBGL_debug_renderer_info EXT_color_buffer_float GL_EXT_color_buffer_float EXT_color_buffer_half_float GL_EXT_color_buffer_half_float EXT_float_blend GL_EXT_float_blend KHR_parallel_shader_compile GL_KHR_parallel_shader_compile WEBGL_multi_draw GL_WEBGL_multi_draw (MyGame_WebGL.framework.js, line 2)
    15.  
    16. [Log] OPENGL LOG: Creating OpenGL ES 3.0 graphics device ; Context level  <OpenGL ES 3.0> ; Context handle 25331144 (MyGame_WebGL.framework.js, line 2)
    17.  
    18. [Log] Input Manager initialize... (MyGame_WebGL.framework.js, line 2)
    19.  
    20. [Log] UnloadTime: 2.000000 ms (MyGame_WebGL.framework.js, line 2)
    21.  
     
  2. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    288
    You can create a WebGLTemplate with a script that replaces the console and build it with the created template so that it will not be output.

    Code (CSharp):
    1. <!DOCTYPE html>
    2. <html lang="en-us">
    3.   <head>
    4.     <meta charset="utf-8">
    5.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    6.     <title>Unity WebGL Player | {{{ PRODUCT_NAME }}}</title>
    7.     <link rel="shortcut icon" href="TemplateData/favicon.ico">
    8.     <link rel="stylesheet" href="TemplateData/style.css">
    9.     <script>
    10.       var f = () =>{};
    11.       var c = {};
    12.       Object.keys(console).forEach(x => {
    13.         if(typeof(console[x]) === 'function'){
    14.           c[x] = f;
    15.         }
    16.       });
    17.       var orgConsole = console;
    18.       window.orgConsole = orgConsole;
    19.       var console = c;
    20.       window.console = console;
    21.     </script>
    22.   </head>
    23.   <body>
    24.     <div id="unity-container" class="unity-desktop">
    25.       <canvas id="unity-canvas" width={{{ WIDTH }}} height={{{ HEIGHT }}}></canvas>
    26.       <div id="unity-loading-bar">
    27.         <div id="unity-logo"></div>
    28.         <div id="unity-progress-bar-empty">
    29.           <div id="unity-progress-bar-full"></div>
    30.         </div>
    31.       </div>
    32.       <div id="unity-warning"> </div>
    33.       <div id="unity-footer">
    34.         <div id="unity-webgl-logo"></div>
    35.         <div id="unity-fullscreen-button"></div>
    36.         <div id="unity-build-title">{{{ PRODUCT_NAME }}}</div>
    37.       </div>
    38.     </div>
    39.     <script>
    40.       var container = document.querySelector("#unity-container");
    41.       var canvas = document.querySelector("#unity-canvas");
    42.       var loadingBar = document.querySelector("#unity-loading-bar");
    43.       var progressBarFull = document.querySelector("#unity-progress-bar-full");
    44.       var fullscreenButton = document.querySelector("#unity-fullscreen-button");
    45.       var warningBanner = document.querySelector("#unity-warning");
    46.  
    47.       // Shows a temporary message banner/ribbon for a few seconds, or
    48.       // a permanent error message on top of the canvas if type=='error'.
    49.       // If type=='warning', a yellow highlight color is used.
    50.       // Modify or remove this function to customize the visually presented
    51.       // way that non-critical warnings and error messages are presented to the
    52.       // user.
    53.       function unityShowBanner(msg, type) {
    54.         function updateBannerVisibility() {
    55.           warningBanner.style.display = warningBanner.children.length ? 'block' : 'none';
    56.         }
    57.         var div = document.createElement('div');
    58.         div.innerHTML = msg;
    59.         warningBanner.appendChild(div);
    60.         if (type == 'error') div.style = 'background: red; padding: 10px;';
    61.         else {
    62.           if (type == 'warning') div.style = 'background: yellow; padding: 10px;';
    63.           setTimeout(function() {
    64.             warningBanner.removeChild(div);
    65.             updateBannerVisibility();
    66.           }, 5000);
    67.         }
    68.         updateBannerVisibility();
    69.       }
    70.  
    71.       var buildUrl = "Build";
    72.       var loaderUrl = buildUrl + "/{{{ LOADER_FILENAME }}}";
    73.       var config = {
    74.         dataUrl: buildUrl + "/{{{ DATA_FILENAME }}}",
    75.         frameworkUrl: buildUrl + "/{{{ FRAMEWORK_FILENAME }}}",
    76.         codeUrl: buildUrl + "/{{{ CODE_FILENAME }}}",
    77. #if MEMORY_FILENAME
    78.         memoryUrl: buildUrl + "/{{{ MEMORY_FILENAME }}}",
    79. #endif
    80. #if SYMBOLS_FILENAME
    81.         symbolsUrl: buildUrl + "/{{{ SYMBOLS_FILENAME }}}",
    82. #endif
    83.         streamingAssetsUrl: "StreamingAssets",
    84.         companyName: "{{{ COMPANY_NAME }}}",
    85.         productName: "{{{ PRODUCT_NAME }}}",
    86.         productVersion: "{{{ PRODUCT_VERSION }}}",
    87.         showBanner: unityShowBanner,
    88.       };
    89.  
    90.       // By default Unity keeps WebGL canvas render target size matched with
    91.       // the DOM size of the canvas element (scaled by window.devicePixelRatio)
    92.       // Set this to false if you want to decouple this synchronization from
    93.       // happening inside the engine, and you would instead like to size up
    94.       // the canvas DOM size and WebGL render target sizes yourself.
    95.       // config.matchWebGLToCanvasSize = false;
    96.  
    97.       if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
    98.         // Mobile device style: fill the whole browser client area with the game canvas:
    99.  
    100.         var meta = document.createElement('meta');
    101.         meta.name = 'viewport';
    102.         meta.content = 'width=device-width, height=device-height, initial-scale=1.0, user-scalable=no, shrink-to-fit=yes';
    103.         document.getElementsByTagName('head')[0].appendChild(meta);
    104.         container.className = "unity-mobile";
    105.  
    106.         // To lower canvas resolution on mobile devices to gain some
    107.         // performance, uncomment the following line:
    108.         // config.devicePixelRatio = 1;
    109.  
    110.         canvas.style.width = window.innerWidth + 'px';
    111.         canvas.style.height = window.innerHeight + 'px';
    112.  
    113.         unityShowBanner('WebGL builds are not supported on mobile devices.');
    114.       } else {
    115.         // Desktop style: Render the game canvas in a window that can be maximized to fullscreen:
    116.  
    117.         canvas.style.width = "{{{ WIDTH }}}px";
    118.         canvas.style.height = "{{{ HEIGHT }}}px";
    119.       }
    120.  
    121. #if BACKGROUND_FILENAME
    122.       canvas.style.background = "url('" + buildUrl + "/{{{ BACKGROUND_FILENAME.replace(/'/g, '%27') }}}') center / cover";
    123. #endif
    124.       loadingBar.style.display = "block";
    125.  
    126.       var script = document.createElement("script");
    127.       script.src = loaderUrl;
    128.       script.onload = () => {
    129.         createUnityInstance(canvas, config, (progress) => {
    130.           progressBarFull.style.width = 100 * progress + "%";
    131.         }).then((unityInstance) => {
    132.           loadingBar.style.display = "none";
    133.           fullscreenButton.onclick = () => {
    134.             unityInstance.SetFullscreen(1);
    135.           };
    136.         }).catch((message) => {
    137.           alert(message);
    138.         });
    139.       };
    140.       document.body.appendChild(script);
    141.     </script>
    142.   </body>
    143. </html>
    144.  
     
    Harpaceas likes this.
  3. unity_4FE6E071BA545750D7C0

    unity_4FE6E071BA545750D7C0

    Joined:
    Apr 10, 2023
    Posts:
    2
    WEBGL Without Custom Template: After building, go to "build" folder. Open "index.html," find </body> and after it Paste: <script> console.log = function() {}; console.warn = function() {}; console.error = function() {}; </script> Save. Refresh already opened page (localhost which had opened after the build was done) in browser ; console is disabled. WEBGL Using Custom Template: Open "index.html" inside webgl template folder that you are using. Find </body>, paste script. Save. Build the application; console is disabled.
     
    D-Organiq and MousePods like this.