我的菜单中有一些按钮,它们都有相同的动画。我要在最后一个按钮动画开始后大约50毫秒播放每个按钮的动画。我该怎么办?
注意:我是中级开发人员,可能自己就能解决这个问题,但是我从来没有以“那种同步”方式制作过多个动画,而且每次我第一次做某事时,我都不会“最佳”地做。另外,我在互联网上找不到我的问题的任何答案,所以我来到了这里。
答案 0 :(得分:1)
我不知道您的设置如何以及如何开始动画。
但是,假设您有一个使用方法YourButtonScript
的按钮脚本StartAnimation
,则可以在Coroutine中进行操作,例如
// reference all your buttons in the Inspector via drag&drop
public YourButtonScript[] buttons;
public void StartAnimations()
{
// Starts the Coroutine
StartCoroutine(AnimationsRoutine());
}
private IEnumerator AnimationsRoutine()
{
foreach(var button in buttons)
{
// however you start the animation on one object
button.StartAnimation();
// now wait for 50ms
// yield tells the routine to "pause" here
// let the frame be rendered and continue
// from this point in the next frame
yield return new WaitForSeconds(0.05f);
}
}
Unity中的协程就像临时的小Update
方法。通过使用默认的yield return null
,您可以告诉Unity此时离开Ienumerator,渲染该帧并在下一帧继续。然后有很多有用的工具,可让您yield
直到满足特定条件,如本示例WaitForSeconds
或WaitForSecondsRealtime