using BeinLab.Util; using System; using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; using XRTool.Util; public enum TextType { UnKnow = 0, Text = 1, TextMesh = 2, TextMeshPro = 3, TextMeshProUGUI = 4 } public class MultTextHelper : MonoBehaviour { private Text uiLabel; private TextMesh meshLabel; private TextMeshPro meshProLabel; private TextMeshProUGUI meshProUILabel; private TextType textType = TextType.UnKnow; [HideInInspector] public bool isClearOnNothing = true; /// /// 语言编号 /// [HideInInspector] public string languageNum; #if UNITY_EDITOR [HideInInspector] public int languageIndex; #endif public void AutoGetComponent() { if (textType == TextType.UnKnow) { if (uiLabel = GetComponent()) { textType = TextType.Text; } else if (meshLabel = GetComponent()) { textType = TextType.TextMesh; } else if (meshProLabel = GetComponent()) { textType = TextType.TextMeshPro; } else if (meshProUILabel = GetComponent()) { textType = TextType.TextMeshProUGUI; } if (textType == TextType.UnKnow) { UnityLog.Instance.LogError(gameObject + " is not Text"); } else { if (LanguageMgr.Instance != null) { LanguageMgr.Instance.changeLanuage += OnChangeLaunge; if (LanguageMgr.Instance.CurLanguage != null) { OnChangeLaunge(LanguageMgr.Instance.CurLanguage); } } } } } private void OnDestroy() { if (LanguageMgr.Instance != null) { LanguageMgr.Instance.changeLanuage -= OnChangeLaunge; } } /// /// 监听到语言变化时 /// /// private void OnChangeLaunge(LanguagePackConf conf) { if (conf != null && !string.IsNullOrEmpty(languageNum)) { //UnityLog.Instance.Log(conf.Language); LanguageConf lc = conf.GetLanguage(languageNum); if (lc != null) { SetRealMsg(lc.Message); } else if (isClearOnNothing) { SetRealMsg(""); } } else { //UnityLog.Instance.LogError("语言包为空" + conf); } } private void Awake() { AutoGetComponent(); } private void Start() { SetLanuageNum(languageNum); } private void OnEnable() { SetLanuageNum(languageNum); } public void SetRealMsg(string msg) { try { if (gameObject) { if (textType == TextType.Text) { uiLabel.text = msg; } else if (textType == TextType.TextMesh) { meshLabel.text = msg; } else if (textType == TextType.TextMeshPro) { meshProLabel.text = msg; } else if (textType == TextType.TextMeshProUGUI) { meshProUILabel.text = msg; } else { UnityLog.Instance.LogError(textType.ToString()); } } } catch { OnDestroy(); } } /// /// 设置语言编号 /// /// public void SetLanuageNum(string num) { if (!string.IsNullOrEmpty(num)) { this.languageNum = num; if (LanguageMgr.Instance.IsInit) { if (LanguageMgr.Instance.CurLanguage != null) { OnChangeLaunge(LanguageMgr.Instance.CurLanguage); } } else { if (TimerMgr.Instance) { TimerMgr.Instance.CreateTimer(() => { SetLanuageNum(num); }, 0.05f, 1); } else { TimerMgr.InitComplte += () => { TimerMgr.Instance.CreateTimer(() => { SetLanuageNum(num); }, 0.05f, 1); }; } } } else { //UnityLog.Instance.LogError(gameObject + "--" + num + " numkey is null!"); } } }