From 4c362271f502b9999cd77e991271f16993d5c665 Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 2 Aug 2021 18:58:01 +0800 Subject: *mics --- Assets/Scripts/Unit/Component/UnitAnimation.cs | 54 +++++++------------------- Assets/Scripts/Unit/Component/UnitState.cs | 27 +++++++------ 2 files changed, 28 insertions(+), 53 deletions(-) (limited to 'Assets/Scripts/Unit/Component') diff --git a/Assets/Scripts/Unit/Component/UnitAnimation.cs b/Assets/Scripts/Unit/Component/UnitAnimation.cs index 7ebd780c..de3d4cce 100644 --- a/Assets/Scripts/Unit/Component/UnitAnimation.cs +++ b/Assets/Scripts/Unit/Component/UnitAnimation.cs @@ -2,38 +2,11 @@ using System.Collections.Generic; using UnityEngine; -// 播放动画,执行帧事件 +// 控制动画播放,执行帧事件 [DisallowMultipleComponent] public class UnitAnimation : UnitComponent { - // animator trigger值,切换动画 - public enum ETrigger - { - Nein = 0, - - ToIdle , - ToMove , - ToSpawn , - - ToDie , - ToHitAir , - ToHitAirAir, - - ToHitKnockDown, - - ToJump, - ToWalk, - - ToAttack, - - ToRise, - - ToStinger, - - ToTurn, - } - - // animator状态 + // 动画 public enum EAnimState { Idle, @@ -46,15 +19,11 @@ public class UnitAnimation : UnitComponent Turn, } - public Dictionary triggers = new Dictionary() { - { EAnimState.Idle, ETrigger.ToIdle}, - { EAnimState.Move, ETrigger.ToMove}, - { EAnimState.Jump, ETrigger.ToJump}, - { EAnimState.Attack, ETrigger.ToAttack}, - { EAnimState.Rise, ETrigger.ToRise}, - { EAnimState.Stinger, ETrigger.ToStinger}, - { EAnimState.Turn , ETrigger.ToTurn}, - }; + public enum ELayer + { + Basic = 0, + Attack = 1, + } public Animator animator { get { return m_Animator; } } private Animator m_Animator; @@ -89,12 +58,15 @@ public class UnitAnimation : UnitComponent } } + public string GetTrigger(EAnimState state) + { + return "To" + state.ToString(); + } + public void Play(EAnimState state) { m_CurState = state; - ETrigger trigger = triggers[state]; - string toAnim = trigger.ToString(); - m_Animator.SetTrigger(toAnim); + m_Animator.SetTrigger(GetTrigger(state)); m_Animator.speed = 0; m_PlaybackTime = 0; m_Owner.unitRootMotion.Reset(); diff --git a/Assets/Scripts/Unit/Component/UnitState.cs b/Assets/Scripts/Unit/Component/UnitState.cs index 500096d3..587c2f33 100644 --- a/Assets/Scripts/Unit/Component/UnitState.cs +++ b/Assets/Scripts/Unit/Component/UnitState.cs @@ -1,4 +1,6 @@ -using System.Collections; +using System; +using System.Reflection; +using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -34,7 +36,6 @@ public class UnitState : UnitComponent public EUnitState CurrentState { get { return m_State; } } private delegate void ExitStateHandler(EUnitState nextState); - private delegate void EnterStateHandler(EUnitState prevState); private Dictionary m_ExitStateHandlerDic = new Dictionary(); @@ -71,10 +72,6 @@ public class UnitState : UnitComponent void InitState() { - m_ExitStateHandlerDic.Add(EUnitState.Idle, OnIdleExit); - m_ExitStateHandlerDic.Add(EUnitState.Move, OnMoveExit); - m_ExitStateHandlerDic.Add(EUnitState.Skill, OnSkillExit); - m_ExitStateHandlerDic.Add(EUnitState.Jump, OnJumpExit); } public void ChangeState(EUnitState nextState, T param = default, bool bForce = false) @@ -87,11 +84,17 @@ public class UnitState : UnitComponent StopAllCoroutines(); EUnitState prevState = m_State; - if (m_ExitStateHandlerDic.ContainsKey(m_State)) - { - m_ExitStateHandlerDic[m_State](nextState); - } - m_State = nextState; + string methodFunc = "On" + m_State.ToString() + "Exit"; + MethodInfo exitMethod = GetType().GetMethod(methodFunc, BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(EUnitState) }, null); + if(exitMethod != null) + { + exitMethod.Invoke(this, new object[] { nextState }); + } + else + { + LogHelper.LogError("缺少 " + methodFunc); + } + m_State = nextState; StartCoroutine(m_State.ToString(), param); } @@ -113,7 +116,7 @@ public class UnitState : UnitComponent void OnIdleExit(EUnitState nextState) { - + LogHelper.Log(nextState.ToString()); } #endregion -- cgit v1.1-26-g67d0