System.Net namespace, particularly
System.Net.Sockets) are non-functional in WebGL. The same applies to Unity’s old
UnityEngine.Network* classes, which are not available when building for WebGL.
If you need to use Networking in WebGL, you currently have the options to use the
The WWW and UnityWebRequest classes are supported in WebGL. They are implemented using the
If you try to access content using
UnityWebReqest, and the remote server does not have CORS set up or configured correctly, you will see an error like this 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.
CORS stands for Cross-Origin Resource Sharing, and is documented here. Basically, the server needs to add some
Access-Control headers to the http responses it sends out, which will tell browsers that it is allowed to let web pages access the content on the server. Here’s an example of a header setup, which would allow Unity WebGL to access resources on a web server from any origin, with common request headers and using the http
"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": "*",
Note that WWW.responseHeaders is limited to a subset of the actual response headers, according to 7.1.1 of the CORS specification.
Also note that XMLHttpRequest does not allow streaming of data, thus the WWW class on WebGL will only process any data once the download has finished (so AssestBundles cannot be decompressed and loaded while downloading as on other platforms).
Do not use code which blocks on a WWW or WebRequest download, like this:
Blocking on WWW or WebRequest downloads does not work on Unity WebGL. Because WebGL is single threaded, and because the
yield statement to wait for the download to finish.
Did you find this page useful? Please give it a rating: