如何知道动画片段已经完成?

时间:2016-01-01 11:18:27

标签: c# animation unity3d

所以有两种方式 [我通过搜索获得,如果有其他方式请分享]来执行此操作。

  • isPlaying Property(例1)
  • 动画时间(示例2)

我不想以第一种方式使用合作例,我不想使用 第二个是使用时间,如果增加动画速度,那么时间将无法正常工作,从而影响代码。

示例1

   public class AnimationSequancePlayer : MonoBehaviour {


    public Animation animation; // The animation we want to play the clips in.
    public AnimationClip[] animationClips; // The animation clips we want to play in order.

    int _currentClipOffset;

    void Start()
    {
        foreach (AnimationClip clip in animationClips)
        {
            animation.AddClip(clip, clip.name); // Make sure the animation player contains all of our clips.
        }
        PlaySequence();
    }

    public void PlaySequence()
    {
        _currentClipOffset = 0; // Reset the index to start at the beginning.
        PlayNextClip();
    }

    public void StopSequence()
    {
        animation.Stop();
        StopAllCoroutines();
    }

    void PlayNextClip()
    {
        animation.Play(animationClips[_currentClipOffset].name); // Play the wanted clip
        if (_currentClipOffset != animationClips.Length)
        { // Check if it's the last animation or not.
            StartCoroutine(WaitForAnimationEnd(() => PlayNextClip())); // Listen for end of the animation to call this function again.
            _currentClipOffset++; // Increase index for next time;
        }
    }

    IEnumerator WaitForAnimationEnd(Action onFinish)
    {
        while (animation.isPlaying)
        { // Check if the animation is playing or not
            yield return null;
        }
        if (onFinish != null) { onFinish(); } // Call the function give in parameter.
    }
}

示例2

 if (GetComponent<Animation>()["Move Crane"].time >= 3f)
        {
         ///logic after animation reached at specified time specified time

        }

1 个答案:

答案 0 :(得分:0)

这段简短的代码片段让我能够做到这一点。注释中给出的代码描述也是如此。对未来的用户很有帮助。

O(n)
相关问题