AlicizaX/Client/Assets/Scripts/CustomeModule/InputGlyph/InputGlyphIHotkeyTrigger.cs
2026-03-19 20:17:04 +08:00

66 lines
1.7 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
[RequireComponent(typeof(UXButton))]
public sealed class InputGlyphIHotkeyTrigger : InputGlyphBehaviourBase
{
[SerializeField] private Component button;
[SerializeField] private string compositePartName;
[SerializeField] private Image targetImage;
private Sprite _cachedSprite;
#if UNITY_EDITOR
private void OnValidate()
{
if (button == null && TryGetComponent(typeof(IHotkeyTrigger), out Component c))
{
button = c;
}
}
#endif
protected override void OnEnable()
{
if (button == null && TryGetComponent(typeof(IHotkeyTrigger), out Component c))
{
button = c;
}
if (targetImage == null)
{
targetImage = GetComponent<Image>();
}
base.OnEnable();
}
protected override void RefreshGlyph()
{
InputActionReference actionReference = button != null ? (button as IHotkeyTrigger).HotkeyAction : null;
if (actionReference == null || actionReference.action == null || targetImage == null)
{
if (targetImage != null && _cachedSprite != null)
{
_cachedSprite = null;
targetImage.sprite = null;
}
return;
}
bool hasSprite = GlyphService.TryGetUISpriteForActionPath(actionReference, compositePartName, CurrentCategory, out Sprite sprite);
if (!hasSprite)
{
sprite = null;
}
if (_cachedSprite != sprite)
{
_cachedSprite = sprite;
targetImage.sprite = sprite;
}
}
}