Unity animation组件
获取animation组件中animations里的某段动画
先上一个组件图
组件里我放了6个动画,从脚本里拿到对应的动画方法如下
Animation anim= Obj.GetComponent();//拿到animation组件
anim.clip = anim.GetClip(“2-1”);//获取对应名字的动画片段并赋值给当前片段 anim.clip
//然后就可以播放啦
anim.Play();
播放模式:
//正播
void NormalPlay()
{
Ani["AnimName"].time = Ani["AnimName"].clip.length;
Ani["AnimName"].speed = -1.0f;
Ani.CrossFade("AnimName");
}
//倒播
void ReversePlay()
{
Ani["AnimName"].time = 0;
Ani["AnimName"].speed = 1.0f;
Ani.CrossFade("AnimName");
}
//停止在某一帧
void StayInAFrame(float timer)
{
//设置停止在哪一帧
Ani["AnimName"].time = timer;
Ani["AnimName"].speed = 0f;
Ani.CrossFade("AnimName");
}
一些操作
public void PlayAnim()
{
anim[AnimClipName].speed = 1;
anim.Play(AnimClipName);
}
public void PourOnAnim()
{
if (anim[AnimClipName].time == 0)
{
anim[AnimClipName].time = anim[AnimClipName].length;
}
anim[AnimClipName].speed = -2;
anim.Play();
}
public void InitAnim()
{
anim.Play();
anim[AnimClipName].time = 0;
anim.Sample();
anim[AnimClipName].enabled = false;
}