Version: 2021.2
WebGL 中的嵌入式资源
WebGL Player 设置

Getting started with WebGL development

This section provides an overview of the platform support and other useful technical information required to build your project using the WebGL option.

Technical Overview

Unity uses the emscripten compiler toolchain to cross-compile the Unity runtime code (written in C and C++) into WebAssembly (also known as Wasm). It is designed to be small in size, load-time and memory efficient as well as able to deliver close to native speed execution. For more information about WebAssembly in Unity, check this blog post.

要将 .NET 游戏代码(C# 脚本)转换为 WebAssembly,我们使用一种称为 IL2CPP 的技术。IL2CPP 接受 .NET 字节码并将其转换为相应的 C++ 源文件,然后使用 emscripten 进行编译以将脚本转换为 Wasm。

Platform support

桌面平台的大多数主要浏览器的当前版本都支持 Unity WebGL 内容,但不同浏览器提供的支持程度存在差异。Unity WebGL 不支持移动设备。

并非所有 Unity 功能都可用于 WebGL 构建,主要是由于平台的限制。具体而言:

  • 由于 JavaScript 中缺少线程支持,因此不支持线程。在 Unity 内部使用线程来加速性能以及在脚本代码和托管的 dll 中使用线程都是不受支持的。实质上不支持 System.Threading 命名空间中的任何内容。

  • 无法在 Visual Studio 中调试 WebGL 构建。请参阅:对 WebGL 构建进行调试和故障排除

  • 出于安全考虑,浏览器不允许直接访问 IP 套接字来进行联网。请参阅:WebGL 网络

  • WebGL 图形 API 等效于 OpenGL ES 2.0 和 3.0,存在一些限制。请参阅:WebGL 图形

  • WebGL 构建使用基于 Web 音频 API 的自定义音频后端。此情况下仅支持基本音频功能。请参阅:使用 WebGL 中的音频

  • WebGL 是一个 AOT 平台,因此不允许使用 System.Reflection.Emit 来动态生成代码。在所有其他 IL2CPP 平台、iOS 和大多数游戏主机上都是如此。

WebGL 中的嵌入式资源
WebGL Player 设置