Files
back_cantanBuilding/Assets/Scripts/UIExtend/TextLocalization.cs
2026-05-26 16:15:54 +08:00

96 lines
2.6 KiB
C#

using asap.core;
using System;
using TMPro;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
namespace GameCore
{
public enum TextType
{
Text, // UGUI Text
TextMeshPro, // TextMeshPro - Text
TextMeshPro_UGUI // TextMeshPro - Text(UI)
}
public class TextLocalization : MonoBehaviour
{
[SerializeField] private string localizationId;
[SerializeField] private TextType textType = TextType.TextMeshPro_UGUI;
private Text text;
private TextMeshProUGUI text_UGUI;
private TextMeshPro text_Pro;
private IDisposable disposable;
void Awake()
{
if (string.IsNullOrEmpty(localizationId))
{
return;
}
switch (textType)
{
case TextType.Text:
text = gameObject.GetComponent<Text>();
break;
case TextType.TextMeshPro_UGUI:
text_UGUI = gameObject.GetComponent<TextMeshProUGUI>();
break;
case TextType.TextMeshPro:
text_Pro = gameObject.GetComponent<TextMeshPro>();
break;
}
if (string.IsNullOrEmpty(localizationId))
{
return;
}
OnChangeLanguage();
disposable = GContext.OnEvent<ChangeLanguageEvent>().Subscribe(OnChangeLanguage);
}
void OnChangeLanguage(ChangeLanguageEvent changeLanguageEvent = null)
{
string localized_text = LocalizationMgr.GetText(localizationId);
if (!string.IsNullOrEmpty(localized_text))
{
switch (textType)
{
case TextType.Text:
if (text)
{
text.text = localized_text;
}
return;
case TextType.TextMeshPro_UGUI:
if (text_UGUI)
{
text_UGUI.text = localized_text;
}
return;
case TextType.TextMeshPro:
if (text_Pro)
{
text_Pro.text = localized_text;
}
return;
}
}
}
private void OnDestroy()
{
if (disposable != null)
{
disposable.Dispose();
disposable = null;
}
}
}
}