killapp/Assets/Scripts/UI/MultiLanguageText.cs

46 lines
1.2 KiB
C#
Raw Normal View History

2025-11-18 09:18:48 +08:00
using UnityEngine;
using TMPro;
2026-03-30 16:25:00 +08:00
using Kill.Base;
using UnityEngine.UI;
namespace Kill.UI
2025-11-18 09:18:48 +08:00
{
2026-03-30 16:25:00 +08:00
public class MultiLanguageText : MonoBehaviour
2025-11-18 09:18:48 +08:00
{
2026-03-30 16:25:00 +08:00
[Tooltip("对应LanguageConfig.json中的键")]
public string textKey; // 在Inspector中设置如"btn_start"
2025-11-18 09:18:48 +08:00
2026-03-30 16:25:00 +08:00
private TextMeshProUGUI textComponentPro; // 若用UGUI改为Text
private Text textComponent;
2025-11-18 09:18:48 +08:00
2026-03-30 16:25:00 +08:00
private void Awake()
2025-11-18 09:18:48 +08:00
{
2026-03-30 16:25:00 +08:00
textComponent = GetComponent<Text>();
textComponentPro = GetComponent<TextMeshProUGUI>();
}
private void Start()
{
UpdateText(); // 初始化显示
}
// 更新文本(语言切换时调用)
public void UpdateText()
{
if (!string.IsNullOrEmpty(textKey))
{
if (textComponentPro != null)
{
textComponentPro.text = LanguageManager.Instance.GetLanguage(textKey);
}
else if (textComponent != null)
{
textComponent.text = LanguageManager.Instance.GetLanguage(textKey);
}
}
2025-11-18 09:18:48 +08:00
}
}
}