WebGL 是一种用于在 Web 浏览器中渲染图形的 API,基于 OpenGL ES 图形库的功能。WebGL 1.0 大致与 OpenGL ES 2.0 功能相匹配,而 WebGL 2.0 大致与 OpenGL ES 3.0 功能相匹配。
By default, Unity Web clears the drawing buffer after each frame, which means the content of the frame buffer clears regardless of the Camera.clearFlags setting. However, you can change this behavior at instantiation time. To do this, set webglContextAttributes.preserveDrawingBuffer
to true
in your Web template:
script.onload = () => {
config['webglContextAttributes'] = {"preserveDrawingBuffer": true}; // Add this line to the index.html file in a Web Template
createUnityInstance(canvas, config, (progress) => }
Unity Web only supports Deferred Rendering Path if WebGL2.0 is available. On WebGL 1.0, Unity Web runtime falls back to Forward Rendering.
Unity Web only supports baked GI. Realtime Global Illumination isn’t currently supported in Web. In addition, Unity Web supports Non-Directional lightmaps only.
Unity Web only supports linear color space rendering with WebGL 2.0. Linear color space rendering doesn’t have fallback support for WebGL 1.0. To build a Web player using linear color space rendering, you need to remove WebGL 1.0 API in the Player settings, open the Other Settings panel, disable the Automatic Graphics API setting.
某些 Web 浏览器不支持 sRGB DXT 纹理压缩。因此在使用线性渲染时会降低渲染性能的质量,原因是所有 DXT 纹理都要在运行时解压缩。
You can’t use VideoClipImporter
to import video clips to your Unity project, because it might increase the initial asset data download size and prevent network streaming. For video playback, use the URL option in the VideoPlayer component and place the asset in the StreamingAssets/ directory to use the built-in network streaming of your browser.
The WebGL 1.0 specification imposes some limitations on GLSLS shader code, which are more restrictive than most OpenGL ES 2.0 implementations. This is mostly relevant when you write your own shaders.
WebGL has specific restrictions on which values to use to index arrays or matrices. For example, WebGL only allows dynamic indexing with constant expressions, loop indices or a combination, except for uniform access in vertex shaders, which you can index using any expression.
Note for WebGL 1.0: Additional restrictions apply on control structures in WebGL 1.0, where it doesn’t allow while
loops and most type of for
loops. However, it allows counting for
loops, where the field initializer sets a variable to a constant, the update adds a constant to or subtracts a constant from the variable, and the continuation test compares the variable to a constant.
Note: Due to limited available memory in Web, you should avoid including unneeded shader variants which can lead to unnecessary memory usage. Therefore, Unity recommends familiarizing yourself with shader variants and shader stripping, and take extra care to ensure that you don’t add shaders with too many variants (for example, Unity’s Standard Shader) to the [Always-included Shaders] section(class-GraphicsSettings#Always) in Graphics Settings.
Unity Web supports dynamic font rendering similar to other Unity platforms. However, as it doesn’t have access to the fonts installed on the user’s machine, if you want to use any fonts, make sure to include them in the project folder (including any fallback fonts for international characters, or bold/italic versions of fonts), and set as fallback font names.
WebGL supports anti-aliasing on most (but not on all) combinations of browsers and GPUs. To use it, anti-aliasing must be enabled in the default Quality setting for the Web platform.
Unity Web supports all reflection probes. Note: WebGL 1.0 doesn’t support Smooth realtime reflection probes.
Unity 包含对 WebGL 2.0 API 的支持,因此为 Web 带来了 OpenGL ES 3.0 级的渲染功能。
By default, Unity Web builds support the WebGL 2.0 API. You can configure this in the Web Player settings > Other Settings panel by disabling the Automatic Graphics API property and adding the WebGL 1.0 API to your project.
Browsers with WebGL 2.0 support have the following advantages:
可以在运行时使用 SystemInfo.graphicsDeviceType 来确定 Unity 实例是使用 OpenGLES3 (WebGL2.0) 还是 OpenGLES2 (WebGL1.0) 进行渲染。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.