c# - C# 上的 WaitForSeconds

标签 c# unity3d wait seconds

看看这个简单的代码:

function Start()
{
    yield WaitForSeconds (4);
    Application.LoadLevel(1);
}

有效!我正在尝试使用 C# 做一些类似的事情,但处理器只是忽略了 WaitForSeconds。这是我在 C# 中的代码:

using UnityEngine;
using System.Collections;

public class openingC : MonoBehaviour
{
    void Start()
    {
        executeWait(5);
        Application.LoadLevel(1);
    }

    void executeWait(float aux)
    {
        StartCoroutine(Wait(aux));
    }

    IEnumerator Wait(float seconds)
    {
        yield return new WaitForSeconds(seconds);
    }
}

有人可以向我解释为什么它不起作用吗?感谢您的宝贵时间。

最佳答案

public class openingC : MonoBehaviour
{
    void Start()
    {
        executeWait(5);
        Application.LoadLevel(1);
    }

    void executeWait(float aux)
    {
        StartCoroutine(Wait(aux));
    }

    IEnumerator Wait(float seconds)
    {
        yield return new WaitForSeconds(seconds);
    }
}

首先,Start方法运行,executeWait被调用,程序跳转到该方法。它找到协程并开始运行它,直到找到 yield 或方法结束。 Yield 返回到程序,指针返回到 executeWait 并完成该方法。指针返回到 Start 并调用 Application.LoadLevel。

您想挂起 LoadLevel 调用。

public class openingC : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(Wait(5));
    }

    //You don't need executeWait

    IEnumerator Wait(float seconds)
    {
        yield return new WaitForSeconds(seconds);
        Application.LoadLevel(1);
    }
}

关于c# - C# 上的 WaitForSeconds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34242353/

相关文章:

image - Netbeans 中的 J2ME(gif 图像问题)

c# 委托(delegate)和事件将文本从类发送到表单

c# - java 与 c# - 哪个在安全性方面更安全?

unity3d - 如何在非 GUI 元素中使用 "9-sliced"图像类型?

c# - unity c#马里奥游戏

Java进程waitFor()函数导致应用程序卡住

C#反射,获取重载方法

c# - 从多个 PNG 创建一个 PNG

c# - UnityScript 或 C# 中的 Eval() 替换为 Unity

c - 当第二个子进程关闭读取管道末尾时杀死第一个子进程