2026-03-17 20:02:47 +08:00
|
|
|
|
using UnityEngine;
|
2025-12-09 20:31:44 +08:00
|
|
|
|
using UnityEngine.InputSystem;
|
2026-03-17 20:02:47 +08:00
|
|
|
|
using UnityEngine.UI;
|
2025-12-09 20:31:44 +08:00
|
|
|
|
|
2026-03-17 20:02:47 +08:00
|
|
|
|
public sealed class InputGlyphImage : InputGlyphBehaviourBase
|
2025-12-09 20:31:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
[SerializeField] private InputActionReference actionReference;
|
2026-03-17 20:08:24 +08:00
|
|
|
|
[SerializeField] private string compositePartName;
|
2025-12-17 20:03:29 +08:00
|
|
|
|
[SerializeField] private Image targetImage;
|
|
|
|
|
|
[SerializeField] private bool hideIfMissing = false;
|
|
|
|
|
|
[SerializeField] private GameObject hideTargetObject;
|
2025-12-09 20:31:44 +08:00
|
|
|
|
|
2026-03-09 20:38:15 +08:00
|
|
|
|
private Sprite _cachedSprite;
|
|
|
|
|
|
|
2026-03-17 20:02:47 +08:00
|
|
|
|
protected override void OnEnable()
|
2025-12-09 20:31:44 +08:00
|
|
|
|
{
|
2026-03-17 20:02:47 +08:00
|
|
|
|
if (targetImage == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
targetImage = GetComponent<Image>();
|
|
|
|
|
|
}
|
2025-12-09 20:31:44 +08:00
|
|
|
|
|
2026-03-17 20:02:47 +08:00
|
|
|
|
base.OnEnable();
|
2025-12-09 20:31:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 20:02:47 +08:00
|
|
|
|
protected override void RefreshGlyph()
|
2025-12-09 20:31:44 +08:00
|
|
|
|
{
|
2026-03-17 20:02:47 +08:00
|
|
|
|
if (actionReference == null || actionReference.action == null || targetImage == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (targetImage != null && _cachedSprite != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_cachedSprite = null;
|
|
|
|
|
|
targetImage.sprite = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ApplyVisibility(false);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 20:08:24 +08:00
|
|
|
|
bool hasSprite = GlyphService.TryGetUISpriteForActionPath(actionReference, compositePartName, CurrentCategory, out Sprite sprite);
|
2026-03-17 20:02:47 +08:00
|
|
|
|
if (_cachedSprite != sprite)
|
2026-03-09 20:38:15 +08:00
|
|
|
|
{
|
2026-03-17 20:02:47 +08:00
|
|
|
|
_cachedSprite = sprite;
|
|
|
|
|
|
targetImage.sprite = sprite;
|
2026-03-09 20:38:15 +08:00
|
|
|
|
}
|
2026-03-17 20:02:47 +08:00
|
|
|
|
|
|
|
|
|
|
ApplyVisibility(hasSprite && sprite != null);
|
2025-12-09 20:31:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 20:02:47 +08:00
|
|
|
|
private void ApplyVisibility(bool hasSprite)
|
2025-12-09 20:31:44 +08:00
|
|
|
|
{
|
2026-03-17 20:02:47 +08:00
|
|
|
|
if (hideTargetObject == null)
|
2025-12-09 20:31:44 +08:00
|
|
|
|
{
|
2026-03-17 20:02:47 +08:00
|
|
|
|
return;
|
2025-12-09 20:31:44 +08:00
|
|
|
|
}
|
2025-12-17 20:03:29 +08:00
|
|
|
|
|
2026-03-17 20:02:47 +08:00
|
|
|
|
bool shouldBeActive = !hideIfMissing || hasSprite;
|
|
|
|
|
|
if (hideTargetObject.activeSelf != shouldBeActive)
|
2026-03-09 20:38:15 +08:00
|
|
|
|
{
|
2026-03-17 20:02:47 +08:00
|
|
|
|
hideTargetObject.SetActive(shouldBeActive);
|
2026-03-09 20:38:15 +08:00
|
|
|
|
}
|
2025-12-09 20:31:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|