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