Download images and apply to sprite
I have an App (Android and iOS) that's works like a catalogue. I have around 300 'products' that users can view images and descriptions.
Currently, all my images are in the project and assigned as Sprites to an Image component. However, in order to make the App smaller I wanted to download the images after the App has been opened.
My initial idea was to use something like this to download and apply a Sprite to each Image:
public string url;
public string website;
void Start() {
StartCoroutine (GetTexture ());
}
IEnumerator GetTexture() {
UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
yield return www.Send();
if(www.isNetworkError) {
Debug.Log(www.error);
}
else {
Texture2D webTexture = ((DownloadHandlerTexture)www.downloadHandler).texture as Texture2D;
Sprite webSprite = SpriteFromTexture2D (webTexture);
gameObject.GetComponent<Image>().sprite = webSprite;
}
}
Sprite SpriteFromTexture2D(Texture2D texture) {
return Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100.0f);
}
However, with 300 odd images, that's going to be very time consuming.
I want to know if there's a quicker way of doing it. For example, all of my images are in a folder called 'Textures', if I download all the images to this folder, would they automatically link up as this is where they are referencing from?
Comment