| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class KeyBoardNavigation : MonoBehaviour
- {
- public static HashSet<KeyBoardNavigation> NavigationSet = new();
- public List<Button> buttons;
- public static bool IsNavigationButton(Button btn)
- {
- foreach (var item in NavigationSet)
- {
- if (item.buttons.Contains(btn)) return true;
- }
- return false;
- }
- public static Button Next(Button btn, int delta, bool first = false)
- {
- if (Mathf.Abs(delta) != 1) throw new System.Exception("delta only support 1 or -1");
- List<Button> buttons = btn.GetComponentInParent<KeyBoardNavigation>().buttons;
- if (buttons.Count == 0) throw new System.Exception("KeyBoardNavigation.buttons Is Empty");
- int index = first ? (delta < 0 ? buttons.Count : -1) : buttons.IndexOf(btn);
- if (delta < 0 && index < 0) index = buttons.Count;
- int maxCheckCount = buttons.Count;
- for (int i = 0; i < maxCheckCount; i++)
- {
- index += delta;
- if (index < 0) index += buttons.Count;
- index = index % buttons.Count;
- Button curBtn = buttons[index];
- if (curBtn && curBtn.gameObject && curBtn.gameObject.activeInHierarchy && curBtn.interactable)
- {
- return buttons[index];
- }
- }
- return null;
- }
- void Awake()
- {
- if (!CommonConfig.StandaloneMode)
- {
- Destroy(this);
- }
- }
- void OnEnable()
- {
- NavigationSet.Add(this);
- }
- void OnDisable()
- {
- if (NavigationSet.Contains(this))
- {
- NavigationSet.Remove(this);
- }
- }
- }
|