我制作了一个脚本,每隔 X 秒显示一次列表中的随机图像,但有时它会在几个小时的游戏过程中显示相同的图像。我想知道如何让它不显示在当前游戏 session 中已经显示的图像。这是我的代码:
void Start () {
// start count
StartCoroutine(RandomReportEvent());
}
xx
IEnumerator RandomReportEvent(){
float wait_time = Random.Range (53.5F, 361.2f); // removed 1879.2f
// test purposes
// float wait_time = Random.Range (1.7F, 3.2f);
yield return new WaitForSeconds(wait_time);
memeWindow.SetActive (true);
// get random reports number within range
float ReportValue = Random.Range (1.2F, 57.8F);
// round to 2 dp
ReportValue = Mathf.Round(ReportValue * 100f) / 100f;
// show random report value in Text
randomReportValue.text = string.Format ("<color=#FF8400FF>{0}K</color>", ReportValue);
// Show Random Image from list
showRandomImage();
}
xx
void showRandomImage(){
// count the amount of images
int count = memeImg.Count;
// randomly select any image from 0 to count number
int index = Random.Range(0, count);
// assing an image from our list
sr.sprite = memeImg[index];
}
如有任何帮助,我们将不胜感激!
最佳答案
我建议您制作一个包含所有图像的列表,随机排列列表的顺序,然后循环遍历它。当您到达终点时,您可以重新洗牌或重新开始相同的序列。
您可以在 this answer. 中找到一种打乱列表或数组的好方法
关于c# - 防止代码以随机顺序显示相同的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50381070/