46 lines
1.2 KiB
C#
46 lines
1.2 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
using UnityEngine.UI;
|
|
|
|
namespace NEG.Localization
|
|
{
|
|
public class LocalizedText : MonoBehaviour
|
|
{
|
|
[SerializeField] private string key = "";
|
|
[SerializeField] private TMP_Text tmpText;
|
|
[SerializeField] private Text text;
|
|
|
|
private void Awake() => LocalizationManager.Register(SetText);
|
|
|
|
private void OnDestroy() => LocalizationManager.Unregister(SetText);
|
|
|
|
private void OnValidate()
|
|
{
|
|
if (tmpText != null || text != null)
|
|
return;
|
|
tmpText = GetComponent<TMP_Text>();
|
|
if (tmpText != null)
|
|
return;
|
|
text = GetComponent<Text>();
|
|
}
|
|
|
|
private void SetText(LocalizationProvider ctx)
|
|
{
|
|
if (text != null)
|
|
text.text = ctx.GetText(key);
|
|
if (tmpText != null)
|
|
tmpText.text = ctx.GetText(key);
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
public void SetTextForEditor()
|
|
{
|
|
if (text != null)
|
|
text.text = "_" + key;
|
|
if (tmpText != null)
|
|
tmpText.text = "_" + key;
|
|
}
|
|
#endif
|
|
}
|
|
} |