Version: 2023.2
Language : English
WebGL performance considerations
Cursor locking and full-screen mode in WebGL

WebGL Networking

You can use networkingThe Unity system that enables multiplayer gaming across a computer network. More info
See in Glossary
in WebGLA JavaScript API that renders 2D and 3D graphics in a web browser. The Unity 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. More info
See in Glossary
in the following two ways:

Optionally, you can use WebSockets or WebRTC from JavaScript to implement your own networking. Note that you can’t use .NET networking classes, because JavaScript code doesn’t have direct access to IP Sockets to implement network connectivity.

Use the UnityWebRequest class in WebGL

Unity supports the UnityWebRequest class in WebGL. To implement the UnityWebRequest class, Unity uses the JavaScript Fetch API, which uses the browser to handle web requests. This imposes security restrictions on accessing cross-domain resources.

If you send a web request to a server other than the one that hosts the Unity content, the server you’re sending it to must authorise the Unity content.

To access cross-domain web resources in WebGL, the server you are trying to access needs to use cross-origin resource sharing (CORS) to authorize cross-domain web resources.

If you try to access content using UnityWebRequest, and the remote server doesn’t have CORS set up or configured correctly, an error like the following appears in the browser console:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://myserver.com/. This can be fixed by moving the resource to the same domain or enabling CORS.

The server needs to add Access-Control headers to the http responses it sends out, to indicate which web pages have permission to read that information from a web browser.

For a demonstration of how to add Access-Control headers that allow Unity WebGL to access resources on a web server from any origin, see the following example. This example includes common request headers and allows the GET, POST or OPTIONS methods:

"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Headers": "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Origin": "*",

UnityWebRequest downloads

Don’t use code that blocks a UnityWebRequest download, such as this:

while(!www.isDone) {}

You can’t block the thread to wait for a UnityWebRequest download to finish otherwise your application freezes. Because WebGL is single threaded, and the fetch API in JavaScript is asynchronous, your download might not finish unless you return control to the browser. Instead, use a Coroutine and a yield statement to wait for the download to finish. For more information, see Examples of coroutines using UnityWebRequest.

Using Unity Multiplayer

Unity Multiplayer enables communication via the WebSockets protocol. See NetworkServer.useWebSockets.

Using WebSockets or WebRTC from JavaScript

WebGL doesn’t allow direct access to IP Sockets, but you can use WebSockets or WebRTC (the two most common networking protocols supported by browsers) to get around this. While WebSockets are widely supported, WebRTC allows peer-to-peer connections between browsers and unreliable connections. Unity doesn’t have a built-in API that allows you to use WebSockets or WebRTC, but you can use a JavaScript plugin to implement this. You can find plugins that implement WebSocket networking on the Unity Asset Store.

WebGL performance considerations
Cursor locking and full-screen mode in WebGL
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961