- Home /
can we use sprite renderer component with Image UI eleement ?
i'm working in UI and i have to change image of "Image UI element" , is it possible or not ? if yes then how ? thanks in advance
Comment
Answer by Ali-hatem · May 13, 2016 at 05:06 PM
UI Image have component called Image (Script) with field named Source Image this is your friend you can drug/drop sprite from asset folder to it or change from script like this . script attached to empty game object :
public Image im;//attach the ui image from hierarchy
public Sprite sp;//attach sprite from asset folder
void Start(){
im.sprite = sp;
}
other example script attached to the ui image it self :
Image im;
public Sprite sp;//attach sprite from asset folder
void Start(){
im = GetComponent<Image> ();
im.sprite = sp;
}
but what if my source image is null at beginning and change sprites at some event trigger and again null at some other trigger, is it good enough to use Resources.Load("something") as Sprite; ? in order to avoid passing all the references of sprite to my script
Your answer