我的脚本中有一个函数调用5个命令,如下所示:
public class ScrambleScript : MonoBehaviour {
public RRotation ScrambleR; // reference for R rotation
public LRotation ScrambleL; // reference for L rotation
public FRotation ScrambleF; // reference for F rotation
public DRotation ScrambleD; // reference for D rotation
public Rotation ScrambleU; // reference for U rotation
void ScrambleCube() {
ScrambleR.ScrambleRNotation();
ScrambleU.ScrambleUNotation();
ScrambleL.ScrambleLNotation();
ScrambleF.ScrambleFNotation();
ScrambleD.ScrambleDNotation();
}
}
一旦我在Update()
中调用了这个函数,命令就会被每一个帧调用,但是我希望能够使用另一个延迟函数来在每个函数调用之间暂停1秒,所以它看起来如下所示:
void ScrambleCube() {
ScrambleR.ScrambleRNotation();
// waits 1 second here.
ScrambleU.ScrambleUNotation();
// waits 1 second here.
ScrambleL.ScrambleLNotation();
// waits 1 second here.
ScrambleF.ScrambleFNotation();
// waits 1 second here.
ScrambleD.ScrambleDNotation();
}
我尝试过使用IEnumerator
函数,但如果我粘贴每个函数调用之间的延迟,我似乎无法做到这一点,如上面所示。还有别的办法吗?还是我不正确地使用了IEnumerator
?
发布于 2018-03-19 21:41:56
您可以使用统一中的一个原则,称为协同使用( IEnumerators
)。
public IEnumerator ScrambleCube () {
ScrambleR.ScrambleRNotation();
yield return new WaitForSeconds(1); // This pauses the execution of
// the function for 1 second
ScrambleU.ScrambleUNotation();
// etc...
}
请注意,您不能简单地按其方法名称调用coroutines,您必须使用一个名为MonoBehaviours
的StartCoroutine
方法。
StartCoroutine(ScrambleCube());
请注意,启动协同线不会延迟发出StartCoroutine
调用的方法的执行。
https://stackoverflow.com/questions/49372589
复制相似问题