I want to change the image on the button click.I have two two button(front and back).When I click on front button the image should get changed.When I click backward the previous image should be loaded.
How can I do it
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.
Sign up to join this communityI want to change the image on the button click.I have two two button(front and back).When I click on front button the image should get changed.When I click backward the previous image should be loaded.
How can I do it
1.
You have an Image component or Texture2D component
Image image;
// or Texture2D texture;
2.
You have an array of sprites
Texture2D[] mySprites;
3.
for right button click -> i++
for left button click -> i--
image.sprite=mySprites[i];
// or texture = mySprites[i];
I think that's what you are looking for :)
Here its a working script .
public Sprite[] gallery; //store all your images in here at design time
public Image displayImage; //The current image thats visible
public Button nextImg; //Button to view next image
public Button prevImg; //Button to view previous image
public int i = 0; //Will control where in the array you are
public void BtnNext () {
if(i + 1 < gallery.Length){
i++;
}
}
public void BtnPrev () {
if(i - 1 > 0){
i--;
}
}
void Update () {
displayImage.sprite = gallery[i];
}