【问题标题】:Check if animation has finished playing?检查动画是否播放完毕?
【发布时间】:2015-11-04 23:38:13
【问题描述】:

如何检查特定动画是否已在 Unity 中播放完毕,然后执行操作? [C#] 我没有使用动画师。

【问题讨论】:

  • 不确定您添加的“我没有使用动画师”如何使 Andrea 的响应无效,因为 IsPlayingAnimation 的属性,而不是 Animator...?

标签: c# unity3d unity5


【解决方案1】:

发件人:http://answers.unity3d.com/questions/52005/destroy-game-object-after-animation.html

从动画编辑器执行动作...

-创建一个具有简单公共函数的脚本,该函数将销毁对象。例如

public class Destroyable : MonoBehaviour 
{ 
    public void DestroyMe() 
    { 
        Destroy(gameObject); 
    } 
}

-将该脚本添加到要销毁的动画对象中。

-在动画编辑器中,将动画滑动条移动到动画的末尾。

-使用动画工具栏中的“添加事件”按钮

-从“编辑动画事件”对话框的功能下拉菜单中选择“DestroyMe”。

-现在您的动画应该播放,运行“DeleteMe”功能,并销毁对象/执行您的操作。

这个方法我用过几次,对动画中的某些东西很方便:)

【讨论】:

    【解决方案2】:

    您应该检查Animation.IsPlaying 的值。

    来自文档:

    using UnityEngine;
    using System.Collections;
    
    public class ExampleClass : MonoBehaviour {
        public Animation anim;
        void Start() {
            anim = GetComponent<Animation>();
        }
        void OnMouseEnter() {
            if (!anim.IsPlaying("mouseOverEffect"))
                anim.Play("mouseOverEffect");
    
        }
    }
    

    【讨论】:

    • @user5497945 你能解释一下为什么吗?这似乎是一个合理的答案,如果您出于某种正当理由无法使用它,请编辑您的问题并添加原因。
    【解决方案3】:

    正如 Andrea 在他的帖子中所说:Animation-IsPlaying 几乎是您所需要的,因为您不使用 Animator。检查Animation 看看你可以使用的其他甜蜜的东西。

    using UnityEngine;
    using UnityEngine.Collections;
    
    public class ExampleClass : MonoBehaviour 
    {
        Animation anim;
        void Start()
    {
       anim = GetComponent<Animation>();
    }
    
    //In update or in another method you might want to check
    if(!anim.isPlaying("StringWithAnimationClip") //or anim.clip.name
       //Do Something
    }
    

    您也可以使用 anim.Stop(); 强制停止动画

    现在你评论说你不想使用 isPlaying() 所以如果你能详细说明我会编辑我的帖子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 2017-07-12
      • 2014-07-04
      相关资源
      最近更新 更多