Files
2026-02-21 16:45:37 +08:00

93 lines
2.3 KiB
C#

using System.Collections.Generic;
using BitStrap;
using UnityEngine;
using UnityEngine.EventSystems;
public class MenuState : MonoBehaviour
{
public bool selectOnEnable = true;
public List<GameObject> firstSelected = new List<GameObject>();
[ReadOnly]
public GameObject currentSelected;
[ReadOnly]
public GameObject prevSelected;
public static bool uiSubmitWasPressed;
private void OnEnable()
{
if (base.gameObject.activeInHierarchy && (bool)EventSystem.current && (selectOnEnable || ((bool)EventSystem.current && (bool)EventSystem.current.currentSelectedGameObject && !EventSystem.current.currentSelectedGameObject.activeInHierarchy)))
{
EventSystem.current.SetSelectedGameObject(null);
prevSelected = (currentSelected = null);
if (UtilitiesInput.rewiredPlayer != null && UtilitiesInput.rewiredPlayer.controllers.joystickCount > 0)
{
SelectFirstObject();
}
else if (uiSubmitWasPressed)
{
SelectFirstObject();
}
}
}
private void OnDisable()
{
prevSelected = (currentSelected = null);
}
private void LateUpdate()
{
if ((bool)EventSystem.current)
{
currentSelected = EventSystem.current.currentSelectedGameObject;
}
else
{
currentSelected = null;
}
if ((bool)currentSelected && !currentSelected.activeInHierarchy)
{
currentSelected = null;
}
if (currentSelected == null && (UtilitiesInput.GetAxis("UIHorizontal") != 0f || UtilitiesInput.GetAxis("UIVertical") != 0f || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D)))
{
if ((bool)prevSelected && prevSelected.activeInHierarchy)
{
EventSystem.current.SetSelectedGameObject(prevSelected);
}
else
{
SelectFirstObject();
}
}
if (UtilitiesInput.GetButtonDown("UISubmit"))
{
uiSubmitWasPressed = true;
}
if ((bool)currentSelected)
{
prevSelected = currentSelected;
}
}
public void SelectFirstObject()
{
for (int i = 0; i < firstSelected.Count; i++)
{
if (firstSelected[i].activeInHierarchy)
{
if ((bool)EventSystem.current)
{
EventSystem.current.SetSelectedGameObject(firstSelected[i]);
}
uiSubmitWasPressed = false;
break;
}
}
}
}