MultTextHelper.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. 
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using TMPro;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using XRTool.Util;
  9. public class MultTextHelper : MonoBehaviour
  10. {
  11. public bool isOnStart;
  12. public bool isOnEnd;
  13. public string startReg;
  14. public string endReg = " : ";
  15. public bool isSetDef = false;
  16. [HideInInspector]
  17. public bool isClearOnNothing = true;
  18. private TextMeshProUGUI label;
  19. /// <summary>
  20. /// 语言编号
  21. /// </summary>
  22. [HideInInspector]
  23. [SerializeField]
  24. private string languageNum;
  25. private bool isAutoChange = true;
  26. public TextMeshProUGUI Label
  27. {
  28. get
  29. {
  30. if (!label)
  31. {
  32. label = GetComponent<TextMeshProUGUI>();
  33. if (IsAutoChange)
  34. {
  35. LanguageMgr.Instance.changeLanuage += OnChangeLaunge;
  36. }
  37. //if (LanguageMgr.Instance.CurLanguage != null)
  38. //{
  39. // OnChangeLaunge(LanguageMgr.Instance.CurLanguage);
  40. //}
  41. }
  42. return label;
  43. }
  44. }
  45. public string LanguageNum { get => languageNum; set => SetLanuageNum(value.ToString()); }
  46. public bool IsAutoChange
  47. {
  48. get => isAutoChange; set
  49. {
  50. isAutoChange = value;
  51. if (!value)
  52. {
  53. LanguageMgr.Instance.changeLanuage -= OnChangeLaunge;
  54. }
  55. }
  56. }
  57. private void OnDestroy()
  58. {
  59. if (LanguageMgr.Instance != null)
  60. {
  61. LanguageMgr.Instance.changeLanuage -= OnChangeLaunge;
  62. }
  63. }
  64. /// <summary>
  65. /// 监听到语言变化时
  66. /// </summary>
  67. /// <param name="obj"></param>
  68. private void OnChangeLaunge(LanguagePackConf conf)
  69. {
  70. if (conf != null)
  71. {
  72. //UnityLog.Log(conf.Language);
  73. LanguageConf lc = LanguageMgr.Instance.GetMessage(LanguageNum.ToString());
  74. if (lc != null)
  75. {
  76. string msg = lc.Message;
  77. if (isOnStart || isOnEnd)
  78. {
  79. if (isOnStart)
  80. {
  81. msg = startReg + msg;
  82. }
  83. else if (isOnEnd)
  84. {
  85. msg += endReg;
  86. }
  87. }
  88. SetRealMsg(msg);
  89. }
  90. else
  91. {
  92. SetRealMsg("null:" + LanguageNum.ToString());
  93. if (isClearOnNothing)
  94. {
  95. SetRealMsg("");
  96. }
  97. //UnityLog.LogError("Exception:语言编号不存在:" + UnityUtil.GetGameObjectPath(transform));
  98. }
  99. }
  100. else
  101. {
  102. // UnityLog.LogError("Exception:语言包为空" + UnityUtil.GetGameObjectPath(transform));
  103. }
  104. }
  105. private void Awake()
  106. {
  107. if (isSetDef)
  108. {
  109. if (int.TryParse(Label.text, out int num))
  110. {
  111. LanguageNum = num.ToString();
  112. }
  113. }
  114. }
  115. private void Start()
  116. {
  117. SetLanuageNum(LanguageNum.ToString());
  118. //UnityLog.Log(LanguageMgr.Instance.CurLanguage + LanguageNum.ToString() + LanguageMgr.Instance.CurLanguage);
  119. }
  120. private void OnEnable()
  121. {
  122. if (Label)
  123. SetLanuageNum(LanguageNum.ToString());
  124. }
  125. public void SetRealMsg(string msg)
  126. {
  127. try
  128. {
  129. if (gameObject && Label)
  130. {
  131. Label.text = msg;
  132. }
  133. }
  134. catch
  135. {
  136. // UnityLog.LogError("设置文本错误=>" + name + msg);
  137. }
  138. }
  139. /// <summary>
  140. /// 设置语言编号
  141. /// </summary>
  142. /// <param name="num"></param>
  143. public void SetLanuageNum(string num)
  144. {
  145. Debug.Log(num);
  146. if (!string.IsNullOrEmpty(num) && Label)
  147. {
  148. int kk;
  149. if (int.TryParse(num, out kk) || num.StartsWith("T"))
  150. {
  151. try
  152. {
  153. if (num.StartsWith("T"))
  154. {
  155. kk = int.Parse(num.Replace("T", ""));
  156. }
  157. this.languageNum = kk.ToString();
  158. OnChangeLaunge(LanguageMgr.Instance.CurLanguage);
  159. }
  160. catch (Exception ex)
  161. {
  162. TimerMgr.Instance.CreateTimer(() =>
  163. {
  164. SetRealMsg(num);
  165. }, 0.1f);
  166. Debug.LogError("SetLanuageNum===>num ’s type is uncorrect:" + num);
  167. }
  168. }
  169. else
  170. {
  171. TimerMgr.Instance.CreateTimer(() =>
  172. {
  173. SetRealMsg(num);
  174. }, 0.1f);
  175. }
  176. //if (LanguageMgr.Instance.CurLanguage != null)
  177. //{
  178. // OnChangeLaunge(LanguageMgr.Instance.CurLanguage);
  179. //}
  180. //else
  181. //{
  182. // UnityLog.LogError(gameObject + "--" + num + " 当前语言包为空");
  183. //}
  184. }
  185. else
  186. {
  187. // UnityLog.LogError(gameObject + "--" + num + " numkey is null!");
  188. }
  189. }
  190. }