当使用具体模型时,可以使模型动画来触发方法,即帧事件,(类似碰撞器,触发器的触发方法)。比如说一个角色的攻击动画,可以通过播放动画到某一帧来执行某个方法。

另外新动画也可以使用Animation做帧事件

帧事件一般用射线或相交球配合使用,做动画检测

通过Anmation图手动添帧事件

1. 点击人物,然后在window找到Animtaion,之后找到要执行帧事件的动画。

Unity3d动画播放和帧事件之美_事件绑定

void Attack1(){
Debug.Log ("Attack!!");

void Attack2(){
Debug.Log ("Attack2!!");

2. 然后移动帧动画,找到要播放的帧,在其银白色处、灰色处?右击添加事件

Unity3d动画播放和帧事件之美_帧动画_02

3. 添加对应(公有)方法,一个动画可以同时触发多个方法,

Unity3d动画播放和帧事件之美_触发器_03

4. 注意,如果添加的动画是只读的不能添加事件,那么要把这个动画ctrl/commond +d 复制出来,给物体重新添加动画,就可以添加事件了。

注意帧事件不能添加而不设置方法,否则会报错

AnimationEvent has no function name specified!

虽然不影响游戏内容。不过也不要只添加事件而不设置不设置事件把。

通过代码添加帧事件

private Animation _ani;
void Awake(){
_ani = GetComponent<Animation> ();
//获取动画片段
AnimationClip attack = _ani.GetClip ("Attack");
//创建事件
AnimationEvent attackEvent = new AnimationEvent ();
//给事件绑定方法
attackEvent.functionName = "NewAttack";
//在某时间触发事件
attackEvent.time = 1.7f;
//添加帧事件
attack.AddEvent (attackEvent);