这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

74 行
2.5 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SetButtonNavigation : MonoBehaviour
{
UIGenericButton[] genericButtons;
UISettingItemFiller[] listSettingItems;
private void Start()
{
listSettingItems = GetComponentsInChildren<UISettingItemFiller>();
genericButtons = GetComponentsInChildren<UIGenericButton>();
MultiInputButton buttonToSelectOnDown = default;
MultiInputButton buttonToSelectOnUp = default;
for (int i = 0; i < listSettingItems.Length; i++)
{
Navigation newNavigation = new Navigation();
newNavigation.mode = Navigation.Mode.Explicit;
newNavigation.selectOnLeft = listSettingItems[i].gameObject.GetComponent<MultiInputButton>().navigation.selectOnLeft;
newNavigation.selectOnRight = listSettingItems[i].gameObject.GetComponent<MultiInputButton>().navigation.selectOnRight;
if (i + 1 < listSettingItems.Length)
{
buttonToSelectOnDown = listSettingItems[i + 1].gameObject.GetComponent<MultiInputButton>();
}
else if (genericButtons.Length > 0)
{
buttonToSelectOnDown = genericButtons[0].gameObject.GetComponent<MultiInputButton>();
SetGenericButtonsNavigations(listSettingItems[i].gameObject.GetComponent<MultiInputButton>());
}
if (i - 1 >= 0)
buttonToSelectOnUp = listSettingItems[i - 1].gameObject.GetComponent<MultiInputButton>();
newNavigation.selectOnDown = buttonToSelectOnDown;
newNavigation.selectOnUp = buttonToSelectOnUp;
listSettingItems[i].gameObject.GetComponent<MultiInputButton>().navigation = newNavigation;
listSettingItems[i].SetNavigation(buttonToSelectOnDown, buttonToSelectOnUp);
}
}
private void OnEnable()
{
if (listSettingItems == null)
{
listSettingItems = GetComponentsInChildren<UISettingItemFiller>();
}
if (listSettingItems.Length > 0)
if (listSettingItems[0].GetComponent<MultiInputButton>() != null)
//select first item
listSettingItems[0].GetComponent<MultiInputButton>().Select();
}
void SetGenericButtonsNavigations(MultiInputButton itemUp)
{
for (int i = 0; i < genericButtons.Length; i++)
{
Navigation newNavigation = new Navigation();
newNavigation.mode = Navigation.Mode.Explicit;
if (i + 1 < genericButtons.Length)
newNavigation.selectOnRight = genericButtons[i + 1].gameObject.GetComponent<MultiInputButton>();
if (i - 1 > 0)
newNavigation.selectOnLeft = genericButtons[i - 1].gameObject.GetComponent<MultiInputButton>();
newNavigation.selectOnUp = itemUp;
}
}
}