This commit is contained in:
陈思海 2025-08-11 14:45:27 +08:00
parent 6bd66da295
commit d8a4cc0993

View File

@ -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; if (m_Buttons.Count == 0) return -1;
int buttonCount = m_Buttons.Count - 1;
int valueIndex = startIndex == -1 ? 0 : startIndex + direction;
int fallBackIndex = -1;
// 计算当前有效起点 valueIndex = valueIndex > buttonCount ? 0 : (valueIndex < 0 ? buttonCount : valueIndex);
int current = (startIndex == -1) if (valueIndex > buttonCount)
? (direction > 0 ? 0 : m_Buttons.Count - 1) {
: startIndex; valueIndex = 0;
}
else if (valueIndex < 0)
{
valueIndex = buttonCount;
}
// 单次简单循环查找 while (valueIndex != startIndex)
for (int i = 0; i < m_Buttons.Count; i++) { {
int index = (current + direction * i + m_Buttons.Count) % m_Buttons.Count; UXButton btn = m_Buttons[valueIndex];
if (btn.isActiveAndEnabled && btn.Interactable)
{
fallBackIndex = valueIndex;
break;
}
if (m_Buttons[index] != null && valueIndex += direction;
m_Buttons[index].isActiveAndEnabled && if (valueIndex > buttonCount)
m_Buttons[index].Interactable) { {
return index; valueIndex = 0;
}
else if (valueIndex < 0)
{
valueIndex = buttonCount;
} }
} }
return -1; // 没有可用按钮
return fallBackIndex;
} }
} }