From d8a4cc0993dc7536d8dc728002d73a7bfbc402f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Mon, 11 Aug 2025 14:45:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Runtime/UXComponent/UX/UXGroup.cs | 45 ++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/Runtime/UXComponent/UX/UXGroup.cs b/Runtime/UXComponent/UX/UXGroup.cs index fe40eea..667c71d 100644 --- a/Runtime/UXComponent/UX/UXGroup.cs +++ b/Runtime/UXComponent/UX/UXGroup.cs @@ -147,24 +147,43 @@ public class UXGroup : UIBehaviour } - private int FindSelectableIndex(int startIndex, int direction) { + private int FindSelectableIndex(int startIndex, int direction) + { if (m_Buttons.Count == 0) return -1; + int buttonCount = m_Buttons.Count - 1; + int valueIndex = startIndex == -1 ? 0 : startIndex + direction; + int fallBackIndex = -1; - // 计算当前有效起点 - int current = (startIndex == -1) - ? (direction > 0 ? 0 : m_Buttons.Count - 1) - : startIndex; + valueIndex = valueIndex > buttonCount ? 0 : (valueIndex < 0 ? buttonCount : valueIndex); + if (valueIndex > buttonCount) + { + valueIndex = 0; + } + else if (valueIndex < 0) + { + valueIndex = buttonCount; + } - // 单次简单循环查找 - for (int i = 0; i < m_Buttons.Count; i++) { - int index = (current + direction * i + m_Buttons.Count) % m_Buttons.Count; + while (valueIndex != startIndex) + { + UXButton btn = m_Buttons[valueIndex]; + if (btn.isActiveAndEnabled && btn.Interactable) + { + fallBackIndex = valueIndex; + break; + } - if (m_Buttons[index] != null && - m_Buttons[index].isActiveAndEnabled && - m_Buttons[index].Interactable) { - return index; + valueIndex += direction; + if (valueIndex > buttonCount) + { + valueIndex = 0; + } + else if (valueIndex < 0) + { + valueIndex = buttonCount; } } - return -1; // 没有可用按钮 + + return fallBackIndex; } }