WebGL Builds with Unity 3D

I recently updated Unity to the latest 2018.2.0 build and I immediately ran into some frustrating issues. My Unity projects were previously on version 5.3.8 and therefore needed to be upgraded.

The first hurdle was opening the newly 'upgraded' solution files in Visual Studio 2017 for some reason Unity has added a bunch of extra projects to the solution:

    Unity.PackageManagerUI.Editor
    Unity.TextMeshPro
    Unity.TextMeshPro.Editor
    Unity.Editor.StandardEvents

Trying to build would then result in build errors such as:

Error CS2001 Source file '..\Packages\com.unity.package-manager-ui\Editor\AssemblyInfo.cs' could not be found.

Removing the erroneous projects from the solution seems to resolve the issue, and it would once again build in Visual Studio correctly.

Once I was happy my code compiled without error I tried to do a WebGL build in Unity which promptly crashed my computer or rather used up all the CPU (I have fairly decent quad core Xeon 3.3GHz processor running 8 threads so this generally does not happen unless I am doing something daft)

After some digging the first place to check is the Editor Logs which are located in C:\Users\{Username}\AppData\Local\Unity\Editor, here I found a rather vague error stating simply that the player failed to build:

Checking online this appear to be an old issue with no specific cause that requires a modification to the emscripten.config which will usually be located here:

C:\Program Files\Unity\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\emscripten.config

Changing the following line stack_size to 2048 seems to do the trick

NODE_JS=[os.getenv('NODE'),'--stack_size=2048','--max-old-space-size=4096']

A few other posts suggested clearing out the contents of the Library folder in the project and restarting the machine to be on the safe side.

After making the changes building does not quite crash the machine but does take a long time and renders my machine unusable for a good 10 minutes.

It seems that the first build will generally be painful but subsequent builds are a little better. This is apparently a known issue and hopefully they are working on a fix but until then be aware that WebGL builds can be very slow.

I have tried some other suggestions such as disabling Anti Virus scans of the build directory and moving to a Solid State Drive but these do not seem to drastically improve things for me.

Inspecting the final result I now want to test it. Unfortunately you cannot just run the index.html file from the files system so it must be run on local IIS. Next issue being that IIS will need to know about all the static files for the Unity build. You will likely get something along the lines of this error:

Uncaught SyntaxError: Unexpected token < in JSON at position 0

The work around is fairly simple however just add a web.config file with the following content into the directory and everything should work as expected:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<remove fileExtension=".mem" />
<remove fileExtension=".data" />
<remove fileExtension=".unity3d" />
<remove fileExtension=".jsbr" />
<remove fileExtension=".membr" />
<remove fileExtension=".databr" />
<remove fileExtension=".unity3dbr" />
<remove fileExtension=".jsgz" />
<remove fileExtension=".memgz" />
<remove fileExtension=".datagz" />
<remove fileExtension=".unity3dgz" />
<remove fileExtension=".json" />
<remove fileExtension=".unityweb" />

<mimeMap fileExtension=".mem" mimeType="application/octet-stream" />
<mimeMap fileExtension=".data" mimeType="application/octet-stream" />
<mimeMap fileExtension=".unity3d" mimeType="application/octet-stream" />
<mimeMap fileExtension=".jsbr" mimeType="application/octet-stream" />
<mimeMap fileExtension=".membr" mimeType="application/octet-stream" />
<mimeMap fileExtension=".databr" mimeType="application/octet-stream" />
<mimeMap fileExtension=".unity3dbr" mimeType="application/octet-stream" />
<mimeMap fileExtension=".jsgz" mimeType="application/x-javascript; charset=UTF-8" />
<mimeMap fileExtension=".memgz" mimeType="application/octet-stream" />
<mimeMap fileExtension=".datagz" mimeType="application/octet-stream" />
<mimeMap fileExtension=".unity3dgz" mimeType="application/octet-stream" />
<mimeMap fileExtension=".json" mimeType="application/json; charset=UTF-8" />
<mimeMap fileExtension=".unityweb" mimeType="application/octet-stream" />
</staticContent>
</system.webServer>
</configuration>

Once the web.config file is added the game can be tested in the browser locally.