【问题标题】:How to wait until animation is finished when using Animator?使用Animator时如何等待动画完成?
【发布时间】:2021-12-24 03:51:23
【问题描述】:

这是一个初学者问题,应该可以从第一个谷歌结果中找到,但我找不到。

我有这种使用Animator 播放动画的方法:

IEnumerator popDownAnimation(string c)
{
    animator = GetComponent<Animator>();
    animator.Play("Pop Down Animation");
    yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 && !animator.IsInTransition(0));
    animator.Play("New State");
}

不记得我从哪里得到的,但yield return new WaitUntil(() =&gt; animator.GetCurrentAnimatorStateInfo(0).normalizedTime &gt; 1 &amp;&amp; !animator.IsInTransition(0)); 不起作用。瞬间从“Pop Down Animation”跳转到“New State”,所以“Pop Down Animation”没有机会播放。

【问题讨论】:

    标签: unity3d animation yield-return


    【解决方案1】:

    试试这个

    IEnumerator popDownAnimation(string c)
    {
       animator = GetComponent<Animator>();
       animator.Play("Pop Down Animation");
       float animationLength = animator.GetCurrentAnimatorStateInfo(0).length;
       yield return new WaitForSecondsRealtime(animationLength);
       animator.Play("New State");
    }
    

    但是,就像提到的@NotVeryGoodAtThis 一样,我不会在代码中执行此操作,除非您有特定原因无法使用动画事件或使用动画器中的参数在动画状态之间进行转换。使用 Unity 的 Animator 窗口并使用状态、参数和过渡来设计动画将比通过代码更容易管理,尤其是当您的动画师变得更加复杂时。

    【讨论】:

      【解决方案2】:

      我建议使用动画事件来做任何你需要的事情。只需记住创建一个公共方法并在动画末尾添加动画事件即可。

      【讨论】:

      • 有没有办法在没有动画事件的情况下做到这一点?将其添加到我的项目中会使它变得混乱
      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      • 好吧,您可以创建一个协程来等待动画的持续时间,但总体而言,从我的观点来看,使用动画事件似乎是一个更好的主意。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      相关资源
      最近更新 更多