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

55 lines
1.4 KiB
C#

using AlicizaX;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(TextMeshProUGUI))]
public sealed class InputGlyphText : InputGlyphBehaviourBase
{
[SerializeField] private InputActionReference actionReference;
[SerializeField] private string compositePartName;
private TMP_Text _textField;
private string _templateText;
private string _cachedFormattedText;
protected override void OnEnable()
{
if (_textField == null)
{
_textField = GetComponent<TMP_Text>();
}
if (string.IsNullOrEmpty(_templateText) && _textField != null)
{
_templateText = _textField.text;
}
base.OnEnable();
}
protected override void RefreshGlyph()
{
if (actionReference == null || actionReference.action == null || _textField == null)
{
return;
}
string formattedText;
if (GlyphService.TryGetTMPTagForActionPath(actionReference, compositePartName, CurrentCategory, out string tag, out string displayFallback))
{
formattedText = Utility.Text.Format(_templateText, tag);
}
else
{
formattedText = Utility.Text.Format(_templateText, displayFallback);
}
if (_cachedFormattedText != formattedText)
{
_cachedFormattedText = formattedText;
_textField.text = formattedText;
}
}
}