如何以统一的简单方式使脚本等待/休眠 [英] How make the script wait/sleep in a simple way in unity

查看:95
本文介绍了如何以统一的简单方式使脚本等待/休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在TextUI.text = ....之间放置一个睡眠功能,以在每个短语之间等待3秒?

How can I put a sleep function between the TextUI.text = ...., to wait 3 seconds between each phrase?

public Text GuessUI;
public Text TextUI;

[...truncated...]

TextUI.text = "Welcome to Number Wizard!";
TextUI.text = ("The highest number you can pick is " + max);
TextUI.text = ("The lowest number you can pick is " + min);

我已经尝试过各种方法,但是没有任何效果,例如:

I already tried various things but none have worked, such as this:

TextUI.text = "Welcome to Number Wizard!";
yield WaitForSeconds (3);
TextUI.text = ("The highest number you can pick is " + max);
yield WaitForSeconds (3);
TextUI.text = ("The lowest number you can pick is " + min);

在bash中,应该是:

In bash, it would be:

echo "Welcome to Number Wizard!"
sleep 3
echo "The highest number you can pick is 1000"
sleep 3
.....

但是我不知道如何在Unity中使用C#做到这一点

but I can't figure out how to do this in Unity with C#

推荐答案

在Unity中等待的方式有很多.这确实很简单,但是我认为值得涵盖执行这些操作的大多数方法:

There are many ways to wait in Unity. It is really simple but I think it's worth covering most ways to do these:

1 .带有协程和 WaitForSeconds

1.With a coroutine and WaitForSeconds.

到目前为止,这是最简单的方法.将需要等待一段时间的所有代码放在协程函数中,然后可以使用 .请注意,在协程函数中,您可以使用StartCoroutine(yourFunction)调用该函数.

The is by far the simplest way. Put all the code that you need to wait for some time in a coroutine function then you can wait with WaitForSeconds. Note that in coroutine function, you call the function with StartCoroutine(yourFunction).

下面的示例将旋转90度,等待4秒,旋转40度,等待2秒,然后最终旋转20度.

void Start()
{
    StartCoroutine(waiter());
}

IEnumerator waiter()
{
    //Rotate 90 deg
    transform.Rotate(new Vector3(90, 0, 0), Space.World);

    //Wait for 4 seconds
    yield return new WaitForSeconds(4);

    //Rotate 40 deg
    transform.Rotate(new Vector3(40, 0, 0), Space.World);

    //Wait for 2 seconds
    yield return new WaitForSeconds(2);

    //Rotate 20 deg
    transform.Rotate(new Vector3(20, 0, 0), Space.World);
}

2 .使用协同程序和 WaitForSecondsRealtime

2.With a coroutine and WaitForSecondsRealtime.

WaitForSecondsWaitForSecondsRealtime之间的唯一区别是WaitForSecondsRealtime正在使用无标度的时间等待,这意味着当使用Time.timeScale暂停游戏时,WaitForSecondsRealtime功能不会受到影响,但WaitForSeconds会.

The only difference between WaitForSeconds and WaitForSecondsRealtime is that WaitForSecondsRealtime is using unscaled time to wait which means that when pausing a game with Time.timeScale, the WaitForSecondsRealtime function would not be affected but WaitForSeconds would.

void Start()
{
    StartCoroutine(waiter());
}

IEnumerator waiter()
{
    //Rotate 90 deg
    transform.Rotate(new Vector3(90, 0, 0), Space.World);

    //Wait for 4 seconds
    yield return new WaitForSecondsRealtime(4);

    //Rotate 40 deg
    transform.Rotate(new Vector3(40, 0, 0), Space.World);

    //Wait for 2 seconds
    yield return new WaitForSecondsRealtime(2);

    //Rotate 20 deg
    transform.Rotate(new Vector3(20, 0, 0), Space.World);
}


等待,仍然可以看到您等待了多长时间:

3 .使用协程,并使用 Time.deltaTime .

3.With a coroutine and incrementing a variable every frame with Time.deltaTime.

一个很好的例子是,当您需要计时器在屏幕上显示它等待了多少时间时.基本上就像一个计时器.

A good example of this is when you need the timer to display on the screen how much time it has waited. Basically like a timer.

当您想使用boolean变量来中断等待/睡眠时也不错,它为true.在这里可以使用yield break;.

It's also good when you want to interrupt the wait/sleep with a boolean variable when it is true. This is where yield break; can be used.

bool quit = false;

void Start()
{
    StartCoroutine(waiter());
}

IEnumerator waiter()
{
    float counter = 0;
    //Rotate 90 deg
    transform.Rotate(new Vector3(90, 0, 0), Space.World);

    //Wait for 4 seconds
    float waitTime = 4;
    while (counter < waitTime)
    {
        //Increment Timer until counter >= waitTime
        counter += Time.deltaTime;
        Debug.Log("We have waited for: " + counter + " seconds");
        //Wait for a frame so that Unity doesn't freeze
        //Check if we want to quit this function
        if (quit)
        {
            //Quit function
            yield break;
        }
        yield return null;
    }

    //Rotate 40 deg
    transform.Rotate(new Vector3(40, 0, 0), Space.World);

    //Wait for 2 seconds
    waitTime = 2;
    //Reset counter
    counter = 0;
    while (counter < waitTime)
    {
        //Increment Timer until counter >= waitTime
        counter += Time.deltaTime;
        Debug.Log("We have waited for: " + counter + " seconds");
        //Check if we want to quit this function
        if (quit)
        {
            //Quit function
            yield break;
        }
        //Wait for a frame so that Unity doesn't freeze
        yield return null;
    }

    //Rotate 20 deg
    transform.Rotate(new Vector3(20, 0, 0), Space.World);
}

您仍然可以通过将while循环移到另一个协程函数并产生它来简化此过程,并且仍然可以看到它的计数,甚至中断计数器.

You can still simplify this by moving the while loop into another coroutine function and yielding it and also still be able to see it counting and even interrupt the counter.

bool quit = false;

void Start()
{
    StartCoroutine(waiter());
}

IEnumerator waiter()
{
    //Rotate 90 deg
    transform.Rotate(new Vector3(90, 0, 0), Space.World);

    //Wait for 4 seconds
    float waitTime = 4;
    yield return wait(waitTime);

    //Rotate 40 deg
    transform.Rotate(new Vector3(40, 0, 0), Space.World);

    //Wait for 2 seconds
    waitTime = 2;
    yield return wait(waitTime);

    //Rotate 20 deg
    transform.Rotate(new Vector3(20, 0, 0), Space.World);
}

IEnumerator wait(float waitTime)
{
    float counter = 0;

    while (counter < waitTime)
    {
        //Increment Timer until counter >= waitTime
        counter += Time.deltaTime;
        Debug.Log("We have waited for: " + counter + " seconds");
        if (quit)
        {
            //Quit function
            yield break;
        }
        //Wait for a frame so that Unity doesn't freeze
        yield return null;
    }
}


等待/休眠,直到变量更改或等于另一个值:

4 .带有协同程序和 WaitUntil 功能:

4.With a coroutine and the WaitUntil function:

等待直到条件变为true.一个示例是一个函数,该函数等待玩家的得分为100然后加载下一个级别.

Wait until a condition becomes true. An example is a function that waits for player's score to be 100 then loads the next level.

float playerScore = 0;
int nextScene = 0;

void Start()
{
    StartCoroutine(sceneLoader());
}

IEnumerator sceneLoader()
{
    Debug.Log("Waiting for Player score to be >=100 ");
    yield return new WaitUntil(() => playerScore >= 10);
    Debug.Log("Player score is >=100. Loading next Leve");

    //Increment and Load next scene
    nextScene++;
    SceneManager.LoadScene(nextScene);
}

5 .带有协同程序和 WaitWhile 功能.

5.With a coroutine and the WaitWhile function.

在条件为true时等待.例如,当您想按退出键时退出应用程序.

Wait while a condition is true. An example is when you want to exit app when the escape key is pressed.

void Start()
{
    StartCoroutine(inputWaiter());
}

IEnumerator inputWaiter()
{
    Debug.Log("Waiting for the Exit button to be pressed");
    yield return new WaitWhile(() => !Input.GetKeyDown(KeyCode.Escape));
    Debug.Log("Exit button has been pressed. Leaving Application");

    //Exit program
    Quit();
}

void Quit()
{
    #if UNITY_EDITOR
    UnityEditor.EditorApplication.isPlaying = false;
    #else
    Application.Quit();
    #endif
}


6 .使用 Invoke 功能:


6.With the Invoke function:

您将来可以调用告诉Unity来调用函数.调用Invoke函数时,您可以传递等待时间,然后再将该函数调用为其第二个参数.下面的示例将在调用Invoke5秒后调用feedDog()函数.

You can call tell Unity to call function in the future. When you call the Invoke function, you can pass in the time to wait before calling that function to its second parameter. The example below will call the feedDog() function after 5 seconds the Invoke is called.

void Start()
{
    Invoke("feedDog", 5);
    Debug.Log("Will feed dog after 5 seconds");
}

void feedDog()
{
    Debug.Log("Now feeding Dog");
}

7 .使用Update()函数和 .

#3 相似,只是它不使用协程.它使用Update函数.

It's just like #3 except that it does not use coroutine. It uses the Update function.

问题在于它需要太多的变量,因此它不会每次都运行,而是在等待后计时器结束时仅运行一次.

The problem with this is that it requires so many variables so that it won't run every time but just once when the timer is over after the wait.

float timer = 0;
bool timerReached = false;

void Update()
{
    if (!timerReached)
        timer += Time.deltaTime;

    if (!timerReached && timer > 5)
    {
        Debug.Log("Done waiting");
        feedDog();

        //Set to false so that We don't run this again
        timerReached = true;
    }
}

void feedDog()
{
    Debug.Log("Now feeding Dog");
}


还有其他在Unity中等待的方法,但是您一定应该知道上面提到的方法,因为这样可以更轻松地在Unity中制作游戏.何时使用每个视情况而定.


There are still other ways to wait in Unity but you should definitely know the ones mentioned above as that makes it easier to make games in Unity. When to use each one depends on the circumstances.

对于您的特定问题,这是解决方案:

For your particular issue, this is the solution:

IEnumerator showTextFuntion()
{
    TextUI.text = "Welcome to Number Wizard!";
    yield return new WaitForSeconds(3f);
    TextUI.text = ("The highest number you can pick is " + max);
    yield return new WaitForSeconds(3f);
    TextUI.text = ("The lowest number you can pick is " + min);
}

要从启动或更新函数中调用/启动协程函数,请使用

And to call/start the coroutine function from your start or Update function, you call it with

StartCoroutine (showTextFuntion());

这篇关于如何以统一的简单方式使脚本等待/休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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