防止代码以随机顺序显示同一图像



我制作了一个脚本,每X秒显示一次列表中的随机图像,但有时它会在数小时的游戏过程中显示相同的图像。我想知道如何让它不显示当前游戏会话中已经显示的图像。这是我的代码:

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];

}

任何帮助将不胜感激!

我建议你列出所有图像,随机化列表的顺序,然后循环访问它。当你到达终点时,你可以重新洗牌,也可以重新开始相同的序列。

您可以在此答案中找到一种随机播放列表或数组的好方法。

如何创建一个您拥有的图像列表,然后从该列表中选择一个带有您的int index = Random.Range(0, count);的随机图像,然后您yourImagesList.RemoveAt(index);从列表中删除该项目,以便在下一次迭代中图像将不可用。从列表中删除映像后,可以使用List<T>.Count更新int count = memeImg.Count;的值。 来源: https://msdn.microsoft.com/en-US/library/5CW9x18Z(v=vs.110).aspx 和 https://msdn.microsoft.com/en-us/library/27b47ht3(v=vs.110).aspx

最新更新