AlicizaX/Client/Assets/InputGlyph/InputGlyphImage.cs

43 lines
1.3 KiB
C#
Raw Normal View History

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
using AlicizaX.InputGlyph;
[RequireComponent(typeof(Image))]
public class InputGlyphImage : MonoBehaviour
{
[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;
void OnEnable()
{
if (targetImage == null) targetImage = GetComponent<Image>();
InputDeviceWatcher.OnDeviceChanged += OnDeviceChanged;
UpdatePrompt();
}
void OnDisable()
{
InputDeviceWatcher.OnDeviceChanged -= OnDeviceChanged;
}
void OnDeviceChanged(InputDeviceWatcher.InputDeviceCategory cat)
{
UpdatePrompt();
}
void UpdatePrompt()
{
if (actionReference == null || actionReference.action == null || targetImage == null) return;
InputDeviceWatcher.InputDeviceCategory deviceCategory = InputDeviceWatcher.CurrentCategory;
2025-12-10 17:38:31 +08:00
if (GlyphService.TryGetUISpriteForActionPath(actionReference, deviceCategory, out Sprite sprite))
{
targetImage.sprite = sprite;
}
2025-12-17 20:03:29 +08:00
if (hideTargetObject) hideTargetObject.SetActive(sprite != null && !hideIfMissing);
}
}