Unity Web Requests
Downloading Images within Unity
Maps Static API image real case
Integrating features always put me in an excited state!
With this article, I’m going to show you how to download images from the internet (having their URL address) and apply them to UI images in a Unity application.
For the specific example, I download a Google Maps static map image.
The following copyable snippet is what you need as a start.
public IEnumerator downloadImage(string url)
{
UnityWebRequest web = UnityWebRequestTexture
.GetTexture(url); yield return web.SendWebRequest(); if (web.result == UnityWebRequest.Result.ConnectionError)
Debug.LogError(web.error);
else
image.texture = DownloadHandlerTexture.GetContent(web); web.Dispose();
}
As you can see, a UnityWebRequest
is required to perform… web requests (duh?), but many static methods are exposed and serve different purposes.
In this case, to download an image, we are going to follow this strategy:
- get a
Texture
from the web request - apply it to a
RawImage
UI object.
The web request, then, should be in the form of a UnityWebRequestTexture
, from which retrieve the texture.