From 01c471cb9513ae7935d4644e58178994fb5fbb83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Fri, 8 Aug 2025 20:56:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Runtime/UXComponent/UX/UXButton.cs | 5 +++ Runtime/UXComponent/UX/UXGroup.cs | 59 ++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/Runtime/UXComponent/UX/UXButton.cs b/Runtime/UXComponent/UX/UXButton.cs index dd36bfd..908d7a8 100644 --- a/Runtime/UXComponent/UX/UXButton.cs +++ b/Runtime/UXComponent/UX/UXButton.cs @@ -84,6 +84,11 @@ public class UXButton : UIBehaviour, IButton, #region Properties + internal bool Interactable + { + get => m_Interactable; + } + internal Animator animator { get diff --git a/Runtime/UXComponent/UX/UXGroup.cs b/Runtime/UXComponent/UX/UXGroup.cs index 297c452..04a9d6d 100644 --- a/Runtime/UXComponent/UX/UXGroup.cs +++ b/Runtime/UXComponent/UX/UXGroup.cs @@ -31,6 +31,7 @@ public class UXGroup : UIBehaviour { if (button) button.IsSelected = false; } + m_Buttons.Clear(); _registeredButtons.Clear(); base.OnDestroy(); @@ -113,6 +114,64 @@ public class UXGroup : UIBehaviour if (button != exclusion && button.IsSelected) return true; } + return false; } + + + public void SelectPrevious() + { + if (m_Buttons.Count == 0) return; + + int startIndex = _current ? m_Buttons.IndexOf(_current) : -1; + int newIndex = FindSelectableIndex(startIndex, -1); + + if (newIndex != -1) + { + SetSelectedButton(m_Buttons[newIndex]); + } + } + + + public void SelectNext() + { + if (m_Buttons.Count == 0) return; + + int startIndex = _current ? m_Buttons.IndexOf(_current) : 0; + int newIndex = FindSelectableIndex(startIndex, 1); + + if (newIndex != -1) + { + SetSelectedButton(m_Buttons[newIndex]); + } + } + + + private int FindSelectableIndex(int startIndex, int direction) + { + if (m_Buttons.Count == 0) return -1; + + startIndex = Mathf.Clamp(startIndex, -1, m_Buttons.Count - 1); + + int fallback = -1; + + for (int i = 0; i < m_Buttons.Count; i++) + { + int index = (startIndex + direction + m_Buttons.Count + i * direction) % m_Buttons.Count; + + index = direction < 0 ? (m_Buttons.Count - index - 1) : index; + + var button = m_Buttons[index]; + + if (button == null || !button.isActiveAndEnabled || !button.Interactable) + continue; + + if (fallback == -1) fallback = index; + + if (_current == null) + return index; + } + + return fallback; + } }