1)使用Invoke
private void BeginRace()
{
Invoke("WaveFlag", 0.5f);
Invoke("Beeps", 1.5f);
Invoke("CrowdBeginsCheer", 2f);
Invoke("CarsStartMoving", 2.2f);
}
2)使用协程
private void BeginRace()
{
StartCoroutine(RaceSequence());
}
private IEnumerator RaceSequence()
{
yield return new WaitForSeconds(.5f);
WaveFlag();
yield return new WaitForSeconds(1f);
Beeps();
yield return new WaitForSeconds(.5f);
CrowBeginsCheer();
yield return new WaitForSeconds(.2f);
CarsStartMoving();
}
你必须掌握这两个协同程序和调用。请尽可能简单地使用Invoke。当你刚刚学习Unity时,避免使用协同程序。 (高级essay上协同程序。)
3)“我需要等到前面的功能执行下列操作之一结束前”
a)每一个这些功能所需的必须是一个IEnumerator
private IEnumerator ExplodeCar()
{
..
}
private IEnumerator CrowdReaction()
{
..
}
private IEnumerator WinningCelebration()
{
..
}
b)打电话给他们一前一后,等待每个完成
private void Sequence()
{
StartCoroutine(Seq())
}
private IEnumerator Seq()
{
yield return StartCoroutine(ExplodeCar());
yield return StartCoroutine(CrowdReaction());
yield return StartCoroutine(WinningCelebration());
}
个
脚注
如果你想等到下一帧,用途:
yield return null;
,如果你有事情你想做的每帧的“叠”,只是做到这一点
void Update()
{
if (newItem = yourStack.Pop())
newItem();
}
如果你有一个“堆栈”你想要做的事情等待每个完成,
void Start()
{
StartCoroutine(YourStackLoop());
}
private IEnumerator stackLoop()
{
while(true)
{
if (newItem = yourStack.Pop())
yield return StartCoroutine(newItem());
else
yield return new;
}
}
注意,更新和协同程序是基本上是相同的事情,读,这项研究。
注意在这个例子中,使用你自己常用的Push/Pop(或者FIFO,或者你想要的任何东西)。如果不熟悉,请在这里搜索许多QA。
你能分享一些你的代码吗(比如你试图按顺序执行这些功能的地方)?你知道每个功能需要多长时间才能完成吗?在这种情况下,你可以抵消'Invoke()'延迟时间。 (这在电影的情况下更适用,其中起搏应该基于时间。) – Serlite
用代码@Serlite更新 –