C# 上的 WaitForSeconds [英] WaitForSeconds on C#

查看:35
本文介绍了C# 上的 WaitForSeconds的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这个简单的代码:

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

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

It works! I'm trying to make something similiar using C#, but the processor just ignore the WaitForSeconds. This is my code in 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);
    }
}

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

Can someone explain to me why it's not working? Thanks for your time.

推荐答案

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.

First, the Start method runs, executeWait is called, the program jumps to the method. It finds the coroutine and starts running it until a yield is found or the end of the method. Yield returns to the program, the pointer goes back up to executeWait and finishes the method. The pointer goes back up to Start and calls for Application.LoadLevel.

您想挂起 LoadLevel 调用.

You want to hang the LoadLevel call.

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# 上的 WaitForSeconds的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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