From 8d4a4c7c781de11ba3735e8ffb435b23c483af7a Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 5 Aug 2021 19:34:04 +0800 Subject: *box --- Assets/Scripts/Unit/Component/UnitAnimation.cs | 43 ++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) (limited to 'Assets/Scripts/Unit/Component/UnitAnimation.cs') diff --git a/Assets/Scripts/Unit/Component/UnitAnimation.cs b/Assets/Scripts/Unit/Component/UnitAnimation.cs index 18439efb..8a8b79a6 100644 --- a/Assets/Scripts/Unit/Component/UnitAnimation.cs +++ b/Assets/Scripts/Unit/Component/UnitAnimation.cs @@ -28,7 +28,7 @@ public class AnimatorLayerInfo string name = clip.clip.name; if (m_AnimationData != null && m_AnimationData.animationName == name) return m_AnimationData; - string path = folder + "AnimationData/" + name + ".asset" ; + string path = folder + "AnimationData/" + name + ".asset" ; // 要注意这里使用名字区别的,因为最终每个角色的动画都会在一个目录下面 #if UNITY_EDITOR m_AnimationData = AssetDatabase.LoadAssetAtPath(path); #endif @@ -56,6 +56,8 @@ public class AnimatorLayerInfo } } + private int preStateHash = -1; + // 当前正在播放和融合的片段信息 public AnimatorClipInfo[] clipInfo { @@ -73,14 +75,34 @@ public class AnimatorLayerInfo } } + //public float playbackTimeInSeconds + //{ + // get + // { + // return stateInfo.normalizedTime * stateInfo.length;// stateInfo.length会等于infinity,因为设置了animator.speed = 0 + // } + //} + + // 并非准确的播放时间,只是逻辑时间,因为动画会加速减速 public float playbackTimeInSeconds { get { - return stateInfo.normalizedTime * stateInfo.length; + return stateInfo.normalizedTime * clipInfo[0].clip.length; + } + } + + // 这个是真实世界的时间 + private float m_PlaybackRealTime; + public float playbackRealTimeInSeconds + { + get + { + return m_PlaybackRealTime; } } + // 播放进度百分比 public float playbackNomralizedTime { get @@ -119,11 +141,28 @@ public class AnimatorLayerInfo UnitController m_Owner; + Coroutine m_CalcPlaybackTimeCoroutine; + public AnimatorLayerInfo(UnitController owner, Animator animator, UnitAnimation.ELayer layer) { this.m_Owner = owner; this.m_Animator = animator; this.layer = layer; + m_CalcPlaybackTimeCoroutine = owner.StartCoroutine(CalcPlaybackRealTimeCoroutine()); + } + + IEnumerator CalcPlaybackRealTimeCoroutine() + { + while (true) + { + if(preStateHash != stateHash) + { + m_PlaybackRealTime = 0; + } + m_PlaybackRealTime += Time.deltaTime; + preStateHash = stateHash; + yield return null; + } } } -- cgit v1.1-26-g67d0