From 7669c3afcbd71f06233bce12bb521c582c07ee5b Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 9 Aug 2021 08:47:48 +0800 Subject: *misc --- .../Scripts/Unit/Action/WaitForActionReachEnd.cs | 3 +- Assets/Scripts/Unit/AnimationData.cs | 121 +++++++++++++++----- Assets/Scripts/Unit/Component/UnitAnimation.cs | 12 ++ Assets/Scripts/Unit/Component/UnitState.cs | 16 ++- Assets/Scripts/Unit/TimelineEventProxy.cs | 125 +++++++++++++++++++++ Assets/Scripts/Unit/TimelineEventProxy.cs.meta | 11 ++ Assets/Scripts/Unit/UnitTimeline.cs | 125 --------------------- Assets/Scripts/Unit/UnitTimeline.cs.meta | 11 -- 8 files changed, 251 insertions(+), 173 deletions(-) create mode 100644 Assets/Scripts/Unit/TimelineEventProxy.cs create mode 100644 Assets/Scripts/Unit/TimelineEventProxy.cs.meta delete mode 100644 Assets/Scripts/Unit/UnitTimeline.cs delete mode 100644 Assets/Scripts/Unit/UnitTimeline.cs.meta (limited to 'Assets/Scripts/Unit') diff --git a/Assets/Scripts/Unit/Action/WaitForActionReachEnd.cs b/Assets/Scripts/Unit/Action/WaitForActionReachEnd.cs index ab755b64..47aa639c 100644 --- a/Assets/Scripts/Unit/Action/WaitForActionReachEnd.cs +++ b/Assets/Scripts/Unit/Action/WaitForActionReachEnd.cs @@ -20,8 +20,7 @@ public class WaitForActionReachEnd : IEnumerator { var stateInfo = m_UnitAnimation.layers[(int)m_Layer].stateInfo; float normalTime = stateInfo.normalizedTime; - LogHelper.Log(stateInfo.loop.ToString()); - return normalTime < 1f; + return normalTime < 1f; } public void Reset() diff --git a/Assets/Scripts/Unit/AnimationData.cs b/Assets/Scripts/Unit/AnimationData.cs index 6035978d..465c0806 100644 --- a/Assets/Scripts/Unit/AnimationData.cs +++ b/Assets/Scripts/Unit/AnimationData.cs @@ -15,39 +15,86 @@ public enum EAnimationToogle } [Serializable] -public class ToggleTimeDictionary : SerializableDictionary { } +public struct FromTo +{ + [SerializeField] public float from; + [SerializeField] public float to; + public Vector2 fromTo + { + get + { + return new Vector2(from, to); + } + set + { + from = value.x; + to = value.y; + } + } + public FromTo(float from, float to) + { + this.from = from; + this.to = to; + } + public FromTo(Vector2 fromTo) + { + this.from = fromTo.x; + this.to = fromTo.y; + } +} + +[Serializable] +public class ToggleTimeDictionary : SerializableDictionary { } public enum EAnimationCurve { TimeScale = 0, // + RootMotionScale = 1, } [Serializable] public class CurveDictionary : SerializableDictionary { } +[Serializable] +public struct AnimationParameter +{ + [Serializable] + public struct Setter + { + [SerializeField] public float normalizedTime; + [SerializeField] public float value; + } + [SerializeField] public List setters; +} + +[Serializable] +public class ParameterDictionary : SerializableDictionary { } + // 某个动画的数据,包括帧事件、碰撞盒、速度曲线 [CreateAssetMenu(fileName = "Animation Data")] public class AnimationData : ScriptableObject { - public string animationName; - public string animationPath; + public string animationName; + public string animationPath; - public List animationEvents; + public List animationEvents; - public List hurtBoxes; - public List hitBoxes; - public List throwBoxes; - public List blockBoxes; - public List defendBoxes; + public List hurtBoxes; + public List hitBoxes; + public List throwBoxes; + public List blockBoxes; + public List defendBoxes; - // 对应的进度的播放速度,默认是1 + // 对应的进度的播放速度,默认是1 [UnityEngine.Serialization.FormerlySerializedAs("curve")] - public AnimationCurve speedCurve; + public AnimationCurve speedCurve; public CurveDictionary curves; public ToggleTimeDictionary toggles; + public ParameterDictionary parameters; + public const int FPS = 30; public AnimationData() @@ -57,23 +104,39 @@ public class AnimationData : ScriptableObject speedCurve = new AnimationCurve(frame0, frame1); } - public List GetColliderBoxesByType(ColliderBox.EColliderType type) - { - switch(type) - { - case ColliderBox.EColliderType.HurtBox: - return hurtBoxes; - case ColliderBox.EColliderType.HitBox: - return hitBoxes; - case ColliderBox.EColliderType.BlockBox: - return blockBoxes; - case ColliderBox.EColliderType.ThrowBox: - return throwBoxes; - case ColliderBox.EColliderType.DefendBox: - return defendBoxes; - } - return null; - } + public List GetColliderBoxesByType(ColliderBox.EColliderType type) + { + switch (type) + { + case ColliderBox.EColliderType.HurtBox: + return hurtBoxes; + case ColliderBox.EColliderType.HitBox: + return hitBoxes; + case ColliderBox.EColliderType.BlockBox: + return blockBoxes; + case ColliderBox.EColliderType.ThrowBox: + return throwBoxes; + case ColliderBox.EColliderType.DefendBox: + return defendBoxes; + } + return null; + } + + //public bool HasParameter(string parameterName, float normalizedTime) + //{ + // if (!parameters.ContainsKey(parameterName)) + // return false; + // var parameter = parameters[parameterName]; + // parameter.setters.Sort((AnimationParameter.Setter a, AnimationParameter.Setter b) => + // { + // return a.normalizedTime - b.normalizedTime < 0 ? -1 : 1; + // }); + //} + + //public float GetParameter(string parameter, float normalizedTime) + //{ + + //} public bool HasCurve(EAnimationCurve curve) { @@ -96,7 +159,7 @@ public class AnimationData : ScriptableObject { if (!HasToggle(toggle)) return false; - return toggles[toggle].y <= normalizedTime && normalizedTime >= toggles[toggle].x; + return toggles[toggle].to <= normalizedTime && normalizedTime >= toggles[toggle].from; } public ColliderInfo GetColliderInfo(ColliderBox.EColliderType type, int index, float playbackTime) diff --git a/Assets/Scripts/Unit/Component/UnitAnimation.cs b/Assets/Scripts/Unit/Component/UnitAnimation.cs index 44cfb08b..bbe4eb22 100644 --- a/Assets/Scripts/Unit/Component/UnitAnimation.cs +++ b/Assets/Scripts/Unit/Component/UnitAnimation.cs @@ -233,6 +233,8 @@ public class UnitAnimation : UnitComponent ToIdle, ToMove, ToAttack, + ToJump, + ToAirAttack, } public Animator animator { get { return m_Animator; } } @@ -325,4 +327,14 @@ public class UnitAnimation : UnitComponent m_Animator.SetTrigger(trigger.ToString()); } + public void AnimJump() + { + SetTrigger(ETrigger.ToJump); + } + + public void AnimAirAttack() + { + SetTrigger(ETrigger.ToAirAttack); + } + } diff --git a/Assets/Scripts/Unit/Component/UnitState.cs b/Assets/Scripts/Unit/Component/UnitState.cs index fd9bba00..0ad1ec58 100644 --- a/Assets/Scripts/Unit/Component/UnitState.cs +++ b/Assets/Scripts/Unit/Component/UnitState.cs @@ -45,6 +45,8 @@ public class UnitState : UnitComponent InitState(); } + UnitAnimation unitAnimation { get { return m_Owner.unitAnimation; } } + #region state param public struct IdleParam {} @@ -202,13 +204,16 @@ public class UnitState : UnitComponent IEnumerator Jump(JumpParam param) { - while(true) + unitAnimation.AnimJump(); + yield return null; + yield return new WaitForTransitionDone(unitAnimation); + while (true) { - // 空中连击 - if(Input.GetKeyDown("j")) + if (unitAnimation.layers[0].playbackNomralizedTime >= 1) + ChangeState(EUnitState.Idle, new IdleParam()); + if (Input.GetKeyDown("j")) { - SkillParam skill = new SkillParam(); - ChangeState(EUnitState.Skill, skill); + ChangeState(EUnitState.Skill, new SkillParam()); } yield return null; } @@ -216,7 +221,6 @@ public class UnitState : UnitComponent void OnJumpExit(EUnitState next) { - } #endregion diff --git a/Assets/Scripts/Unit/TimelineEventProxy.cs b/Assets/Scripts/Unit/TimelineEventProxy.cs new file mode 100644 index 00000000..00b5f1dc --- /dev/null +++ b/Assets/Scripts/Unit/TimelineEventProxy.cs @@ -0,0 +1,125 @@ +using System; +using System.Collections; +using System.Reflection; +using System.Collections.Generic; +using UnityEngine; +#if UNITY_EDITOR +using UnityEditor; +#endif + +// 执行帧事件 +[DisallowMultipleComponent] +public partial class TimelineEventProxy +{ + public enum EEventType + { + EventCamera_Zoom, // 相机聚焦 + EventCamera_Shake, // 相机晃动 + EventCamera_Blur, // 相机模糊 + EventCamera_WhiteOut, // 相机白屏 + + EventMesh_AfterImage, // 角色残像 + EventMesh_Fade, // 角色透明度 + EventMesh_Gloss, // 角色泛光 + + EventEnv_Dark, + EventEnv_Exposure, + + EventUI_Drift, // + EventUI_Blur, // + + EventProjectile, // 发射体 + EventEffect, // 特效 + EventSound, // 音效 + + EventBulletTime, // 子弹时间 + } + + public const int kMaxEventsPerFrame = 10; + public const int FPS = 30; // timeline 每秒采样30次 + + private int m_PrevFrame = -1; + + private UnitAnimation m_UnitAnimation; + + private Transform m_Root; + + public TimelineEventProxy(Transform root, UnitAnimation unitAnimation = null) + { + m_Root = root; + } + + public static Type GetTypeByName(string name) + { + Type type = Type.GetType(name); + return type; + } + + public void ExecuteAnimationEvents(AnimationData animData, float animFrame) + { + if (animData == null) + return; + int frame = (int)animFrame; + if (frame != m_PrevFrame) + { + for (int i = m_PrevFrame + 1; i <= frame; i++) + { + List framesHasEvent = animData.GetAnimationEventFrameIndices(); + if (framesHasEvent.Contains(i)) + { + List events = animData.GetAnimationEventsAtFrame(i); + ExecuteEvents(events); + ListPool.Release(events); + } + ListPool.Release(framesHasEvent); + } + } + m_PrevFrame = frame; + } + + void ExecuteEvents(List events) + { + if (events == null || events.Count == 0) + return; + foreach (var e in events) + { + string name = e.type.ToString(); + MethodInfo method = GetType().GetMethod(name, BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(AnimationEventBase) }, null); + if (method != null) + { + object[] param = new object[] { e }; + method.Invoke(this, param); + } + } + } + + #region Event handle + + void EventEffect(AnimationEventBase animEvent) + { + EventEffect effect = animEvent as EventEffect; + if (effect == null) + return; + string path = effect.effectPath; +#if UNITY_EDITOR + GameObject prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject; + if (prefab != null) + { + GameObject root = new GameObject(); + + GameObject go = GameObject.Instantiate(prefab); + go.transform.SetParent(root.transform); + FxClear onClear = root.AddComponent(); + onClear.RunInEditor = true; + onClear.Initialize(new PlayEffectInfo(path, EffectPlayTypes.Oneshot, m_Root, effect.position, effect.rotation, effect.scale, 0, false)); + } +#endif + } + + void EventCamera_Shake(AnimationEventBase animEvent) + { + + } + + #endregion +} \ No newline at end of file diff --git a/Assets/Scripts/Unit/TimelineEventProxy.cs.meta b/Assets/Scripts/Unit/TimelineEventProxy.cs.meta new file mode 100644 index 00000000..c311d274 --- /dev/null +++ b/Assets/Scripts/Unit/TimelineEventProxy.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 893827f42c99cd849987e51e6af8820d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Unit/UnitTimeline.cs b/Assets/Scripts/Unit/UnitTimeline.cs deleted file mode 100644 index 99c6bdaf..00000000 --- a/Assets/Scripts/Unit/UnitTimeline.cs +++ /dev/null @@ -1,125 +0,0 @@ -using System; -using System.Collections; -using System.Reflection; -using System.Collections.Generic; -using UnityEngine; -#if UNITY_EDITOR -using UnityEditor; -#endif - -// 执行帧事件 -[DisallowMultipleComponent] -public partial class TimelineEventProxy -{ - public enum EEventType - { - EventCamera_Zoom, // 相机聚焦 - EventCamera_Shake, // 相机晃动 - EventCamera_Blur, // 相机模糊 - EventCamera_WhiteOut, // 相机白屏 - - EventMesh_AfterImage, // 角色残像 - EventMesh_Fade, // 角色透明度 - EventMesh_Gloss, // 角色泛光 - - EventEnv_Dark, - EventEnv_Exposure, - - EventUI_Drift, // - EventUI_Blur, // - - EventProjectile, // 发射体 - EventEffect, // 特效 - EventSound, // 音效 - - EventBulletTime, // 子弹时间 - } - - public const int kMaxEventsPerFrame = 10; - public const int FPS = 30; // timeline 每秒采样30次 - - private int m_PrevFrame = -1; - - private UnitAnimation m_UnitAnimation; - - private Transform m_Root; - - public TimelineEventProxy(Transform root, UnitAnimation unitAnimation = null) - { - m_Root = root; - } - - public static Type GetTypeByName(string name) - { - Type type = Type.GetType(name); - return type; - } - - public void ExecuteAnimationEvents(AnimationData animData, float animFrame) - { - if (animData == null) - return; - int frame = (int)animFrame; - if (frame != m_PrevFrame) - { - for (int i = m_PrevFrame + 1; i <= frame; i++) - { - List framesHasEvent = animData.GetAnimationEventFrameIndices(); - if (framesHasEvent.Contains(i)) - { - List events = animData.GetAnimationEventsAtFrame(i); - ExecuteEvents(events); - ListPool.Release(events); - } - ListPool.Release(framesHasEvent); - } - } - m_PrevFrame = frame; - } - - void ExecuteEvents(List events) - { - if (events == null || events.Count == 0) - return; - foreach (var e in events) - { - string name = e.type.ToString(); - MethodInfo method = GetType().GetMethod(name, BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(AnimationEventBase) }, null); - if (method != null) - { - object[] param = new object[] { e }; - method.Invoke(this, param); - } - } - } - - #region Event handle - - void EventEffect(AnimationEventBase animEvent) - { - EventEffect effect = animEvent as EventEffect; - if (effect == null) - return; - string path = effect.effectPath; -#if UNITY_EDITOR - GameObject prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject; - if (prefab != null) - { - GameObject root = new GameObject(); - - GameObject go = GameObject.Instantiate(prefab); - go.transform.SetParent(root.transform); - FxClear onClear = root.AddComponent(); - onClear.RunInEditor = true; - onClear.Initialize(new PlayEffectInfo(path, EffectPlayTypes.Oneshot, m_Root, effect.position, effect.rotation, effect.scale, 0, false)); - } -#endif - } - - void EventCamera_Shake(AnimationEventBase animEvent) - { - - } - - #endregion -} \ No newline at end of file diff --git a/Assets/Scripts/Unit/UnitTimeline.cs.meta b/Assets/Scripts/Unit/UnitTimeline.cs.meta deleted file mode 100644 index eacf2ed1..00000000 --- a/Assets/Scripts/Unit/UnitTimeline.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7bb2f04adc210b04dad020a77628fe79 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: -- cgit v1.1-26-g67d0