ClientServer/Client/Assets/Scripts/UI/UISelectionManager.cs
TG9six 03a642d635 first push
first push
2025-09-06 17:17:39 +04:00

78 lines
2.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using TMPro;
public class UISelectionManager : MonoBehaviour
{
readonly List<Selectable> _selectables = new List<Selectable>();
void Update()
{
if (ShouldRestoreSelection())
{
RestoreSelection();
//if (EventSystem.current.currentSelectedGameObject != null)
//{
// Debug.Log($"Current Selection: {EventSystem.current.currentSelectedGameObject.name}");
//}
}
}
void RestoreSelection()
{
gameObject.GetComponentsInChildren<Selectable>(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<Selectable>(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;
}
}