I’m trying to figure out how to change UI image sprite using a C# script. I’ve been browsing the net for couple of days and i just can’t figure it out. Image can be a local file or URL. Can someone help please?
Thanks
I’m trying to figure out how to change UI image sprite using a C# script. I’ve been browsing the net for couple of days and i just can’t figure it out. Image can be a local file or URL. Can someone help please?
Thanks
Modified Source: Unity - Scripting API: WWW
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ExampleClass : MonoBehaviour {
// The output of the image
public Image img;
// The source image
public string url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg";
IEnumerator Start() {
WWW www = new WWW(url);
yield return www;
img.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0, 0));
}
}
Don’t forget to error check!
if (www.texture != null)
{
m_Image.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0, 0));
}
}