版权声明:大家好,我是笨笨,笨笨的笨,笨笨的笨,转载请注明出处,谢谢! https://blog.csdn.net/jx520/article/details/83341862
//顺便说一句,U3D的动画编辑器有BUG,添加关键帧事件后选了函数会说不支持,其实是可以用。运行下看结果来定吧。
//大家好我是笨笨,笨笨的笨,笨笨的笨,谢谢!
//20150810 by Jerryjin
using UnityEngine;
using System.Collections;
public class KeyframeEvent : MonoBehaviour {
Animator anim;
AnimationEvent t_animEvent;
void Start () {
anim = GetComponent<Animator>();
UIEventListener.Get(this.gameObject).onClick = ButtonClick;//添加监听事件
}
void testEventPrint(float i){
print("脚本添加的关键帧事件! : " + i);
//用速度控制播放和暂停
anim.speed = 0;
}
public void KeyframeEventCallback()
{
print("帧事件,回调成功! " + Time.deltaTime);
}
void ButtonClick(GameObject go){
// print("点击事件!" );
//用速度控制播放和暂停
anim.speed = 1;
//动态添加关键帧事件,搞了半天start 下面好像数据没加载完。所以总是出错
AnimationInfo[] t_animInfo = anim.GetCurrentAnimationClipState(0);
//AnimationClip t_clip = t_animInfo[0];
if (t_animInfo.Length > 0) {
t_animEvent = new AnimationEvent();
t_animEvent.functionName = "testEventPrint";//回调函数的名字
t_animEvent.floatParameter = 9527;//传float类型参数
t_animEvent.time = 1;//K帧的位置秒。
t_animInfo[0].clip.AddEvent(t_animEvent);//事件添加给动画clip
}
// //播放动画
// //查找对象 "path" 获取它的 Animator 组件,然后播放动画,"ani_01"是 Animator界面中的 satate 状态名称。
GameObject.Find("path").GetComponent<Animator>().Play ("ani_01",0,.5f);//play("状态名",层索引,标准化时间0%到100%)
// //播放暂停切换
// if(anim.speed == 1)
// {
// anim.speed = 0;
// }else
// {
// anim.speed = 1;
// }
}
}