栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何以统一的简单方式使脚本等待/休眠

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何以统一的简单方式使脚本等待/休眠

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

1
用协程和

WaitForSeconds

到目前为止,这是最简单的方法。将需要等待一段时间的所有代码放入协程函数中,然后可以使用

WaitForSeconds
。请注意,在协程函数中,您可以使用调用该函数
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

之间唯一的区别

WaitForSeconds
WaitForSecondsRealtime
WaitForSecondsRealtime
为使用非标度的时间来等待,这意味着暂停了比赛的时候
Time.timeScale
,该
WaitForSecondsRealtime
功能将不会受到影响,但
WaitForSeconds
会。

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

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

当您希望使用

boolean
真变量来中断等待/睡眠时,这也很好。这是
yield break;
可以使用的地方。

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
循环移到另一个协程函数并产生它来简化此过程,并且仍然可以看到它的计数甚至中断计数器。

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
功能:

等到条件变为

true
。一个示例是一个函数,它等待玩家的分数被
100
加载,然后加载下一个级别。

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
功能。

等待条件成立

true
。例如,当您想按退出键退出应用程序时。

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
具有功能:

您将来可以调用告诉Unity来调用函数。调用该

Invoke
函数时,您可以传递时间等待将该函数调用为其第二个参数。下面的示例将
feedDog()
5
Invoke
被调用后调用该函数。

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

7

Update()
具有和功能
Time.deltaTime

就像 #3一样, 除了不使用协程。它使用该

Update
功能。

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

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中制作游戏。何时使用每个视情况而定。

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

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);}

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

StartCoroutine (showTextFuntion());


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/394113.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号