com.alicizax.unity.ui.exten.../Runtime/UXComponent/Text/UXTextMeshPro.cs

49 lines
1.3 KiB
C#
Raw Normal View History

2025-08-06 10:56:25 +08:00
#if TEXTMESHPRO_SUPPORT
2025-02-07 16:15:34 +08:00
using TMPro;
namespace UnityEngine.UI
{
public class UXTextMeshPro : TextMeshProUGUI
{
2025-09-25 11:09:09 +08:00
[SerializeField] private int m_localizationID;
[SerializeField] private string m_localizationKey = "";
2025-04-07 13:56:27 +08:00
#if UNITY_EDITOR
protected override void OnValidate()
{
base.OnValidate();
2025-12-16 17:08:10 +08:00
if (!Application.isPlaying && !string.IsNullOrEmpty(m_localizationKey))
{
text = LocalizationRefreshHelper.GetPreviewLabel(m_localizationKey);
}
}
#endif
2025-02-07 16:15:34 +08:00
protected override void Start()
{
base.Start();
if (!Application.isPlaying) return;
ChangeLanguage();
}
protected void ChangeLanguage()
{
2025-09-25 11:09:09 +08:00
if (!string.IsNullOrEmpty(m_localizationKey) && !"None".Equals(m_localizationKey) && UXComponentExtensionsHelper.LocalizationHelper != null)
2025-02-07 16:15:34 +08:00
{
2025-09-25 11:09:09 +08:00
text = UXComponentExtensionsHelper.LocalizationHelper.GetString(m_localizationKey);
2025-02-07 16:15:34 +08:00
}
}
2025-05-15 15:34:12 +08:00
/// <summary>
/// 重新动态设置多语言
/// </summary>
/// <param name="localizationID"></param>
public void SetLocalization(string localizationID)
{
2025-09-25 11:09:09 +08:00
m_localizationKey = localizationID;
2025-05-15 15:34:12 +08:00
ChangeLanguage();
}
2025-02-07 16:15:34 +08:00
}
}
2025-08-06 10:56:25 +08:00
#endif