c# - 防止代码以随机顺序显示相同的图像?

标签 c# unity3d

我制作了一个脚本,每隔 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/

相关文章:

c# - Unity 中的游戏逻辑编程

c# - 在Android设备上从Unity3D访问SQLite数据库时出现问题

c# - 页面范围的正则表达式

c# - 使 ScaleTransform 从中心而不是左上角开始

c# - 在Google Play商店中提交 “500 No individual errors”的原因

c# - 从本地主机和在线 asp.net 发送电子邮件

c# - 将一个字符串换行成多行

c# - 使用 Debugger.Log 调用记录附加信息

c# - 如何使用按钮统一打开和关闭声音

c# - Unity(Android Studio)重复录入错误