-1
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ What did you try? \$\endgroup\$
    – Vaillancourt
    Oct 6, 2016 at 12:17
  • \$\begingroup\$ @AlexandreVaillancourt : I got it working \$\endgroup\$ Oct 6, 2016 at 12:23

2 Answers 2

1
\$\begingroup\$

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 :)

\$\endgroup\$
1
  • \$\begingroup\$ Happy to hear that \$\endgroup\$
    – Geo Baby
    Oct 6, 2016 at 12:24
0
\$\begingroup\$

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];
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .