NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Framework/Localization/LocalizeText.cs

53 lines
1001 B
C#
Raw Permalink Normal View History

2023-08-22 19:08:45 +08:00
using Framework;
using TMPro;
using UnityEngine;
[RequireComponent(typeof(TMP_Text))]
2024-04-02 16:46:40 +08:00
[DisallowMultipleComponent]
2023-08-22 19:08:45 +08:00
public class LocalizeText: MonoBehaviour
{
public string text_key;
private void Start()
{
Localize();
}
void Localize()
{
2023-10-19 15:00:19 +08:00
if (text_key == null || text_key.Length <= 0) return;
var txt = GetComponent<TMP_Text>();
if (txt)
2023-08-22 19:08:45 +08:00
{
2023-10-19 15:00:19 +08:00
txt.text = StringManager.Instance.GetLocalizeTextByKey(text_key);
2023-08-22 19:08:45 +08:00
}
}
#if UNITY_EDITOR
private void OnValidate()
{
LocalizeOnInspector();
}
void LocalizeOnInspector()
{
string text = "";
if (text_key != null && text_key.Length > 0)
{
2023-10-19 15:00:19 +08:00
text = EditorTableManager.GetLocalizeText(text_key);
2023-08-22 19:08:45 +08:00
}
if (text != null)
{
var txt = GetComponent<TMP_Text>();
if (txt)
{
txt.text = text;
}
}
}
#endif
}