如何确保一个协程在另一个协同程序运行之前完成?

时间:2018-03-10 14:07:19

标签: unity3d coroutine

我有两个协同程序。一个人有几个协同程序。第一个协程在第二个协程在Start()方法内运行之前不等待第二个协同程序完成。请参阅下面的代码。

void Start() {
    StartCoroutine(DrawLineWithDuration (3.0f));
    StartCoroutine (changeLinePos (2.0f));
}

IEnumerator DrawLineWithDuration (float duration)  {

    lr.positionCount = 1;
    lr.SetPosition(0, GetPoint(0));

    float waitDur = duration / numPoints;

    for (int i = 1; i < numPoints + 1; i++) {
        float t = i / (float)numPoints;
        lr.positionCount = i+1;
        lr.SetPosition(i, GetPoint(t));

        yield return new WaitForSeconds(waitDur);
    }
}

IEnumerator moveToPosition1(Vector3 toPosition, float duration)
{

    float counter = 0;

    Vector3 startPos = points[1];

    while (counter < duration)
    {
        counter += Time.deltaTime;
        points[1] = Vector3.Lerp(startPos, toPosition, counter / duration);
        yield return null;
    }

}

IEnumerator moveToPosition2(Vector3 toPosition, float duration)
{
    float counter = 0;
    Vector3 startPos = points[2];

    while (counter < duration)
    {
        counter += Time.deltaTime;
        points[2] = Vector3.Lerp(startPos, toPosition, counter / duration);
        yield return null;
    }
}


IEnumerator moveToOriginal1(Vector3 toPosition, float duration)
{

    float counter = 0;

    Vector3 startPos = points[1];

    while (counter < duration)
    {
        counter += Time.deltaTime;
        points[1] = Vector3.Lerp(startPos, toPosition, counter / duration);
        yield return null;
    }

}

IEnumerator moveToOriginal2(Vector3 toPosition, float duration)
{
    float counter = 0;
    Vector3 startPos = points[2];

    while (counter < duration)
    {
        counter += Time.deltaTime;
        points[2] = Vector3.Lerp(startPos, toPosition, counter / duration);
        yield return null;
    }
}


IEnumerator changeLinePos(float waitTime)   {

    yield return new WaitForSeconds(waitTime);

    while (isRepeating) {
        StartCoroutine (moveToPosition1 (newPos1, moveDuration));
        StartCoroutine (moveToPosition2 (newPos2, moveDuration));
        yield return new WaitForSeconds (betweenPosMoveDelay);

        StartCoroutine (moveToOriginal1 (oriPos1, moveDuration));
        StartCoroutine (moveToOriginal2 (oriPos2, moveDuration));

        yield return new WaitForSeconds (betweenPosMoveDelay);
    }

}

Start()内如何确保StartCoroutine(DrawLineWithDuration (3.0f));StartCoroutine (changeLinePos (2.0f));开始前的给定时间内完成changeLinePos(float waitTime)。同样在moveToPosition1范围内,我如何确保moveToPosition2moveToOriginal1以及moveToOriginal1moveToOriginal2之前rowEditrowEditInit完成?

1 个答案:

答案 0 :(得分:0)

实际上非常简单,您可以yield return StartCoroutine(DrawLineWithDuration (3.0f));作为changeLinePos中的第一行,如下所示:

void Start() {    
    StartCoroutine (ChangeLinePos (2.0f));
}

IEnumerator ChangeLinePos(float waitTime)   {
    yield return StartCoroutine(DrawLineWithDuration (3.0f));
    while (isRepeating) {
        StartCoroutine (MoveToOriginal1 (oriPos1, moveDuration));
        StartCoroutine (MoveToOriginal2 (oriPos2, moveDuration));
        yield return new WaitForSeconds (betweenPosMoveDelay);
    }
}

IEnumerator MoveToOriginal1(Vector3 toPosition, float duration) {
    yield return StartCoroutine (MoveToPosition1 (newPos1, moveDuration));
    //Rest of MoveToOriginal1 code
}

IEnumerator MoveToOriginal2(Vector3 toPosition, float duration) {
    yield return StartCoroutine (MoveToPosition2 (newPos2, moveDuration));
    //Rest of MoveToOriginal1 code
}

采用这种结构,时间如下:

  • ChangeLinePosStart()
  • 开始
  • 第一行开始DrawLineWithDuration并停止执行ChangeLinePos直到前者完成
  • DrawLineWithDuration完成后,如果isRepeating==true,则代码同步启动MoveToOriginal1MoveToOriginal2
  • MoveToOriginal1MoveToOriginal2都会启动MoveToPosition1MoveToPosition2并等到最后两个结束。

我离开了while,因为它似乎受到了协同程序之外的某些内容的影响,因此请确保isRepeating在需要时更改为false,如果不是MoveToOriginal1并且MoveToOriginal2协同程序将一直启动,直到应用程序停止。