unity5, animation event

一,给导入的fbx动画添加animation event:

如下图,在双击状态机中的idle状态,打开右面的面板,点开Events项会出现一个时间轴,点击下方播放器的播放按钮或者拖动播放器时间轴上的红线,Events时间轴上的红线会随之移动,当红线移动到合适的时间点,点击Events时间轴左边的加号便可添加一个动画事件。比如我们在idle动画播放到一半的位置添加一个事件,选中刚添加的事件会弹出编辑框,我们将Function名称改为idleHalfEvent。

此时如果Hierarchy中选中Player,并打开Animation窗口(菜单->Window->Animation),clip选idle(显示为idle (Read-Only)),缩放右边窗口直到能看到整个动画轴,可以看到前面添加的idleHalfEvent事件(函数)也出现在这里。

注:其实添加animation event也可以不走前面的步骤,直接在这个animation窗口中添加亦可。

不过此时显示为idleHalfEvent (Function Not Supported)。因此现在还没有实现idleHalfEvent这个函数。

为Player添加脚本idleHalfEventScript.cs:

using UnityEngine;
using System.Collections;

public class idleHalfEventScript : MonoBehaviour {

    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
    }
    public void idleHalfEvent(){
        Debug.Log ("event message: idle half!");

    }
}

然后再回去看刚才的animation窗口,就会发现idleHalfEvent事件不再提示“Function Not Supported”了,如下图:

至此animation event添加完成,运行程序,可以看到每次当player的idle动画播放到一半的时候控制台会输出;

event message: idle half!
UnityEngine.Debug:Log(Object)

二,给直接在unity里制作动画添加animation event:

直接在animation窗口中添加即可。

原文地址:https://www.cnblogs.com/wantnon/p/4515448.html