简体   繁体   English

在Unity C#中延迟执行'If'语句

[英]Delaying the execution of an 'If' statement in Unity C#

i am looking to execute a specific piece of code directly after the first one has finished and not at the same time like they are currently running. 我希望在第一个代码完成后直接执行特定的代码,而不是像它们当前正在运行的那样同时执行。

private void Update()
{
    //This is the code to be executed first
    if ((textActive == true) && (stopText == false))
    {
        Debug.Log("TextActive");
        KeyText("On");
        objectToEnable4.SetActive(true);
        stopText = true;
    }    

    //after which this code will execute to disable Object4
    if (stopText == true)
    {

        objectToEnable4.SetActive(false);
    }
}

both pieces of code work perfectly i just need to implement a delay for the second code section i'm looking to delay the code for 2 seconds to allow time for an animation to play 这两段代码都可以正常工作,我只需要为第二个代码部分实现一个延迟即可,我希望将代码延迟2秒钟,以便有时间播放动画

thanks for the help in advance. 我在这里先向您的帮助表示感谢。

Good time to use a coroutine: 是时候使用协程了:

private void Update()
{
    //This is the code to be executed first
    if ((textActive == true) && (stopText == false))
    {
        Debug.Log("TextActive");
        KeyText("On");
        objectToEnable4.SetActive(true);
        stopText = true;
        StartCoroutine(myDelay());
    }
}

IEnumerator myDelay()
{
    // waits for two seconds before continuing
    yield return new WaitForSeconds(2f);

    if (stopText == true)
    {
        objectToEnable4.SetActive(false);
    }
}

Base on the code that you have provided, I think they are doing just what you want it to do: Execute in order. 根据您提供的代码,我认为他们正在按照您希望的方式进行操作:按顺序执行。 However, because the code is very simple so it may seems like they are running at the same time, which make you can not see the objectToEnable4 at all. 但是,由于代码非常简单,因此似乎它们正在同时运行,这使您根本看不到objectToEnable4。 The way you can pause the execution is by using Unity's Coroutine. 暂停执行的方法是使用Unity的协程。

The following code are the example of coroutine at Unity's Scritping API: 以下代码是Unity Scritping API上协程的示例:

using UnityEngine;
using System.Collections;

public class WaitForSecondsExample : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(Example());
    }

    IEnumerator Example()
    {
        print(Time.time);
        yield return new WaitForSecondsRealtime(5);
        print(Time.time);
    }
}

The first answer perfectly applied the corotine to your code. 第一个答案完美地将corotine应用于了您的代码。 Hope you find it useful. 希望你觉得它有用。

imo there's an even 'better' way (at least, simpler way) using Invoke. imo使用Invoke甚至有一种“更好”的方式(至少是更简单的方式)。

private void Update()
{
    if (textActive && !stopText)
    {
        KeyText("On");
        objectToEnable4.SetActive(true);
        stopText = true;
        Invoke("MyDelay", 2);
    }
}

private void MyDelay()
{
    if (stopText) 
    {
        objectToEnable4.SetActive(false);
    }
}

I'm not sure why you even need to use the bool stopText, maybe for something else you haven't shown us? 我不确定为什么您甚至需要使用bool stopText,也许还有其他您没有给我们看的东西? If not you could remove that too! 如果没有,您也可以删除它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM