Version: 2020.3
WebGL 빌드 디버깅 및 문제 해결

WebGL 네트워킹

You can use networking in WebGL in the following two ways:

  • UnityWebRequest class

  • Unity Multiplayer(previously UNet)

    Note: UNet is a deprecated solution and a new Multiplayer and Networking Solution (MLAPI) is under development.

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

This page contains guidelines on how to use networking in WebGL.

Using 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 does not 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 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

Do not use code that blocks a UnityWebReqest download, like this:

while(!www.isDone) {}

You cannot 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 does not 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 does not 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 빌드 디버깅 및 문제 해결