优化
This commit is contained in:
parent
6bd66da295
commit
d8a4cc0993
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user