6

Everything I search is based on the old UI.

I've tried the following

button.image = Resource.Load<Image>("...");
button.GetComponent<Image>() = Resources.Load<Image>("....");
button.GetComponent<Button>().image = Resources.Load<Image>("....");
button.GetComponent<Image>().sprite = Resources.Load<Sprite>("....");

I want to change the button image on an event.

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
Mastro
  • 1,477
  • 2
  • 22
  • 52

2 Answers2

6

Tested and Working.

public Sprite myImage;
public Button myBtn;
void Start(){

         myImage = Resources.Load<Sprite>("BTNS"); // Make sure not to include the file extension

         //Make sure it is added in the Inspector. Or reference it using GameObject.Find.
         myBtn.image.sprite = myImage; // That is right, no need to GetComponent.

}
Aizen
  • 1,807
  • 2
  • 14
  • 28
3

Also you can use the properties of Button Component

http://docs.unity3d.com/Manual/script-Button.html

Specifically you can set the button's behavior based on events.

Example:

Empty properties

And then :

Button with sprite swap behavior on events

mayo
  • 3,845
  • 1
  • 32
  • 42