在 Unity 中使用延迟创建无限循环 [英] Create infinite loop in Unity with delay

查看:32
本文介绍了在 Unity 中使用延迟创建无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Unity 中创建一个无限循环而不使用主线程?我看到了一些例子,但它没有用:

I need to create a infinite loop in Unity without using the main thread? I saw some example, but it's not usefull:

while(true){
         var aa;
         debug.log("print.");
} 

我想添加一些延迟,例如2 秒.如果有人知道解决方案,请帮忙.

I want to add some delay like e.g. 2 seconds. If anybody knows the solution please help.

推荐答案

用这个来创建循环;

private IEnumerator LoopFunction(float waitTime)
{
    while (true)
    {
        Debug.Log("print.");
        yield return new WaitForSeconds(waitTime);
        //Second Log show passed waitTime (waitTime is float type value ) 
        Debug.Log("print1.");
    }
}

为了调用函数,不要使用 Update()FixedUpdate(),使用类似 Start() 这样你就不会'不创建循环的无限实例;

For calling the function, don't use Update() or FixedUpdate(), use something like Start() so you don't create infinite instances of the loop;

 void Start()
 {
      StartCoroutine(LoopFunction(1));
 }

这篇关于在 Unity 中使用延迟创建无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆