- Home /
load URL images in Webgl.
I used the same example in the scripting api for load images. http://docs.unity3d.com/ScriptReference/WWW-texture.html
the image can load for webplayer and other platforms, but for WebGL it doesnt load the image. the error in the browser said. "Access-Control-Allow-Origin" thanks for the help. :)
Answer by vladrybak · Sep 26, 2015 at 04:10 PM
On WebGL If you try to access content using WWW, and the remote server does not have CORS set up or configured correctly, you will see an error like this in the browser console.
The Unity WWW class for WebGL is implemented using the XMLHttpRequest class in JavaScript and using the browser to handle WWW requests. This imposes some security restrictions on accessing cross-domain resources. Basically any WWW request to a server which is different from the server hosting the WebGL content needs to be authorized by the server you are trying to access. This is similar to the crossdomain.xml requirement in the Unity Web Player, but the protocol is different. To access cross-domain WWW resources in WebGL, the server you are trying to access needs to authorize this using CORS.
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 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": "*",
CORS stands for Cross-Origin Resource Sharing, and is documented here.
Your answer
Follow this Question
Related Questions
WebGL - Open URL in new tab? 10 Answers
Unity + Cors in WebGL 1 Answer
Multiple Cursors - WebGL Unity 5.6 2 Answers
Webgl browser error " DirectoryNotFoundException: Could not find a part of the path" 0 Answers
WebGL - InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable 1 Answer