一,Animator扩展方法。
public static class AnimatorHelper
{
public static bool CheckAnimFinish(this Animator anim,float checkTime = 1)
{
return anim.GetCurrentAnimatorStateInfo(0).normalizedTime >= checkTime && !anim.IsInTransition(0);
}
public static bool CheckAnimFinish(this Animator anim,string animName, float checkTime = 1)
{
return anim.CheckAnimFinish(checkTime) && anim.GetCurrentAnimatorStateInfo(0).IsName(animName);
}
public static bool CheckAnimTransitionComplete(this Animator anim, string animName)
{
return !anim.IsInTransition(0) && anim.GetCurrentAnimatorStateInfo(0).IsName(animName);
}
public static bool CheckAnimFinishWithoutTransition(this Animator anim, string animName, float checkTime = 1)
{
return anim.GetCurrentAnimatorStateInfo(0).normalizedTime >= checkTime && anim.GetCurrentAnimatorStateInfo(0).IsName(animName);
}
public static bool CheckAnimSwitchToStatus(this Animator anim, string animName)
{
return anim.GetCurrentAnimatorStateInfo(0).IsName(animName);
}
}
}
二 使用Any State 然判断条件复原(StateMachineBehaviour)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Animations;
namespace Assets.Scripts.UI.Battle.Anim
{
public class AnimatorBoolRecovery:StateMachineBehaviour
{
public string RecoveryName;
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex,
AnimatorControllerPlayable controller)
{
//Debug.Log("AnimatorBoolRecovery:"+ RecoveryName+" name:"+ animator.name);
animator.SetBool(RecoveryName,false);
}
}
}
详细地址:https://blog.csdn.net/u010019717/article/details/44760683
三.简单框架
退场动画——进场动画的切换
namespace HotFix_Project.Script.UI.Town
{
public interface ITownPanel
{
///
/// 状态重制
///
void Reset();
///
/// 播放进场动画
///
///
void PlayOpenAnim(Action OnPlayFinish);
///
/// 播放退场动画
///
///
void PlayCloseAnim(Action OnPlayFinish);
///
/// 获取当前GameObject
///
///
GameObject GetCurrGameObject();
///
/// 激活面板
///
void SetActive(bool isActive);
}
}
public static IEnumerator WaitPanelAnimFinish(Animator anim, string animName, Action OnPlayFinish)
{
Debug.Log("WaitPanelAnim Start:"+ animName);
//等待切换至开场动画状态
while (!anim.CheckAnimSwitchToStatus(animName))
{
yield return new WaitForEndOfFrame();
}
//等待开场动画播放完成
while (!anim.CheckAnimFinish() && anim.GetCurrentAnimatorStateInfo(0).IsName(animName))
{
yield return new WaitForEndOfFrame();
}
if (OnPlayFinish != null)
{
OnPlayFinish();
}
Debug.Log("WaitPanelAnim Finish:" + animName);
}