2025-12-09 20:31:44 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
|
2026-03-09 20:38:15 +08:00
|
|
|
public sealed class InputGlyphImage : MonoBehaviour
|
2025-12-09 20:31:44 +08:00
|
|
|
{
|
|
|
|
|
[SerializeField] private InputActionReference actionReference;
|
2025-12-17 20:03:29 +08:00
|
|
|
[SerializeField] private Image targetImage;
|
|
|
|
|
[SerializeField] private bool hideIfMissing = false;
|
|
|
|
|
[SerializeField] private GameObject hideTargetObject;
|
2025-12-09 20:31:44 +08:00
|
|
|
|
2026-03-09 20:38:15 +08:00
|
|
|
private InputDeviceWatcher.InputDeviceCategory _cachedCategory;
|
|
|
|
|
private Sprite _cachedSprite;
|
|
|
|
|
|
2025-12-09 20:31:44 +08:00
|
|
|
void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
if (targetImage == null) targetImage = GetComponent<Image>();
|
|
|
|
|
InputDeviceWatcher.OnDeviceChanged += OnDeviceChanged;
|
2026-03-09 20:38:15 +08:00
|
|
|
_cachedCategory = InputDeviceWatcher.InputDeviceCategory.Keyboard;
|
2025-12-09 20:31:44 +08:00
|
|
|
UpdatePrompt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
InputDeviceWatcher.OnDeviceChanged -= OnDeviceChanged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnDeviceChanged(InputDeviceWatcher.InputDeviceCategory cat)
|
|
|
|
|
{
|
2026-03-09 20:38:15 +08:00
|
|
|
if (_cachedCategory != cat)
|
|
|
|
|
{
|
|
|
|
|
_cachedCategory = cat;
|
|
|
|
|
UpdatePrompt();
|
|
|
|
|
}
|
2025-12-09 20:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdatePrompt()
|
|
|
|
|
{
|
|
|
|
|
if (actionReference == null || actionReference.action == null || targetImage == null) return;
|
2026-03-09 20:38:15 +08:00
|
|
|
|
|
|
|
|
// Use cached category instead of re-querying CurrentCategory
|
|
|
|
|
if (GlyphService.TryGetUISpriteForActionPath(actionReference, "", _cachedCategory, out Sprite sprite))
|
2025-12-09 20:31:44 +08:00
|
|
|
{
|
2026-03-09 20:38:15 +08:00
|
|
|
if (_cachedSprite != sprite)
|
|
|
|
|
{
|
|
|
|
|
_cachedSprite = sprite;
|
|
|
|
|
targetImage.sprite = sprite;
|
|
|
|
|
}
|
2025-12-09 20:31:44 +08:00
|
|
|
}
|
2025-12-17 20:03:29 +08:00
|
|
|
|
2026-03-09 20:38:15 +08:00
|
|
|
if (hideTargetObject != null)
|
|
|
|
|
{
|
|
|
|
|
bool shouldBeActive = sprite != null && !hideIfMissing;
|
|
|
|
|
if (hideTargetObject.activeSelf != shouldBeActive)
|
|
|
|
|
{
|
|
|
|
|
hideTargetObject.SetActive(shouldBeActive);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-09 20:31:44 +08:00
|
|
|
}
|
|
|
|
|
}
|