How to have get button display a raw image?
Hi, currently I'm trying to get a button to display a raw image. I have the game objects set-up in the canvas I just don't know how to get the image to be invisible when the scene loads and then when the button is clicked to have the image appear.
For context this is for a character selection screen and I'm wanting the image of the player character to display when the character head shot is clicked on.
Answer by Dinosaurs · Nov 19, 2015 at 11:31 PM
Disable the gameObject with the image, and have the button activate it when clicked.
gameObject.SetActive(true);
I'm completely new to coding do you care to elaborate a bit more, please.
Answer by SeasiaInfotechind · Nov 20, 2015 at 05:39 AM
Hi, If I have understood your problem correctly, solution is below:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class abc : MonoBehaviour {
public Button xyz; // button to be clicked
public Sprite change_texture; // texture you want to assign on button click
// Use this for initialization
void Start () {
}
// Update is called once per frame
public void ButtonClicked ()
{
xyz.GetComponent<Image>().sprite = change_texture; // on clicking button sprite would change
}
}
Also don't forget to assign button function to be called on buttonclick from inspector.ScreenShot is attached. Script is attached to main camera.
I hope your problem gets solved.Have a good Day!!
Hi, first of all thanks for your help.
I have a couple of queries, I'm unsure where to put in the specific names of the objects and images in my project.
For context these are the names of the items I'm using.
Button to be clicked : Es
UI Raw Image (place where I want to image to be displayed) : EsFull
Image I want displayed : EsmeraldaFull
From the code you gave me this is what I have so far :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ShowPlayerCharacter : $$anonymous$$onoBehaviour {
public Button Es; // button to be clicked
public Sprite EsmeraldaFull; // texture you want to assign on button click
// Use this for initialization
void Start () {
}
// Update is called once per frame
public void ButtonClicked ()
{
Es.GetComponent<Image>().sprite = EsmeraldaFull; // on clicking button sprite would change
}
}
Also I can see that you placed the script on the camera, is there any specific options that I need to select on the camera to get the script to work.
Thanks again.
Hi, Were you able to make it work? Its not necessary to place your script on camera.You can place it on any object and drag drop that object on button Onclick property. In order to fetch the method you need to give reference of the script and its done by passing the object holding the script. So here my script was attached to camera thats why i dragged camera on Onclick property of button. If you will select camera and check inspector you will find the script attached .You only need to drag drop all the reference. eg: In option showing EsmeraldaFull , drag image from assets . Feel free to ask if any doubts.
Answer by superhero02 · Jun 25, 2017 at 09:04 AM
When I Put characters in rawimage button how can you do levelmanager and character select at the same time