循环图像数组
Loop Through Array of Images
我有一张 UI 图片,我希望它在 UI 按钮点击时显示不同的图片。我知道如何遍历 UI 文本,但不知道如何遍历 UI 图片。以下是我可以对文本执行的操作:
Text text;
string[] array = {"Hi!", "Ho!", "You clicked me!"};
int num = 0;
void Start() {
text = GetComponent<Text> ();
}
void Change() {
num++;
if(num == 3) {
num = 0;
}
text.text = array[num];
}
我怎样才能对图像做同样的事情?
这是你如何做到这一点,我最近在我的游戏中实现了这个:
public class ImageHandler : MonoBehaviour
{
public Image img;
public List<Sprite> imageObjs;
private Sprite activeImage;
public void LoadLevelImage(int levelNumber)
{
this.activeImage = (Sprite)Instantiate(imageObjs[levelNumber - 1]);
img.sprite = activeImage;
}
}
我有一张 UI 图片,我希望它在 UI 按钮点击时显示不同的图片。我知道如何遍历 UI 文本,但不知道如何遍历 UI 图片。以下是我可以对文本执行的操作:
Text text;
string[] array = {"Hi!", "Ho!", "You clicked me!"};
int num = 0;
void Start() {
text = GetComponent<Text> ();
}
void Change() {
num++;
if(num == 3) {
num = 0;
}
text.text = array[num];
}
我怎样才能对图像做同样的事情?
这是你如何做到这一点,我最近在我的游戏中实现了这个:
public class ImageHandler : MonoBehaviour
{
public Image img;
public List<Sprite> imageObjs;
private Sprite activeImage;
public void LoadLevelImage(int levelNumber)
{
this.activeImage = (Sprite)Instantiate(imageObjs[levelNumber - 1]);
img.sprite = activeImage;
}
}