- Home /
Question by
besmartsoftwaredevelopment · Mar 24, 2018 at 07:15 PM ·
buttonscripting beginnerspritesimages
swap images using 2 buttons(next, previous)
Hi, i'm new to unity. i wanna swap between images using two buttons, i have about 100 images. any help will be appreciated. so far i could change sprite image using a click but i want a script that can go through next image and previous image if possible.
thanks in advance
Comment
Well this should be a simple task, you are basically just enumerating your image array, easiest possible method I can think of is.
public Sprite[] sprites;
public Image image;
private int current;
private void Awake()
{
image.sprite = sprites[current];
}
public void Next()
{
if (current + 1 < sprites.Length)
{
current++;
}
else
{
current = 0;
}
image.sprite = sprites[current];
}
public void Previous()
{
if (current - 1 >= 0)
{
current--;
}
else
{
current = sprites.Length - 1;
}
image.sprite = sprites[current];
}
Then you just assign the OnClick event of the Next button to Next(), the Previous button to Previous, the image object to the image variable, and then fill the sprite array with all your sprites.