- Home /
How do you download image to UI.Image?
This is as far as I got
IEnumerator downloadImg (string url){
WWW www = new WWW(url);
yield return www;
Texture2D texture = new Texture2D(1, 1);
www.LoadImageIntoTexture (texture);
Sprite sprite = Sprite.Create (texture, new Rect (0, 0, texture.width, texture.height), new Vector2 (0.5f, 0.5f));
imagePrefabClone.GetComponent<Image>().sprite = sprite;
}
But for some reason the sprite is not getting set. Any ideas?
Answer by Hoskins355 · Jan 07, 2016 at 10:20 PM
This got it working
IEnumerator isDownloading(string url){
// Start a download of the given URL
var www = new WWW(url);
// wait until the download is done
yield return www;
// Create a texture in DXT1 format
Texture2D texture = new Texture2D(www.texture.width, www.texture.height, TextureFormat.DXT1, false);
// assign the downloaded image to sprite
www.LoadImageIntoTexture(texture);
Rect rec = new Rect(0, 0, texture.width, texture.height);
Sprite spriteToUse = Sprite.Create(texture,rec,new Vector2(0.5f,0.5f),100);
imageToDisplay.sprite = spriteToUse;
www.Dispose();
www = null;
}
Answer by mamad_m2 · Feb 11, 2020 at 11:37 AM
You can do it easily with Davinci
The library has a simple usage and supports Unity UI.Image and 3D model textures.
Davinci.get().load(imageUrl).into(image).start();
Hope this helps!
Why would you use an external library when it can easily be done with Unity?
It's not an external library. It has a simple c# script that written for unity and has a lot of benefits and features like fading animation and process callbacks. Take a look at its that page. You don't have to reinventing the wheel, cause 'time' is matters ;)
Wow that's a cool project And your art style is really fantatastic-looking too
Answer by samra2494 · Nov 01, 2017 at 11:40 AM
I thinks helpful for you.
//here is the code
string WebUrl , ServerUrl; public Image Web_image , ServerImage; Texture2D tex; WWW Link;
IEnumerator LoadImageInternet()
{
tex = new Texture2D(4, 4, TextureFormat.DXT1 , false);
Link = new WWW (WebUrl);
yield return Link;
Link.LoadImageIntoTexture (tex);
Web_image.sprite = Sprite.Create (tex, new Rect (0, 0, tex.width, tex.height), new Vector2 (0, 0));
}
//Button Event
public void LoadFromInternet()
{
WebUrl = "https://upload.wikimedia.org/wikipedia/commons/d/dc/Cats_Petunia_and_Mimosa_2004.jpg";
StartCoroutine (LoadImageInternet ());
}
Answer by Josewdf · Feb 13, 2021 at 10:29 AM
Now in 2021 Unity recommend this code that is more efficient and also worked perfectly.
//PlayGamesPlatform.Instance.GetUserImageUrl() Used to get player Image from Google Play Games
using UnityEngine.Networking;
private IEnumerator GetPlayerImage(string url)
{
UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
yield return www.SendWebRequest();
Texture2D myTexture = DownloadHandlerTexture.GetContent(www);
Rect rec = new Rect(0, 0, myTexture.width, myTexture.height);
Sprite spriteToUse = Sprite.Create(myTexture, rec, new Vector2(0.5f, 0.5f), 100);
playerImage = spriteToUse;
}
Your answer
Follow this Question
Related Questions
!texture.texture error 0 Answers
Error Trying to Download a Image from a Local File 1 Answer
How do I progressively download images with WWW. 1 Answer
Image is not loading into Texture 0 Answers
Detect WWW Image bitmap dimensions? 2 Answers