Startcoroutine无效

时间:2016-09-10 14:45:55

标签: unity3d coroutine

我试图在显示两个文字之间做出延迟。但它没有用。 代码是:

public class Class1: MonoBehaviour 
{
    public Text text1, text2;
    public bool inArea = false;

    private void Update () 
    {
       if (!inArea)
       {
           inArea     = true;
           text1.text = "";
           text2.text = "text2";
           StartCoroutine(timer());
           text2.text = "text3";            
      }
 }
 IEnumerator timer()
 {
     yield return new WaitForSeconds(100);
 }

我也试过WaitForSecondsRealTime()。它也无法正常工作。

1 个答案:

答案 0 :(得分:1)

我认为你有点误解了这一点。

一个协程将与其他东西(在你的情况下是text2.text = "text3")并行运行,如果它本身不是yield的延迟(为此你需要从协程调用你的协程或使用javascript将在内部执行此操作。

您必须将所有代码移动到应该受延迟影响的协同程序,如下所示:

private void Update () 
{
    if (!inArea)
    {
        inArea = true;
        StartCoroutine(timer());          
    }
}

IEnumerator timer()
{
    text1.text = "";
    text2.text = "text2";

    yield return new WaitForSeconds(100);
    text2.text = "text3";  
}

或者,您可以通过将Update更改为void Update来使IEnumerator Update成为协程。

在你目前的情况下,第一个应该没问题。

相关问题