前言
当使用具体模型时,可以使模型动画来触发方法,即帧事件,(类似碰撞器,触发器的触发方法)。比如说一个角色的攻击动画,可以通过播放动画到某一帧来执行某个方法
通过Anmation图手动添帧事件
- 点击人物,然后在window找到Animtaion,之后找到要执行帧事件的动画。
void Attack1(){
Debug.Log ("Attack!!");
}
void Attack2(){
Debug.Log ("Attack2!!");
}
- 然后移动帧动画,找到要播放的帧,在其银白色处、灰色处?右击添加事件
添加对应(公有)方法,一个动画可以同时触发多个方法,
注意,如果添加的动画是只读的不能添加事件,那么要把这个动画ctrl/commond +d 复制出来,给物体重新添加动画,就可以添加事件了
通过代码添加帧事件
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);
}