AlicizaX/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphUXButton.cs

65 lines
1.5 KiB
C#
Raw Normal View History

using UnityEngine;
2026-03-09 20:38:15 +08:00
using UnityEngine.InputSystem;
using UnityEngine.UI;
2026-03-09 20:38:15 +08:00
[RequireComponent(typeof(UXButton))]
public sealed class InputGlyphUXButton : InputGlyphBehaviourBase
2026-03-09 20:38:15 +08:00
{
[SerializeField] private UXButton button;
[SerializeField] private Image targetImage;
2026-03-09 20:38:15 +08:00
private Sprite _cachedSprite;
#if UNITY_EDITOR
private void OnValidate()
{
if (button == null)
{
button = GetComponent<UXButton>();
}
}
#endif
protected override void OnEnable()
2026-03-09 20:38:15 +08:00
{
if (button == null)
{
button = GetComponent<UXButton>();
}
2026-03-09 20:38:15 +08:00
if (targetImage == null)
2026-03-09 20:38:15 +08:00
{
targetImage = GetComponent<Image>();
2026-03-09 20:38:15 +08:00
}
base.OnEnable();
2026-03-09 20:38:15 +08:00
}
protected override void RefreshGlyph()
2026-03-09 20:38:15 +08:00
{
InputActionReference actionReference = button != null ? button.HotKeyRefrence : null;
if (actionReference == null || actionReference.action == null || targetImage == null)
2026-03-09 20:38:15 +08:00
{
if (targetImage != null && _cachedSprite != null)
2026-03-09 20:38:15 +08:00
{
_cachedSprite = null;
targetImage.sprite = null;
2026-03-09 20:38:15 +08:00
}
return;
}
bool hasSprite = GlyphService.TryGetUISpriteForActionPath(actionReference, string.Empty, CurrentCategory, out Sprite sprite);
if (!hasSprite)
{
sprite = null;
}
if (_cachedSprite != sprite)
{
_cachedSprite = sprite;
targetImage.sprite = sprite;
2026-03-09 20:38:15 +08:00
}
}
}