我有两个协同程序。一个人有几个协同程序。第一个协程在第二个协程在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
范围内,我如何确保moveToPosition2
和moveToOriginal1
以及moveToOriginal1
和moveToOriginal2
之前rowEdit
和rowEditInit
完成?
答案 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
}
采用这种结构,时间如下:
ChangeLinePos
从Start()
DrawLineWithDuration
并停止执行ChangeLinePos
直到前者完成DrawLineWithDuration
完成后,如果isRepeating==true
,则代码同步启动MoveToOriginal1
和MoveToOriginal2
MoveToOriginal1
和MoveToOriginal2
都会启动MoveToPosition1
和MoveToPosition2
并等到最后两个结束。我离开了while
,因为它似乎受到了协同程序之外的某些内容的影响,因此请确保isRepeating
在需要时更改为false
,如果不是MoveToOriginal1
并且MoveToOriginal2
协同程序将一直启动,直到应用程序停止。