C# 如何在Unity中对for循环应用间隔/延迟

C# 如何在Unity中对for循环应用间隔/延迟,c#,unity3d,C#,Unity3d,我试图以一个for循环中生成的1-3的随机数开始我的场景 我希望数字每次都是随机的,但是我不希望相同的两个数字直接生成,而是先生成1-3之间的随机数,然后等待60秒,生成1-3之间的随机数,不包括最近生成的数字 using System.Collections; using System.Collections.Generic; using UnityEngine; public class KOTH_ZoneRandom : MonoBehaviour { int Rand;

我试图以一个for循环中生成的1-3的随机数开始我的场景

我希望数字每次都是随机的,但是我不希望相同的两个数字直接生成,而是先生成1-3之间的随机数,然后等待60秒,生成1-3之间的随机数,不包括最近生成的数字

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class KOTH_ZoneRandom : MonoBehaviour
{
    int Rand;
    int Lenght = 4;
    List<int> list = new List<int>();
    void Start()
    {
        list = new List<int>(new int[Lenght]);

        for (int j = 1; j < Lenght; j++)
        {

            Rand = Random.Range(1, 4);

            while (list.Contains(Rand))
            {
                Rand = Random.Range(1, 4);
            }

            list[j] = Rand;
            print(list[j]);
        }

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类科思区:单一行为
{
国际兰特;
int长度=4;
列表=新列表();
void Start()
{
列表=新列表(新整数[长度]);
对于(int j=1;j
Edit试图实现一个共同例程作为循环的间隔。但是它仍然不工作,它打印到控制台,因此co例程肯定正在执行,但是WaitForSeconds函数似乎不工作

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class KOTH_ZoneRandom : MonoBehaviour
{
    int Rand;
    int Length = 4;
    List<int> list = new List<int>();
    void Start()
    {
        list = new List<int>(new int[Length]);

        for (int j = 1; j < Length; j++)
        {
            StartCoroutine(loopDelay());
            Rand = Random.Range(1, 4);

            while (list.Contains(Rand))
            {
                Rand = Random.Range(1, 4);
            }

            list[j] = Rand;
            print(list[j]);
        }

    }
    IEnumerator loopDelay ()
    {

        print("started delay");
        yield return new WaitForSeconds(60);
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类科思区:单一行为
{
国际兰特;
整数长度=4;
列表=新列表();
void Start()
{
列表=新列表(新整数[长度]);
对于(int j=1;j
  • Start方法可以是协同程序,因此将返回类型更改为
    IEnumerator

  • 添加
    返回新的WaitForSeconds(60)位于for循环的末尾

  • IEnumerator Start()
    {
    列表=新列表(新整数[长度]);
    对于(int j=1;j
    从性能的角度改进shingo的答案(我不喜欢这个while循环,因为即使它最终会结束,它也可能只会被绑定到最坏的情况下^^。如果它能工作,那就很难看了):

    使用System.Linq;
    void start()
    {
    start例程(KothTimer());
    }
    IEnumerator KothTimer()
    {
    System.Random rnd=新的System.Random();
    var oneToFourShuffled=(新int[4]{1,2,3,4}).OrderBy(x=>rnd.Next()).ToArray();
    对于(int j=0;j
    您有两种选择,协同路由或轮询。你能解释一下如何使用这个值吗?当它改变时总是正好60秒吗?你想让这个序列永远持续下去吗?游戏本身本质上是一个山中之王式的第一人称射击游戏,我想用随机值来随机化得分山的移动模式。是的,如果可能的话,最好永远继续序列,非常感谢回复,因此RGN的第一次迭代将负责分数的第一个位置HillStartCorroutine是非阻塞的,并将调用的函数推入伪并行执行,这就是为什么您没有看到预期的延迟。但是,让start函数调用协同程序更好,正如下面的Immersive建议的那样。然后,协同程序将只包含代码您的启动函数容器。我不能同意您的观点,我认为放弃内置功能来锦上添花并不好。无论如何,我对您的答案投了赞成票,因为它很好;)我同意你的观点,只需要使用内置功能就可以了。我只是习惯于更大的项目,您的start函数可能会调用多个协同程序,例如,使用不同的时间间隔。但你是对的,当编码时,一个人应该数1,2,很多。从start函数中调用外部iEnumerator只有在真正需要的时候才更好。我总是忘记
    start
    本身可以是一个协同程序。我还是不喜欢这样使用它,因为对我来说它感觉不标准。
    void start() {
        StartCoroutine(KothTimer());
    }
    
    IEnumerator KothTimer() {
    
        var wait = new WaitForSeconds(HillSwitchDelay);
    
        while(enabled) {
            var chosenHill = ListOfHills[Random.Range(0, ListOfHills.count)];
            SetActiveHill(chosenHill);
            yield return wait;
        }
    }
    
    using System.Linq;
    
    void start()
    {
        StartCoroutine(KothTimer());
    }
    
    IEnumerator KothTimer()
    {
        System.Random rnd= new System.Random();
        var oneToFourShuffled = (new int[4]{1,2,3,4}).OrderBy(x => rnd.Next()).ToArray();
    
        for (int j = 0; j < Length; j++)
        {
            list[j] = oneToFourShuffled[j];
            print(list[j]);
    
            yield return new WaitForSeconds(60);
        }
    }