Note: UNet is deprecated, and will be removed from Unity in the future. A new system is under development. For more information and next steps see this blog post and the FAQ. |
To retrieve a Texture file from a remote server, you can use UnityWebRequest.Texture.
This function is very similar to UnityWebRequest.GET
but is optimized for downloading and storing textures efficiently.
This function takes a single string as an argument. The string specifies the URL from which you wish to download an image file for use as a Texture.
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__The Unity system that enables multiplayer gaming across a computer network. [More info](UNetOverview.html)<span class="tooltipGlossaryLink">See in [Glossary](Glossary.html#Networking)</span>;
public class MyBehaviour : MonoBehaviour {
void Start() {
StartCoroutine(GetTexture());
}
IEnumerator GetTexture() {
UnityWebRequest www = UnityWebRequestTexture.GetTexture("http://www.my-server.com/image.png");
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError) {
Debug.Log(www.error);
}
else {
Texture myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
}
}
}
Alternatively, you can implement GetTexture using a helper getter:
IEnumerator GetTexture() {
UnityWebRequest www = UnityWebRequestTexture.GetTexture("http://www.my-server.com/image.png");
yield return www.SendWebRequest();
Texture myTexture = DownloadHandlerTexture.GetContent(www);
}