Unity脚本-代码没有顺序执行.如何关闭多线程?



代码不按顺序执行是什么意思?我有以下代码:

IEnumerator Game(){
// do something
yield return new WaitForSecondsRealtime(10f);
// do something else
Debug.Log("game method")
}
void Start(){
// some code ..
StartCoroutine(Game());
// other code ..
Debug.Log("end of the code");
Debug.Log("something else");
}

出于某种原因,我将在控制台中输入:

end of the code
something else
game method

我猜这是与多线程有关的东西。我如何关闭它(我如何避免它)?

按顺序执行

你的代码应该是这样的:

void Start()
{  
StartCoroutine(Game());
}
IEnumerator Game(){
// do something
yield return new WaitForSecondsRealtime(10f);
// do something else
Debug.Log("game method")
ExecuteLast();
}
void ExecuteLast()
{
Debug.Log("end of the code");
Debug.Log("something else");
}

最新更新