To retrieve a Texture file from a remote server, you can use UnityWebRequestTexture.GetTexture. This function is very similar to UnityWebRequest.Get, but is optimized for downloading and storing textures efficiently.
UnityWebRequestTexture.GetTexture takes a string or Uri object as an argument that specifies a URL of an image file to download and use as a Texture. Additionally, it can take DownloadedTextureParams as second argument, allowing you more control over the texture that will be created.
UnityWebRequest and sets the target URL to the string argument. This function sets no other flags or custom headers.DownloadHandlerTexture object to the UnityWebRequest. DownloadHandlerTexture is a specialized Download Handler which is optimized for storing images which are to be used as Textures in the Unity Engine. Using this class significantly reduces memory reallocation compared with downloading raw bytes and creating a Texture manually in script.using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class MyBehaviour : MonoBehaviour {
void Start() {
StartCoroutine(GetTexture());
}
IEnumerator GetTexture() {
UnityWebRequest www = UnityWebRequestTexture.GetTexture("https://www.my-server.com/image.png");
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success) {
Debug.Log(www.error);
}
else {
Texture myTexture = DownloadHandlerTexture.GetContent(www);
}
}
}