- Home /
Problem dipsplaying an image from url to UI.Image component
Hi! What I need to do is download an image from a url and display it in a UI.Image component (so as sprite). I managed to do this with the script below, but only seems to work in unity, while not working on my android tablet (application crash).
public void Start () {
StartCoroutine(RealLoadImage());
}
private IEnumerator RealLoadImage () {
string url = "http://www.MY_URL_IMAGE...";
WWW imageURLWWW = new WWW(url);
yield return imageURLWWW;
if(imageURLWWW.texture != null)
{
Sprite sprite = new Sprite();
sprite = Sprite.Create(imageURLWWW.texture, new Rect(0, 0, imageURLWWW.texture.width, imageURLWWW.texture.height), Vector2.zero);
GetComponent<UnityEngine.UI.Image>().sprite = sprite;
}
yield return null;
}
This script is attached to the UI.Image
Bump guys. In the same pickle at the moment. Using Unity 5.2
Within the android manifest... have you defined the permissions for internet access?
<uses-permission android:name="android.permission.INTERNET" />
Answer by John3D · Nov 30, 2016 at 02:08 PM
Your solution is here: http://answers.unity3d.com/questions/1175862/loading-a-sprite-from-url-c.html
Answer by palomo9314 · May 14, 2020 at 04:10 PM
@Requiem99 your code worked for me on android, THANK YOU A LOT I LOVE YOU! public Sprite spritex; public Image imagen; public Text texto;
void Start()
{
}
public void cargaimagen4(){
StartCoroutine(RealLoadImage());
}
private IEnumerator RealLoadImage () {
string url = "htttp://imagelocation.png";
WWW imageURLWWW = new WWW(url);
texto.text+=url+"|";
yield return imageURLWWW;
if(imageURLWWW.texture != null)
{
Debug.Log("texture bien"+url);
texto.text+="url valida";
spritex = Sprite.Create(imageURLWWW.texture, new Rect(0, 0, imageURLWWW.texture.width, imageURLWWW.texture.height), Vector2.zero);
imagen.sprite=spritex;
texto.text+="ya debio cargarse";
}else{
Debug.Log("texture null");
texto.text+="null";
}
yield return null;
}
Your answer
