自用unity动画相关方法,不废话,直接上干货!
/// <summary>
/// 播放Trigger类型参数动画
/// </summary>
/// <param name="animator">动画</param>
public static void PlayAmi(Animator animator, int index = 0)
{
AnimationClip clip = animator.GetCurrentAnimatorClipInfo(0)[index].clip;
animator.Play(clip.name, 0, 0);
animator.SetTrigger("IsPlay");
}
/// <summary>
/// 播放Bool类型参数动画
/// </summary>
/// <param name="animator">动画</param>
/// <param name="isPlay">状态</param>
public static void PlayAmi(Animator animator, bool isPlay, int index = 0)
{
AnimationClip clip = animator.GetCurrentAnimatorClipInfo(0)[index].clip;
animator.Play(clip.name, 0, 0);
animator.SetBool("IsPlay", isPlay);
}
/// <summary>
/// 播放Integer类型参数动画
/// </summary>
/// <param name="animator">动画</param>
/// <param name="num">整型数字</param>
public static void PlayAmi(Animator animator, int num, int index = 0)
{
AnimationClip clip = animator.GetCurrentAnimatorClipInfo(0)[index].clip;
animator.Play(clip.name, 0, 0);
animator.SetInteger("IsPlay", num);
}
/// <summary>
/// 跳过动画
/// </summary>
/// <param name="animator">动画控制器</param>
/// <param name="index">动画下标</param>
public static void SkipAmi(Animator animator, int index = 0)
{
AnimationClip clip = animator.GetCurrentAnimatorClipInfo(0)[index].clip;
animator.Play(clip.name, 0, 1);
}
/// <summary>
/// 获得animator下某个动画片段的时长方法
/// </summary>
/// <param animator="animator">Animator组件</param>
/// <param name="name">要获得的动画片段名字</param>
/// <returns></returns>
public static float GetAnimatorLength(Animator animator, string name)
{
//动画片段时间长度
float length = 0;
AnimationClip[] clips = animator.runtimeAnimatorController.animationClips;
Debug.Log(clips.Length);
foreach (AnimationClip clip in clips)
{
Debug.Log(clip.name);
if (clip.name.Equals(name))
{
length = clip.length;
break;
}
}
return length;
}