From ce73a13f28e5a947df8f1f87f1f1be20010952ec Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 2 Aug 2021 08:35:26 +0800 Subject: =?UTF-8?q?*=20=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/Unit/UnitRootMotion.cs | 81 +++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 13 deletions(-) (limited to 'Assets/Scripts/Unit/UnitRootMotion.cs') diff --git a/Assets/Scripts/Unit/UnitRootMotion.cs b/Assets/Scripts/Unit/UnitRootMotion.cs index a2607700..18e0093f 100644 --- a/Assets/Scripts/Unit/UnitRootMotion.cs +++ b/Assets/Scripts/Unit/UnitRootMotion.cs @@ -1,24 +1,79 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +#if UNITY_EDITOR +using UnityEditor; +#endif // 同步root motion到角色根节点 [DisallowMultipleComponent] -public class UnitRootMotion : MonoBehaviour +public class UnitRootMotion : UnitComponent { - Transform m_Root; - Animator m_Animator; - - private void Awake() - { - m_Root = transform.parent; - m_Animator = GetComponent(); - } - - public Vector3 UpdateRootMotion() + RootMotionData m_RootMotionData; + + Dictionary m_RootMotionDic = new Dictionary(); + + private float m_PrevNormalTime; + + public override void Initialize() { - - return Vector3.zero; + base.Initialize(); + } + + public void Reset() + { + m_PrevNormalTime = 0; + } + +#if false // 用自定义root motion + public override void OnUpdate() + { + base.OnUpdate(); + + var state = m_Owner.unitAnimation.curState; + float playbackTime = m_Owner.unitAnimation.playbackTime; + + var rootMotion = m_RootMotionDic[state]; + float normalTime = (playbackTime % rootMotion.animationLength) / rootMotion.animationLength; + + if (m_PrevNormalTime > normalTime) + m_PrevNormalTime = 0; + + m_Owner.transform.position += rootMotion.GetRootMotionDistance(m_PrevNormalTime, normalTime); + m_PrevNormalTime = normalTime; } + public void SetUpRootMotion(string unitFolder, UnitActionData actions) + { + if (actions == null) + return; + + foreach (var action in actions.actions) + { +#if UNITY_EDITOR + AnimationClip clip = action.Value; + string name = clip.name; + string path = unitFolder + "RootMotion/" + name + ".asset"; + RootMotionData data = AssetDatabase.LoadAssetAtPath(path); + m_RootMotionDic.Add(action.Key, data); +#endif + } + } + +#else + + public override void OnUpdate() + { + base.OnUpdate(); + } + + public void UpdateRootMotion() + { + Vector3 dest = m_Owner.unitAnimation.animator.deltaPosition; + dest.x = 0; + m_Owner.transform.position += dest; + } + +#endif + } -- cgit v1.1-26-g67d0