2016-01-01 52 views
0

所以有两种方式 [我通过搜索得到,如果有任何其他方式请分享]做到这一点。如何知道动画剪辑已完成?

  • IsPlaying模块属性(例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 

     } 
+0

您是否试过_动画Events_? – Kay

+0

nope这是什么 –

+0

我正在使用max动画不unity3d。 –

回答

0

这种短代码段能够我做到这一点。也在评论中给出了代码的描述。有助于未来的用户。

bool animationClipPlaying = false; 
void Update() 
{ 
    if (GetComponent<Animation>().IsPlaying(clipNameCompare)) 
    { 
     //as animation start to play true the bool 
     animationClipPlaying = true; //true this for comparsion 
    } 
    //if animation is not playing and the bool is true then, animation finished 
    else if (!GetComponent<Animation>().IsPlaying(clipNameCompare) && animationClipPlaying) 
    { 
     Debug.Log(clipNameCompare : " has finished"); 
     //False so that this condition run only once 
     aanimationClipPlaying = false; 
    } 
} 
相关问题