Notes on the Unity WebGL Build Tool

Overview

There are some things to take note of when using the Unity WebGL build tool, as will be documented in this section.

When selecting the template for the game that is being built, the templateName variable found in TRWebBuild.cs does not use template tags, and therefore needs to be updated manually.

    static string GetTemplateName () {

        const string templateName = "NAME";
        //Change NAME to appropriate template name, e.g. Meteors.
    
        return templateName;
    }

The following function removes the annoying compatibility warning popup that appears when the user attempts to run a webGL build on a mobile device.

It works by:

  • Finding the JS file that contains the mobile warning code
  • Reading the file into a string
  • Deleting the existing file
  • Overwriting the warning text
  • Writing the new file to the destination of the old file

Once the new file has been written to the destination of the old file, the warning popup will no longer appear.

A drawback to this method is that, should Unity change their warning text, it will need to be manually replaced in this function, even if the change is as minor as capitalisation or punctuation.

  public static void RemoveMobileWarning() {

        string unityLoaderPath = "";


        DirectoryInfo info = new DirectoryInfo(GetBuildPath() + "/Build/");

        FileInfo[] files = info.GetFiles();

        // Find the .js file which contains the mobile warning code.

        for (int i=0; i < files.Length; i ++) {            

            if (files[i].Extension == ".js") {

                unityLoaderPath = files[i].FullName;                

            }

        }


        StreamReader sr = new StreamReader(unityLoaderPath);

        string unityloaderFile = sr.ReadToEnd();

        sr.Close();


        FileUtil.DeleteFileOrDirectory(unityLoaderPath);


        unityloaderFile = unityloaderFile.Replace(@"UnityLoader.SystemInfo.hasWebGL?UnityLoader.SystemInfo.mobile?e.popup(""Please note that Unity WebGL is not currently supported on mobiles. Press OK if you wish to continue anyway."",[{text:""OK"",callback:t}]):[""Edge"",""Firefox"",""Chrome"",""Safari""].indexOf(UnityLoader.SystemInfo.browser)==-1?e.popup(""Please note that your browser is not currently supported for this Unity WebGL content. Press OK if you wish to continue anyway."",[{text:""OK"",callback:t}]):t():e.popup(""Your browser does not support WebGL"",[{text:""OK"",callback:r}])", "t();");


        StreamWriter sw = new StreamWriter(unityLoaderPath);

        sw.Write(unityloaderFile);

        sw.Close();

    }