/****************************************************
文件:Test01.cs
作者:RAN
邮箱: 1647707119@qq.com
日期:2020/06/09 16:29
功能:测试
*****************************************************/
using UnityEngine;
public class Test01 : MonoBehaviour
{
public Animator anim;
void Update()
{
if(Input.GetKeyDown(KeyCode.A))
{
AnimatorManager.Instance.PlayAnim(anim, 1f);//一倍速播放
}
else if(Input.GetKeyDown(KeyCode.B))
{
AnimatorManager.Instance.PlayAnim(anim, 2f);//二倍速播放
}
else if(Input.GetKeyDown(KeyCode.C))
{
AnimatorManager.Instance.PlayAnim(anim, -1f);//一倍速倒放
}
else if(Input.GetKeyDown(KeyCode.D))
{
AnimatorManager.Instance.PlayAnim(anim, -2f);//二倍速倒放
}
else if(Input.GetKeyDown(KeyCode.Space))
{
AnimatorManager.Instance.StopAnim(anim);//停止anim动画
}
}
}
/****************************************************
文件:AnimatorManager.cs
作者:RAN
邮箱: 1647707119@qq.com
日期:2020/6/8 16:47:36
功能:控制Animator动画倒放、暂停
*****************************************************/
using UnityEngine;
public class AnimatorManager : MonoBehaviour
{
private static AnimatorManager _instance;
public static AnimatorManager Instance
{
get
{
return _instance;
}
}
void Awake()
{
_instance = this;
}
//播放动画anim(速度为speed)
public void PlayAnim(Animator anim, float speed)
{
if(anim!=null)
{
anim.enabled = true;
AnimatorClipInfo[] temps = anim.GetCurrentAnimatorClipInfo(0);
AnimatorClipInfo clipInfo = new AnimatorClipInfo();
if(temps.Length>0)
{
clipInfo = temps[0];//获取动画clip
}
anim.StartPlayback();
anim.speed = speed;
anim.Play(clipInfo.clip.name, 0, speed < 0 ? 1 : 0);
}
}
//暂停动画anim
public void StopAnim(Animator anim)
{
AnimatorClipInfo[] temps = anim.GetCurrentAnimatorClipInfo(0);
AnimatorClipInfo clipInfo = new AnimatorClipInfo();
if(temps.Length>0)
{
clipInfo = temps[0];
}
anim.Play(clipInfo.clip.name, 0, 0);
anim.speed = 0;
}
}