From c25f3f46d6d857991f49029bf64dc2f5766d2d16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Wed, 17 Dec 2025 15:15:34 +0800 Subject: [PATCH] fix --- Runtime/UXComponent/Button/UXButton.cs | 62 +++----------------------- 1 file changed, 6 insertions(+), 56 deletions(-) diff --git a/Runtime/UXComponent/Button/UXButton.cs b/Runtime/UXComponent/Button/UXButton.cs index 063d777..8666d87 100644 --- a/Runtime/UXComponent/Button/UXButton.cs +++ b/Runtime/UXComponent/Button/UXButton.cs @@ -38,7 +38,6 @@ public class UXButton : UXSelectable, IButton, IPointerClickHandler, ISubmitHand private bool _mTogSelected; private Coroutine _resetRoutine; private Coroutine _deferredDeselectRoutine; - private Coroutine _deferredLockRoutine; // 新增:用于 Focus 延迟锁定的协程 private WaitForSeconds _waitFadeDuration; // 静态锁(用于 normal 模式点击后的“保持 Selected”并能转移) @@ -119,10 +118,6 @@ public class UXButton : UXSelectable, IButton, IPointerClickHandler, ISubmitHand { if (_resetRoutine != null) StopCoroutine(_resetRoutine); - if (_deferredDeselectRoutine != null) - StopCoroutine(_deferredDeselectRoutine); - if (_deferredLockRoutine != null) - StopCoroutine(_deferredLockRoutine); base.OnDestroy(); } @@ -379,36 +374,15 @@ public class UXButton : UXSelectable, IButton, IPointerClickHandler, ISubmitHand // 等一帧,保证 EventSystem 更新 currentSelectedGameObject yield return null; - // 当前选中对象(可能为 null) - var currentSelected = EventSystem.current != null ? EventSystem.current.currentSelectedGameObject : null; + bool selectionIsNull = EventSystem.current == null || EventSystem.current.currentSelectedGameObject == null; - if (currentSelected == null) + if (selectionIsNull) { - // 没有任何选中对象:清理锁 + // 统一走封装入口清理锁 SetLockedButton(null); - m_IsFocusLocked = false; - } - else - { - // 如果有选中对象,检查它是否属于一个 UXButton(可能是子对象) - var selectedBtn = currentSelected.GetComponentInParent(); - if (selectedBtn == null) - { - // 新选中对象不是 UXButton 的组成部分:清理锁 - SetLockedButton(null); - m_IsFocusLocked = false; - } - else if (selectedBtn != this) - { - // 新选中对象属于另一个 UXButton:把锁转给那个按钮 - // (即便目标的 OnSelect 也可能会做这件事,这里直接转能避免时序问题) - SetLockedButton(selectedBtn); - } - else - { - // selectedBtn == this:焦点仍然在自己上,不需要清理 - } + // 额外保证本实例的本地标志被清掉(以防极端顺序仍然残留) + m_IsFocusLocked = false; } _deferredDeselectRoutine = null; @@ -598,7 +572,6 @@ public class UXButton : UXSelectable, IButton, IPointerClickHandler, ISubmitHand if (!IsInteractable()) return; - // 先发起选中请求 if (EventSystem.current != null) { EventSystem.current.SetSelectedGameObject(gameObject, new BaseEventData(EventSystem.current)); @@ -606,32 +579,9 @@ public class UXButton : UXSelectable, IButton, IPointerClickHandler, ISubmitHand m_IsNavFocused = true; - // 停掉已有的延迟锁协程(如果有) - if (_deferredLockRoutine != null) - { - StopCoroutine(_deferredLockRoutine); - _deferredLockRoutine = null; - } - - // 如果已经成为 EventSystem 的 selected,则立即锁定;否则延迟一帧再确认并锁定 - if (IsStillSelected()) + if ((s_LockedButton != null && s_LockedButton != this) || s_LockedButton == null) { SetLockedButton(this); } - else - { - _deferredLockRoutine = StartCoroutine(DeferredLockAfterFocus()); - } - } - - private IEnumerator DeferredLockAfterFocus() - { - // 等一帧,让 EventSystem 完成 selected 的更新和分发(OnDeselect/OnSelect 等) - yield return null; - - if (IsStillSelected()) - SetLockedButton(this); - - _deferredLockRoutine = null; } }