2023-03-17 23:41:38 -04:00

50 lines
1.2 KiB
C#

using JetBrains.Annotations;
using Sirenix.OdinInspector;
using TMPro;
using UnityEngine;
namespace JohnsonUtils.Canvases
{
public class TextUIComponent : UIComponentBase
{
#if ODIN_INSPECTOR
[Required]
#endif
[Header("Association"), SerializeField]
private TMP_Text text;
private string _initialText;
public string Text
{
get => text.text;
set => text.text = string.IsNullOrEmpty(value) ? string.Empty : value;
}
[UsedImplicitly]
public Color Color
{
get => text.color;
set => text.color = value;
}
private void Awake() => _initialText = text.text;
private void Start() => Debug.Assert(text, $"A {nameof(text)} must be assigned to a {nameof(TMP_Text)}");
public void ResetText() => text.text = _initialText;
[PublicAPI]
public void AddText(string textToAdd) => text.text += textToAdd;
[PublicAPI]
public void EraseText() => text.text = string.Empty;
private void OnValidate()
{
if (!text)
text = GetComponent<TMP_Text>();
}
}
}