123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- 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;
- /// <summary>
- /// 语言编号
- /// </summary>
- [HideInInspector]
- public string languageNum;
- #if UNITY_EDITOR
- [HideInInspector]
- public int languageIndex;
- #endif
- public void AutoGetComponent()
- {
- if (textType == TextType.UnKnow)
- {
- if (uiLabel = GetComponent<Text>())
- {
- textType = TextType.Text;
- }
- else if (meshLabel = GetComponent<TextMesh>())
- {
- textType = TextType.TextMesh;
- }
- else if (meshProLabel = GetComponent<TextMeshPro>())
- {
- textType = TextType.TextMeshPro;
- }
- else if (meshProUILabel = GetComponent<TextMeshProUGUI>())
- {
- 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;
- }
- }
- /// <summary>
- /// 监听到语言变化时
- /// </summary>
- /// <param name="obj"></param>
- 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();
- }
- }
- /// <summary>
- /// 设置语言编号
- /// </summary>
- /// <param name="num"></param>
- 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!");
- }
- }
- }
|