using System;
using System.Collections;
using UnityEngine;
namespace ZC
{
///
/// 动画辅助类
///
public static class AnimatorHelper
{
///
/// 动画播放结束后执行方法
///
/// 状态机
/// 播放完毕执行的方法
///
public static IEnumerator AnimatorOver(this Animator animator, Action action)
{
yield return new WaitForFixedUpdate(); //需要延时一帧,否则获取到的动画片段还没开始播放,就可能被判断为已经播放完成了
yield return new WaitUntil(() => animator.PlayIsOver());
action?.Invoke();
}
///
/// 根据名字播放对应动画片段,并在动画播放结束后执行指定方法
///
/// 状态机
/// 播放的动画名字
/// 播放完毕执行的方法
///
public static IEnumerator AnimatorOver(this Animator animator, string animatorClipName, Action action)
{
animator.Play(animatorClipName);
//yield return new WaitForFixedUpdate();//需要延时一帧,否则获取到的动画片段还没开始播放,就可能被判断为已经播放完成了(可能需要)
yield return new WaitUntil(() => animator.PlayIsOver());
Debug.Log(PlayIsOver(animator));
action?.Invoke();
}
///
/// 判断动画是否播放完毕
///
/// 状态机
///
public static bool PlayIsOver(this Animator animator)
{
var info = animator.GetCurrentAnimatorStateInfo(0);
if (info.normalizedTime >= 1.0f)
{
return true;
}
return false;
}
}
}