Getting started with WebGL development
WebGL开发入门
What is Unity WebGL?
什么是Unity WebGL?
The WebGL build option allows Unity to publish content as JavaScript programs which use HTML5 technologies and the WebGL rendering API to run Unity content in a web browser. To build and test your content for WebGL, choose the WebGL build target in the Build Player window, and click Build & Run.
可以使用Unity的WebGL构建选项将内容以JavaScript程序发布,这当中使用了HTML5技术和WebGL渲染接口API以在浏览器中运行Unity内容。在Build Player窗口中选择WebGL构建目标,然后点击Build & Run,就能以WebGL形式构建并测试你的内容。
Technical overview
技术概览
To run in WebGL, all 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 efficient native code.
要在WebGL中运行,所有代码必须是JavaScript。我们使用emscripten编译工具链将Unity运行时代码(用C和C++编写)跨平台编译成asm.js JavaScript。asm.js是JavaScript的一个很可优化的子集,允许JavaScipt引擎将asm.js代码AOT编译成非常高效的原生代码。
To convert the .NET game code (your C# and UnityScript scripts) into JavaScript, we use a technology called IL2CPP. IL2CPP takes .NET bytecode and converts it to corresponding C++ source files, which is then compiled using emscripten to convert your scripts to JavaScript.
为将.NET游戏代码(你的C#和UnityScript脚本)转换成JavaScript,我们使用了称为IL2CPP的技术。IL2CPP提取.NET字节码并将其转换成相对应的C++源文件,接着使用emscripten技术编译这些文件,将你的脚本转换成JavaScript。
Platform support
平台支持
Unity WebGL content is supported in the current versions of most major browsers on the desktop, however there are differences in the level of support offered by the different browsers. Mobile devices are not supported by Unity WebGL
大多数主流浏览器的当前版本都支持Unity WebGL内容,然而不同浏览器的支持程度不一样。Unity WebGL不支持移动设备。
Not all features of Unity are available in WebGL builds, mostly due to constraints of the platform. Specifically:
WebGL构建里并不包含Unity的所有内容,这主要因为平台的限制。具体来说是以下几点:
· Threads are not supported due to the lack of threading supporting in JavaScript. This applies to both Unity’s internal use of threads to speed up performance, and to the use of threads in script code and managed dlls. Essentially, anything in the System.Threading
namespace is not supported.
· 构建的WebGL内容不支持多线程,这是由于JavaScript本身就缺乏多线程的支持。其中包括Unity内部为提高性能所用的多线程,以及脚本代码和托管dll里使用的多线程。基本上,System.Threading
命名空间里的所有内容都不被支持。
· WebGL builds cannot be debugged in MonoDevelop or Visual Studio. See: Debugging and trouble shooting WebGL builds.
在MonoDevelop或Visual Studio中WebGL构建不能被调试。详见:WebGL构建调试及疑难解答。
· Browsers do not allow direct access to IP sockets for networking, due to security concerns. See: WebGL Networking.
出于安全起见,浏览器不允许在网络编程里直接访问IP套接字。详见:WebGL网络编程。
· The WebGL graphics API is equivalent to OpenGL ES 2.0, which has some limitations. See: WebGL Graphics.
WebGL图形接口API相当于OpenGL ES2.0,其有很多限制。详见:WebGL图形技术。
· WebGL builds use a custom backend for Audio, based on the Web Audio API. This supports only basic audio functionality. See: Using Audio in WebGL.
基于Web Audio接口API ,WebGL构建为音频使用自定义的后台技术,其只支持基本的音频特性。详见:在WebGL中使用音频。
· WebGL is an AOT platform, so it does not allow dynamic generation of code using System.Reflection.Emit
. This is the same on all other IL2CPP platforms, iOS, and most consoles.
WebGL是一个AOT平台,因此它不支持使用System.Reflection.Emit
动态创建代码。在所有其他IL2CPP平台、iOS和大多数控制台上也是不支持的。