在继续 for 循环之前等待事件完成

Waiting for an event to finish before continuing in for loop

所以我在这部分卡住了一段时间。 我正在写一个项目,我有大约 12 轮相同的测试。 所以基本上我想要相同的功能,只是每次都使用不同的头像。

有一个事件触发,当它触发时调用名为 FinishTest 的函数 我希望仅当 FinishTest 被触发并完成时循环才继续。

for (int i = 0;  i < numberOfAvatars; i++) {
    StartTest(avatars[i]); // This is not really relevent to the question
    //Wait until the FinishTest event is finished before continuing the loop
}

当我的模型和某个对象之间存在触发器时,FinishTest 被激活。 FinishTest 不可能在 startTest 之前完成,因为 StartTest 基本上会实例化模型,仅此而已,所以我不认为必须有某种方法来确保 StartTest 在 FinishTest 之前完成。

函数内部的内容并不重要,但为什么不重要:

private void  StartTest(GameObject avatar) {
        Instantiate(avatar, new Vector3(2f,2f,2f), Quaterion.identity));
}

private void FinishTest()
{
    testsResults[testNumber] = new TestResult(avatarsName, holdingObject); 
       testsResults[testNumber].setTimeUntilAvatarGotShot(string.Format("{0:F2}",timer) 

}

感谢您的帮助。

bool TestFinished;
IEnumerator RunTest() {
    for (int i = 0;  i < numberOfAvatars; i++) {
        TestFinished = false;
        StartTest(avatars[i]); // This is not really relevent to the question

        //Wait until the FinishTest event is finished before continuing the loop
        while (!TestFinished) yield return null;
    }
}

private void FinishTest()
{
    testsResults[testNumber] = new TestResult(avatarsName, holdingObject); 
    testsResults[testNumber].setTimeUntilAvatarGotShot(string.Format("{0:F2}",timer);
    TestFinished = true;
}

然后你 StartCoroutine(RunTest()) 在你的代码中的某个地方。