A few weeks ago at GDC, we announced support for WebGL publishing for Unity 5. Now I’d like to share some more information on what this is all about, and what you can expect from it.

Some background

WebGL is a 3d graphics library built into the browser which allows JavaScript programs to do 3d rendering inside any supported browser without requiring any plug-ins. To us, this always seemed like a perfect fit for running Unity content on the web, as it would give end users the most barrier-free experience – as the browser would supply everything needed out of the box, and everything would just work without the user bothering with installing any plug-ins.

However, we initially had some doubts on whether this would be technically achievable, as WebGL is a JavaScript API – which means that all our code (both our Unity runtime and your game code) needs to run in JavaScript somehow. But at the same time, we thought that this technology was too cool not to try it anyways, so we started experimenting with it at a HackWeek in Copenhagen two years ago. Also we had been talking to Mozilla around that time, who have been very eager to help us and to proof to us that this can indeed be done – so they had some engineers come over to Copenhagen to join the fun.

It took us a few more HackWeeks of tinkering around and some developments on the browser side as well, until we reached a point where we realized that we could make a real viable product out of this – which is when we started going into real production.

To give you an idea of what is possible right now, here is a Unity player exported to WebGL with a current alpha version of Unity 5

Currently supported browsers for this content are Firefox and Chrome 35 (Chrome 35 is currently in beta, and is needed, as the current Chrome 34 release version has a JavaScript bug which is causing this game to hang).

Click the icon below to play Dead Trigger 2 by Madfinger games in your browser, demonstrating an immersive fullscreen FPS experience in WebGL. Controls are WASD to walk, mouse to look, Q to switch weapons, Tab to switch to Melee combat, and 1, 2, and 3 for special powers (try them!).

And here is a build of our classic AngryBots demo (which runs fine on Firefox and the release version of Chrome):

Technical details

As mentioned above, to run in WebGL, all our code needs to be JavaScript. We use the emscripten compiler toolchain to cross-compile the Unity runtime code (written in C and C++) into asm.js JavaScript. asm.js is a very optimizable subset of JavaScript which allows JavaScript engines to AOT-compile asm.js code into very performant native code (see here for a better explanation).

To convert the .NET game code (your C# and UnityScript scripts) into JavaScript, we developed a new technology in-house which we call IL2CPP. IL2CPP takes .NET bytecode and converts it to corresponding C++ source files, which we can then compile using any C++ compiler — such as emscripten to get your scripts converted to JavaScript. Expect more information on IL2CPP soon.

WebGL in Unity 5.0

We plan to WebGL support available in Unity 5.0 as an early-access add-on (before you ask: the terms and prices of this add-on have not been decided on yet). Early-Access means that it will be capable of publishing content to WebGL (like the examples above), but it will have some limitations in features and in browser compatibility. In particular, the following features will not be supported:

Runtime generation of Substance textures

MovieTextures

Networking other then WWW class (a WebSockets plug-in is available)

Support for WebCam and Microphone access

Hardware cursor support

Most of the non-basic audio features

Script debugging

Threads

Any .NET features requiring dynamic code generation

In terms of browser support, this initial version will only support the desktop versions of Firefox and Chrome (other browsers might work for some content, but only these two will be officially supported).

We expect to resolve most of those limitations (except for things which are restrictions imposed by the platform) during the 5.x release cycle, and to be able to support a wider range of browsers as well as the platform matures – at which point we will drop the early-access label and make WebGL a fully supported build platform in Unity.

The Unity Web Player in Unity 5

While WebGL is a very exciting new technology, currently, the Unity Web Player is still the most feature-complete and the most performant solution for targeting the web with Unity, and will stay as a supported platform in Unity 5.x. It may be a very useful strategy to dual-publish your content using both WebGL and the Web Player, in order to get the widest possible reach for your audience.

Longer term, however, we expect that the performance and feature gap between the Web Player and WebGL will become much more narrow, and we expect that browser vendors will make the Web Player obsolete by dropping support for plug-ins, at which point WebGL will become the prime solution for targeting the web with Unity.