Unity - C# - 快速排序算法 - 协程
Unity - C# - quick sort alghoritm - courutines
我正在用 Unity 编写一个应用程序,它将根据立方体的高度对立方体进行排序。我坚持使用快速排序算法。
代码工作正常,直到最后两行是协程而不是常规方法。不幸的是,他们都 运行 并行,这是错误的...我正在寻找一种方法来构建我的代码 运行 当时只有一个协程。
IEnumerator QuickSort(GameObject[] unsortedList, int left, int right)
{
if (left < right)
{
GameObject temp;
Vector3 tempPosition;
float pivotValue = unsortedList[right].transform.localScale.y;
int i = left - 1;
for (int j = left; j < right; j++)
{
if (unsortedList[j].transform.localScale.y < pivotValue)
{
i++;
temp = unsortedList[i];
unsortedList[i] = unsortedList[j];
unsortedList[j] = temp;
if (i != j)
{
yield return new WaitForSeconds(1);
tempPosition = unsortedList[i].transform.localPosition;
LeanTween.moveLocalX(unsortedList[i], unsortedList[j].transform.localPosition.x, 1f);
LeanTween.moveLocalZ(unsortedList[i], -3f, 0.5f).setLoopPingPong(1);
LeanTween.moveLocalX(unsortedList[j], tempPosition.x, 1f);
LeanTween.moveLocalZ(unsortedList[j], 3f, 0.5f).setLoopPingPong(1);
}
}
}
yield return new WaitForSeconds(1);
temp = unsortedList[i + 1];
unsortedList[i + 1] = unsortedList[right];
unsortedList[right] = temp;
tempPosition = unsortedList[i + 1].transform.localPosition;
LeanTween.moveLocalX(unsortedList[i + 1], unsortedList[right].transform.localPosition.x, 1f);
LeanTween.moveLocalZ(unsortedList[i + 1], -3f, 0.5f).setLoopPingPong(1);
LeanTween.moveLocalX(unsortedList[right], tempPosition.x, 1f);
LeanTween.moveLocalZ(unsortedList[right], 3f, 0.5f).setLoopPingPong(1);
int pivotIndex = i + 1;
StartCoroutine(QuickSort(cubes, 0, pivotIndex - 1));
StartCoroutine(QuickSort(cubes, pivotIndex + 1, cubes.Length - 1));
}
}
如果您只是使用 StartCoroutine
,那么您 运行 将后续协程作为新的 并行 例程。
如果你真的想等到这些递归例程 完成 在 运行 下一个之前,那么你必须 yield
它们就像
// This already "runs" this IEnumerator and at the same time waits until it is done
yield return QuickSort(cubes, 0, pivotIndex - 1);
yield return QuickSort(cubes, pivotIndex + 1, cubes.Length - 1);
你也根本不需要 StartCoroutine
。仅当您希望能够在单独启动的例程上使用 StopCoroutine
时才需要这样做。
不过,老实说,我个人不会在 LeanTween 和协程之间混用太多 ;)
我正在用 Unity 编写一个应用程序,它将根据立方体的高度对立方体进行排序。我坚持使用快速排序算法。
代码工作正常,直到最后两行是协程而不是常规方法。不幸的是,他们都 运行 并行,这是错误的...我正在寻找一种方法来构建我的代码 运行 当时只有一个协程。
IEnumerator QuickSort(GameObject[] unsortedList, int left, int right)
{
if (left < right)
{
GameObject temp;
Vector3 tempPosition;
float pivotValue = unsortedList[right].transform.localScale.y;
int i = left - 1;
for (int j = left; j < right; j++)
{
if (unsortedList[j].transform.localScale.y < pivotValue)
{
i++;
temp = unsortedList[i];
unsortedList[i] = unsortedList[j];
unsortedList[j] = temp;
if (i != j)
{
yield return new WaitForSeconds(1);
tempPosition = unsortedList[i].transform.localPosition;
LeanTween.moveLocalX(unsortedList[i], unsortedList[j].transform.localPosition.x, 1f);
LeanTween.moveLocalZ(unsortedList[i], -3f, 0.5f).setLoopPingPong(1);
LeanTween.moveLocalX(unsortedList[j], tempPosition.x, 1f);
LeanTween.moveLocalZ(unsortedList[j], 3f, 0.5f).setLoopPingPong(1);
}
}
}
yield return new WaitForSeconds(1);
temp = unsortedList[i + 1];
unsortedList[i + 1] = unsortedList[right];
unsortedList[right] = temp;
tempPosition = unsortedList[i + 1].transform.localPosition;
LeanTween.moveLocalX(unsortedList[i + 1], unsortedList[right].transform.localPosition.x, 1f);
LeanTween.moveLocalZ(unsortedList[i + 1], -3f, 0.5f).setLoopPingPong(1);
LeanTween.moveLocalX(unsortedList[right], tempPosition.x, 1f);
LeanTween.moveLocalZ(unsortedList[right], 3f, 0.5f).setLoopPingPong(1);
int pivotIndex = i + 1;
StartCoroutine(QuickSort(cubes, 0, pivotIndex - 1));
StartCoroutine(QuickSort(cubes, pivotIndex + 1, cubes.Length - 1));
}
}
如果您只是使用 StartCoroutine
,那么您 运行 将后续协程作为新的 并行 例程。
如果你真的想等到这些递归例程 完成 在 运行 下一个之前,那么你必须 yield
它们就像
// This already "runs" this IEnumerator and at the same time waits until it is done
yield return QuickSort(cubes, 0, pivotIndex - 1);
yield return QuickSort(cubes, pivotIndex + 1, cubes.Length - 1);
你也根本不需要 StartCoroutine
。仅当您希望能够在单独启动的例程上使用 StopCoroutine
时才需要这样做。
不过,老实说,我个人不会在 LeanTween 和协程之间混用太多 ;)