原创文章如需转载请注明:转载自 脱莫柔Unity3D学习之旅 QQ群:【Unity3D(AR/VR) 334163814】【Unity3D(游戏) 119706192】 本文链接地址:Unity3D 动画回调
注册Animation回调
public class test_AddAnimationEvent : MonoBehaviour
{
Animation anim;
void Start () {
anim = GetComponent<Animation>();
AddEvent(1, "testLog");
}
void AddEvent(float EventTime, string funcname)
{
AnimationEvent auidoEvent = new AnimationEvent();
auidoEvent.time = EventTime;
auidoEvent.functionName = funcname;
anim.clip.AddEvent(auidoEvent);
//注:同一动画能被多次注册回调。
}
int n = 0;
public void testLog()
{
n++;
Debug.Log("n=" + n + ", time=" + DateTime.Now.Ticks / 1000000.0);
}
}
注册Animtor回调
1.先创建个脚本,用来执行回调事件。
public class test_animatorEvent : MonoBehaviour
{
int n = 0;
public void CallNull()
{
n++;
Debug.Log("n=" + n + ", time=" + DateTime.Now.Ticks / 1000000.0);
}
int i = 0;
public void CallInt(int _int)
{
i++;
Debug.Log(_int + " | i=" + i + ", time=" + DateTime.Now.Ticks / 1000000.0);
}
int i_s = 0;
public void CallIntStr(int _int,string _str)
{
i_s++;
Debug.Log(_int + "&" + _str + " | i_s=" + i_s + ", time=" + DateTime.Now.Ticks / 1000000.0);
}
}
2.找到要绑定回调的动画,选择其文件。
3.找准添加回调时的时机,准备添加动画回调的方法。
4.点击
添加回调方法。
添加完成后,是这样的,这个还是可以再拖动调整回调时机。
到此,动画回调就注册完了。
5.投入使用,将第一步写好的脚本挂在Animator同级的gameobject上。
运行结果:
6.再来试试带参数的。
运行结果: