using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using TMPro; public class UISelectionManager : MonoBehaviour { readonly List _selectables = new List(); void Update() { if (ShouldRestoreSelection()) { RestoreSelection(); //if (EventSystem.current.currentSelectedGameObject != null) //{ // Debug.Log($"Current Selection: {EventSystem.current.currentSelectedGameObject.name}"); //} } } void RestoreSelection() { gameObject.GetComponentsInChildren(includeInactive: false, _selectables); foreach (var selectable in _selectables) { if (!(selectable is TMP_InputField)) { if (selectable.isActiveAndEnabled && selectable.interactable) { selectable.Select(); break; } } } } static bool ShouldRestoreSelection() { if (HasValidSelection()) { return false; } // The selection is permitted to be lost when a click is made outside of a selectable bool requireSelection = (!Input.mousePresent || Player.Instance.GetInputHandler().UsingGamePad); // Restore selection immediately or when any keyboard/controller input is detected. return requireSelection || IsAnyJoinedButtonDown(); } static bool HasValidSelection() { var selected = EventSystem.current.currentSelectedGameObject; return ( selected != null && selected.activeInHierarchy && selected.TryGetComponent(out var selectable) && selectable.interactable ); } static bool IsAnyJoinedButtonDown() { //for (var i = 0; i < 1; i++) //{ // var player = ReInput.players.GetPlayer(i); // if (player.isPlaying && (player.GetAnyButton() || player.GetAnyNegativeButton())) // { // return true; // } //} return false; } }