Hi!
I have a small UI Image with the canvas render set to render Camera. I have all of my sprite .png images in a folder. I need to change the image in the UI to another image based on a button press. How do I achieve this?
Hi!
I have a small UI Image with the canvas render set to render Camera. I have all of my sprite .png images in a folder. I need to change the image in the UI to another image based on a button press. How do I achieve this?
public Sprite newSprite;
void Update ()
{
if (Input.GetKeyDown(Keycode.Plus))
GetComponent(SpriteRenderer).sprite = newSprite;
}
Keycode.whatever will be the key you want to trigger the change. This script would go on the object with your sprite renderer. And set the sprite in the inspector for field newSprite before pressing play.
– jprocha101
whatever that folder is called, change its name to Resources. You can then call them using Resources.Load. you can check if a button is pressed by using GUI.Button So You could do something like this: if (GUI.Button(//your rectangle for the button, btnImage){ btnImage = Resources.Load("//new image name") as Texture;
– Vice_VersaOK, this is good but let me be more specific on "button". I meant so say keyboard button. Right now in the code, I have it mapped to Keypad Plus and Keypad Minus keys for another function. Essentially, if I press a keyboard button, I want the image to change.
– Whutchison