123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
-
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- using XRTool.Util;
- public class MultTextHelper : MonoBehaviour
- {
- public bool isOnStart;
- public bool isOnEnd;
- public string startReg;
- public string endReg = " : ";
- public bool isSetDef = false;
- [HideInInspector]
- public bool isClearOnNothing = true;
- private TextMeshProUGUI label;
- /// <summary>
- /// 语言编号
- /// </summary>
- [HideInInspector]
- [SerializeField]
- private string languageNum;
- private bool isAutoChange = true;
- public TextMeshProUGUI Label
- {
- get
- {
- if (!label)
- {
- label = GetComponent<TextMeshProUGUI>();
- if (IsAutoChange)
- {
- LanguageMgr.Instance.changeLanuage += OnChangeLaunge;
- }
- //if (LanguageMgr.Instance.CurLanguage != null)
- //{
- // OnChangeLaunge(LanguageMgr.Instance.CurLanguage);
- //}
- }
- return label;
- }
- }
- public string LanguageNum { get => languageNum; set => SetLanuageNum(value.ToString()); }
- public bool IsAutoChange
- {
- get => isAutoChange; set
- {
- isAutoChange = value;
- if (!value)
- {
- LanguageMgr.Instance.changeLanuage -= OnChangeLaunge;
- }
- }
- }
- 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)
- {
- //UnityLog.Log(conf.Language);
- LanguageConf lc = LanguageMgr.Instance.GetMessage(LanguageNum.ToString());
- if (lc != null)
- {
- string msg = lc.Message;
- if (isOnStart || isOnEnd)
- {
- if (isOnStart)
- {
- msg = startReg + msg;
- }
- else if (isOnEnd)
- {
- msg += endReg;
- }
- }
- SetRealMsg(msg);
- }
- else
- {
- SetRealMsg("null:" + LanguageNum.ToString());
- if (isClearOnNothing)
- {
- SetRealMsg("");
- }
- //UnityLog.LogError("Exception:语言编号不存在:" + UnityUtil.GetGameObjectPath(transform));
- }
- }
- else
- {
- // UnityLog.LogError("Exception:语言包为空" + UnityUtil.GetGameObjectPath(transform));
- }
- }
- private void Awake()
- {
- if (isSetDef)
- {
- if (int.TryParse(Label.text, out int num))
- {
- LanguageNum = num.ToString();
- }
- }
- }
- private void Start()
- {
- SetLanuageNum(LanguageNum.ToString());
- //UnityLog.Log(LanguageMgr.Instance.CurLanguage + LanguageNum.ToString() + LanguageMgr.Instance.CurLanguage);
- }
- private void OnEnable()
- {
- if (Label)
- SetLanuageNum(LanguageNum.ToString());
- }
- public void SetRealMsg(string msg)
- {
- try
- {
- if (gameObject && Label)
- {
- Label.text = msg;
- }
- }
- catch
- {
- // UnityLog.LogError("设置文本错误=>" + name + msg);
- }
- }
- /// <summary>
- /// 设置语言编号
- /// </summary>
- /// <param name="num"></param>
- public void SetLanuageNum(string num)
- {
- Debug.Log(num);
- if (!string.IsNullOrEmpty(num) && Label)
- {
- int kk;
- if (int.TryParse(num, out kk) || num.StartsWith("T"))
- {
- try
- {
- if (num.StartsWith("T"))
- {
- kk = int.Parse(num.Replace("T", ""));
- }
- this.languageNum = kk.ToString();
- OnChangeLaunge(LanguageMgr.Instance.CurLanguage);
- }
- catch (Exception ex)
- {
- TimerMgr.Instance.CreateTimer(() =>
- {
- SetRealMsg(num);
- }, 0.1f);
- Debug.LogError("SetLanuageNum===>num ’s type is uncorrect:" + num);
- }
- }
- else
- {
- TimerMgr.Instance.CreateTimer(() =>
- {
- SetRealMsg(num);
- }, 0.1f);
- }
- //if (LanguageMgr.Instance.CurLanguage != null)
- //{
- // OnChangeLaunge(LanguageMgr.Instance.CurLanguage);
- //}
- //else
- //{
- // UnityLog.LogError(gameObject + "--" + num + " 当前语言包为空");
- //}
- }
- else
- {
- // UnityLog.LogError(gameObject + "--" + num + " numkey is null!");
- }
- }
- }
|