Unity Web Requests

Downloading Images within Unity

Maps Static API image real case

Daniele Quero, PhD
Geek Culture
Published in
3 min readJun 17, 2022

--

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:

  1. get a Texture from the web request
  2. apply it to a RawImage UI object.

The web request, then, should be in the form of a UnityWebRequestTexture, from which retrieve the texture.

--

--

Daniele Quero, PhD
Geek Culture

A professional developer with passion for game developing and skill-growing. A former Nuclear Physics Researcher who changed his life to pursue his dreams