49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
using UnityEngine;
|
||
using TMPro;
|
||
using Kill.Managers;
|
||
using UnityEngine.UI;
|
||
using System;
|
||
|
||
namespace Kill.UI.Components
|
||
{
|
||
public class MultiLanguageText : MonoBehaviour
|
||
{
|
||
[Tooltip("对应LanguageConfig.json中的键")]
|
||
public string textKey; // 在Inspector中设置,如"btn_start"
|
||
|
||
private TextMeshProUGUI textComponentPro; // 若用UGUI,改为Text
|
||
private Text textComponent;
|
||
Action<string> callback;
|
||
string callbackStr;
|
||
private void Awake()
|
||
{
|
||
textComponent = GetComponent<Text>();
|
||
textComponentPro = GetComponent<TextMeshProUGUI>();
|
||
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);
|
||
}
|
||
|
||
}
|
||
callback?.Invoke(callbackStr);
|
||
}
|
||
public void SetCallBack(Action<string> action,string str)
|
||
{
|
||
callback=action;
|
||
callbackStr=str;
|
||
}
|
||
}
|
||
} |