using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace ChivaXR { /// /// UnityAnimation 工具 /// public class AnimationToolkit { /// /// 设置动画状态(Normalized) /// /// /// /// /// public static void SetAnimationNormalizedToPose(Animation _animation, float _normalizedTime) { AnimationState _aniState = _animation[_animation.clip.name]; _aniState.enabled = true; _aniState.weight = 1; _aniState.normalizedTime = _normalizedTime; _animation.Sample(); _aniState.enabled = false; } /// /// 设置动画状态(Normalized) /// /// /// /// /// public static void SetAnimationNormalizedToPose(Animation _animation, string _aniName, float _normalizedTime) { AnimationState _aniState = _animation[_aniName]; _aniState.enabled = true; _aniState.weight = 1; _aniState.normalizedTime = _normalizedTime; _animation.Sample(); _aniState.enabled = false; } /// /// 设置动画状态(Normalized)并播放 /// /// /// /// /// public static void SetAnimationNormalizedToPlay(Animation _animation, string _aniName, float _normalizedTime, float _speed = 1, Action finishedCallBack = null) { AnimationState _aniState = _animation[_aniName]; _aniState.enabled = true; _aniState.speed = _speed; _aniState.weight = 1; _aniState.normalizedTime = _normalizedTime; _animation.Sample(); _aniState.enabled = false; _animation.Play(_aniName); if (finishedCallBack != null) { //协程等待动画播放完毕时长,执行委托 SetIEnumeratorActionCallBackByTime(finishedCallBack, (1 - _normalizedTime) * _aniState.length / _speed); } } /// /// 获取动画播放时长 /// /// /// /// /// public static float GetAnimationPlayTime(Animation _animation, string _aniName, float _speed = 1) { AnimationState _aniState = _animation[_aniName]; return _aniState.length / _speed; } /// /// 播放动画 /// /// /// /// public static void PlayAnimationByClip(Animation _animation, string _aniName, float speed = 1, Action finishedCallBack = null) { _animation.clip = _animation[_aniName].clip; _animation[_aniName].speed = speed; _animation.Play(_aniName); if (finishedCallBack != null) { //协程等待动画播放完毕时长,执行委托 SetIEnumeratorActionCallBackByTime(finishedCallBack, _animation[_aniName].length / speed); } } /// /// 协程等待时间执行委托 /// /// /// public static void SetIEnumeratorActionCallBackByTime(Action callBack, float time) { AnimationCoroutine.Instance.StartCoroutine(UnityAcitionCallBackByTime(callBack, time)); } static IEnumerator UnityAcitionCallBackByTime(Action callBack, float time) { yield return new WaitForSeconds(time); callBack?.Invoke(); } } }