AlicizaX/Client/Assets/InputGlyph/InputGlyphImage.cs

41 lines
1.2 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;
private Image targetImage;
[SerializeField] private bool hideIfMissing = true;
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;
string path = GlyphService.GetBindingControlPath(actionReference);
if (GlyphService.TryGetUISpriteForActionPath(path, deviceCategory, out Sprite sprite))
{
targetImage.sprite = sprite;
}
}
}