rabidus-test/Assets/UITextDisplayBase.cs

38 lines
920 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(TMPro.TextMeshProUGUI))]
public class UITextDisplayBase : MonoBehaviour
{
protected TMPro.TextMeshProUGUI _text;
protected ITextChangable _textChanger;
[SerializeField]
protected string _defaultValue = string.Empty;
[SerializeField]
protected string _prefix;
[SerializeField]
protected string _postfix;
protected virtual void Awake()
{
_text = GetComponent<TMPro.TextMeshProUGUI>();
_text.SetText(_defaultValue);
}
protected virtual void OnEnable()
{
_textChanger.OnTextChange.AddListener(UpdateText);
}
protected virtual void OnDisable()
{
_textChanger.OnTextChange.RemoveListener(UpdateText);
}
private void UpdateText(object obj)
{
_text.SetText(_prefix + obj.ToString() + _postfix);
}
}