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 +++++++++++++++- Assets/Scripts/Unit/Component/UnitCollider.cs | 65 ++++++++++++++++++++++++ Assets/Scripts/Unit/Controller/UnitController.cs | 7 ++- 3 files changed, 112 insertions(+), 3 deletions(-) (limited to 'Assets/Scripts') 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; + } } } diff --git a/Assets/Scripts/Unit/Component/UnitCollider.cs b/Assets/Scripts/Unit/Component/UnitCollider.cs index 78757706..2630cb91 100644 --- a/Assets/Scripts/Unit/Component/UnitCollider.cs +++ b/Assets/Scripts/Unit/Component/UnitCollider.cs @@ -2,8 +2,73 @@ using System.Collections.Generic; using UnityEngine; +// 角色当前的碰撞盒 [DisallowMultipleComponent] public class UnitCollider : UnitComponent { + public bool showGizmos; + + public override void Initialize() + { + base.Initialize(); + showGizmos = true; + } + + // 返回当前激活的对应类型的碰撞盒数据 + public ColliderInfo[] GetCurrentBoxesInfoByType(ColliderBox.EColliderType type, UnitAnimation.ELayer layer = UnitAnimation.ELayer.Basic) + { + var layerInfo = m_Owner.unitAnimation.layers[(int)layer]; + AnimationData animData = layerInfo.animationData; + float playbackTime = layerInfo.playbackNomralizedTime * layerInfo.clipInfo[0].clip.length; + //float playbackTime = layerInfo.playbackRealTimeInSeconds; + ColliderInfo[] infos = animData.GetActiveCollidersInfo(type, playbackTime); + return infos; + } + +#if UNITY_EDITOR + + public void OnDrawGizmos() + { + if (!showGizmos) + return; + + Vector3 unitPos = m_Owner.transform.position; + + OnDrawColliders(ColliderBox.EColliderType.HurtBox, Color.green); + OnDrawColliders(ColliderBox.EColliderType.HitBox, Color.red); + } + + void OnDrawColliders(ColliderBox.EColliderType type, Color color) + { + ColliderInfo[] boxes = GetCurrentBoxesInfoByType(type); + if (boxes == null || boxes.Length == 0) + return; + Vector3 unitPos = m_Owner.transform.position; + Quaternion right = Quaternion.Euler(0, 0, 0); + Vector3 fac = new Vector3(1,1, m_Owner.transform.forward.normalized == Vector3.forward ? 1 : -1); + Color oldC = Gizmos.color; + Gizmos.color = color * 0.5f; + for (int i = 0; i < boxes.Length; ++i) + { + var box = boxes[i]; + if (!box.isValid) + continue; + Vector3 localPos = box.position; + Vector3 localSize = box.size; + var pivot = box.pivot; + Vector3 pos = Vector3.zero; // gizmo位置 + switch (pivot) + { + case ColliderBox.Pivot.MiddleBottom: + localPos.y += localSize.y / 2; + break; + } + pos = unitPos + Vector3.Scale(localPos, fac); + Gizmos.DrawCube(pos, localSize); + } + Gizmos.color = oldC; + } + +#endif } diff --git a/Assets/Scripts/Unit/Controller/UnitController.cs b/Assets/Scripts/Unit/Controller/UnitController.cs index a1eecde6..b1e708bb 100644 --- a/Assets/Scripts/Unit/Controller/UnitController.cs +++ b/Assets/Scripts/Unit/Controller/UnitController.cs @@ -16,6 +16,8 @@ public class UnitController : MonoBehaviour public UnitRootMotion unitRootMotion; + public UnitCollider unitCollider; + public GameObject unitObj; // 角色模型 public bool isTowardRight @@ -72,7 +74,10 @@ public class UnitController : MonoBehaviour unitRootMotion = gameObject.GetOrAddComponent(); unitRootMotion.Initialize(); - } + unitCollider = gameObject.GetOrAddComponent(); + unitCollider.Initialize(); + + } public virtual void Update() { -- cgit v1.1-26-g67d0