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

63 lines
1.7 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public sealed class InputGlyphImage : MonoBehaviour
public sealed class InputGlyphImage : InputGlyphBehaviourBase
{
[SerializeField] private InputActionReference actionReference;
[SerializeField] private Image targetImage;
[SerializeField] private bool hideIfMissing = false;
[SerializeField] private GameObject hideTargetObject;
private Sprite _cachedSprite;
protected override void OnEnable()
{
if (targetImage == null)
{
targetImage = GetComponent<Image>();
}
base.OnEnable();
}
protected override void RefreshGlyph()
{
if (actionReference == null || actionReference.action == null || targetImage == null)
{
if (targetImage != null && _cachedSprite != null)
{
_cachedSprite = null;
targetImage.sprite = null;
}
ApplyVisibility(false);
return;
}
bool hasSprite = GlyphService.TryGetUISpriteForActionPath(actionReference, string.Empty, CurrentCategory, out Sprite sprite);
if (_cachedSprite != sprite)
{
_cachedSprite = sprite;
targetImage.sprite = sprite;
}
ApplyVisibility(hasSprite && sprite != null);
}
private void ApplyVisibility(bool hasSprite)
{
if (hideTargetObject == null)
{
return;
}
bool shouldBeActive = !hideIfMissing || hasSprite;
if (hideTargetObject.activeSelf != shouldBeActive)
{
hideTargetObject.SetActive(shouldBeActive);
}
}
}