为什么收益率返回新的等待秒不起作用

本文关键字:等待 不起作用 收益率 返回 为什么 | 更新日期: 2023-09-27 18:24:30

以下是我的代码:

void Start ()
{
    Debug.Log("before");
    StartCoroutine(CharTooLong());
    Debug.Log("after");
}
IEnumerator CharTooLong(){
    Debug.Log ("up");
    yield return new WaitForSeconds(2);
    Debug.Log ("down");
}

我只是使用这两种方法来运行,但控制台只打印如下:

before
up
after

为什么不打印"down"?我有进口多特温在我的团结,这是效果吗?

如何解决这个问题?

为什么收益率返回新的等待秒不起作用

如果游戏暂停,WaitForSeconds将不工作(timescale = 0)重新启动游戏不会重置时间刻度。您可以在Edit -> Project settings -> Time检查时间刻度参数中检查当前时间刻度。

请确保您有适当的时间刻度,或者使用WaitForSeconds:的替代品

void Start ()
{
    Debug.Log("before");
    StartCoroutine(CharTooLong());
    Debug.Log("after");
}
IEnumerator CharTooLong(){
    Debug.Log ("up");
    yield return new WaitForUnscaledSeconds(2);
    Debug.Log ("down");
}
public static IEnumerator WaitForUnscaledSeconds(float time){
    float ttl = 0;
    while(time > ttl){
        ttl += Time.unscaledDeltaTime;
        yield return null;
    }
}

您使用了unityScript标记,但您的代码是c#!如果您使用的是UnityScript,您的代码必须是这样的:

function Start () {
    Debug.Log("before");
    StartCoroutine("CharTooLong");
    Debug.Log("after");
}
function movement() {
    Debug.Log ("up");
    yield WaitForSeconds(2);
    Debug.Log ("down");
}