diff --git a/Runtime/UXComponent/Button/UXButton.cs b/Runtime/UXComponent/Button/UXButton.cs index 8f2339b..cf536cc 100644 --- a/Runtime/UXComponent/Button/UXButton.cs +++ b/Runtime/UXComponent/Button/UXButton.cs @@ -36,7 +36,6 @@ public class UXButton : UXSelectable, IButton, IPointerClickHandler, ISubmitHand private bool m_IsDown; private bool m_HasExitedWhileDown; private bool _mTogSelected; - private Animator _animator; private Coroutine _resetRoutine; private WaitForSeconds _waitFadeDuration; @@ -47,21 +46,10 @@ public class UXButton : UXSelectable, IButton, IPointerClickHandler, ISubmitHand private bool m_IsNavFocused = false; - private static readonly Dictionary _animTriggerCache = new() - { - { "Normal", Animator.StringToHash("Normal") }, - { "Highlighted", Animator.StringToHash("Highlighted") }, - { "Pressed", Animator.StringToHash("Pressed") }, - { "Selected", Animator.StringToHash("Selected") }, - { "Disabled", Animator.StringToHash("Disabled") }, - }; - #endregion #region Properties - private Animator Animator => _animator ? _animator : _animator = GetComponent(); - public bool Selected { get => _mTogSelected; @@ -538,22 +526,6 @@ public class UXButton : UXSelectable, IButton, IPointerClickHandler, ISubmitHand base.ApplyTransition(m_ChildTransitions[i], state, instant); } - /// - /// 覆盖 PlayAnimation:使用 UXButton 的 Animator 与 trigger 缓存(保持原逻辑) - /// - /// - protected override void PlayAnimation(string trigger) - { - if (!Animator || !Animator.isActiveAndEnabled || string.IsNullOrEmpty(trigger)) - return; - - foreach (int id in _animTriggerCache.Values) - Animator.ResetTrigger(id); - - if (_animTriggerCache.TryGetValue(trigger, out int hash)) - Animator.SetTrigger(hash); - } - private void PlayAudio(AudioClip clip) { if (clip && UXComponentExtensionsHelper.AudioHelper != null) @@ -562,7 +534,6 @@ public class UXButton : UXSelectable, IButton, IPointerClickHandler, ISubmitHand #endregion - public void Focus() { if (!IsInteractable()) diff --git a/Runtime/UXComponent/Selectable/UXSelectable.cs b/Runtime/UXComponent/Selectable/UXSelectable.cs index f20433e..0762dc1 100644 --- a/Runtime/UXComponent/Selectable/UXSelectable.cs +++ b/Runtime/UXComponent/Selectable/UXSelectable.cs @@ -58,6 +58,19 @@ namespace UnityEngine.UI protected int m_CurrentIndex = -1; protected bool m_EnableCalled = false; + private Animator _animator; + public Animator Animator => _animator ? _animator : _animator = GetComponent(); + + public static readonly Dictionary _animTriggerCache = new() + { + { "Normal", Animator.StringToHash("Normal") }, + { "Highlighted", Animator.StringToHash("Highlighted") }, + { "Pressed", Animator.StringToHash("Pressed") }, + { "Selected", Animator.StringToHash("Selected") }, + { "Disabled", Animator.StringToHash("Disabled") }, + }; + + public Graphic targetGraphic { get { return m_MainTransition.targetGraphic; } @@ -644,7 +657,14 @@ namespace UnityEngine.UI /// protected virtual void PlayAnimation(string trigger) { - // base does nothing — subclasses (e.g. UXButton) can override to use Animator. + if (!Animator || !Animator.isActiveAndEnabled || string.IsNullOrEmpty(trigger) || !gameObject.activeInHierarchy) + return; + + foreach (int id in _animTriggerCache.Values) + Animator.ResetTrigger(id); + + if (_animTriggerCache.TryGetValue(trigger, out int hash)) + Animator.SetTrigger(hash); } #endregion