MultTextHelper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using BeinLab.Util;
  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 enum TextType
  10. {
  11. UnKnow = 0,
  12. Text = 1,
  13. TextMesh = 2,
  14. TextMeshPro = 3,
  15. TextMeshProUGUI = 4
  16. }
  17. public class MultTextHelper : MonoBehaviour
  18. {
  19. private Text uiLabel;
  20. private TextMesh meshLabel;
  21. private TextMeshPro meshProLabel;
  22. private TextMeshProUGUI meshProUILabel;
  23. private TextType textType = TextType.UnKnow;
  24. [HideInInspector]
  25. public bool isClearOnNothing = true;
  26. /// <summary>
  27. /// 语言编号
  28. /// </summary>
  29. [HideInInspector]
  30. public string languageNum;
  31. #if UNITY_EDITOR
  32. [HideInInspector]
  33. public int languageIndex;
  34. #endif
  35. public void AutoGetComponent()
  36. {
  37. if (textType == TextType.UnKnow)
  38. {
  39. if (uiLabel = GetComponent<Text>())
  40. {
  41. textType = TextType.Text;
  42. }
  43. else if (meshLabel = GetComponent<TextMesh>())
  44. {
  45. textType = TextType.TextMesh;
  46. }
  47. else if (meshProLabel = GetComponent<TextMeshPro>())
  48. {
  49. textType = TextType.TextMeshPro;
  50. }
  51. else if (meshProUILabel = GetComponent<TextMeshProUGUI>())
  52. {
  53. textType = TextType.TextMeshProUGUI;
  54. }
  55. if (textType == TextType.UnKnow)
  56. {
  57. UnityLog.Instance.LogError(gameObject + " is not Text");
  58. }
  59. else
  60. {
  61. if (LanguageMgr.Instance != null)
  62. {
  63. LanguageMgr.Instance.changeLanuage += OnChangeLaunge;
  64. if (LanguageMgr.Instance.CurLanguage != null)
  65. {
  66. OnChangeLaunge(LanguageMgr.Instance.CurLanguage);
  67. }
  68. }
  69. }
  70. }
  71. }
  72. private void OnDestroy()
  73. {
  74. if (LanguageMgr.Instance != null)
  75. {
  76. LanguageMgr.Instance.changeLanuage -= OnChangeLaunge;
  77. }
  78. }
  79. /// <summary>
  80. /// 监听到语言变化时
  81. /// </summary>
  82. /// <param name="obj"></param>
  83. private void OnChangeLaunge(LanguagePackConf conf)
  84. {
  85. if (conf != null && !string.IsNullOrEmpty(languageNum))
  86. {
  87. //UnityLog.Instance.Log(conf.Language);
  88. LanguageConf lc = conf.GetLanguage(languageNum);
  89. if (lc != null)
  90. {
  91. SetRealMsg(lc.Message);
  92. }
  93. else if (isClearOnNothing)
  94. {
  95. SetRealMsg("");
  96. }
  97. }
  98. else
  99. {
  100. //UnityLog.Instance.LogError("语言包为空" + conf);
  101. }
  102. }
  103. private void Awake()
  104. {
  105. AutoGetComponent();
  106. }
  107. private void Start()
  108. {
  109. SetLanuageNum(languageNum);
  110. }
  111. private void OnEnable()
  112. {
  113. SetLanuageNum(languageNum);
  114. }
  115. public void SetRealMsg(string msg)
  116. {
  117. try
  118. {
  119. if (gameObject)
  120. {
  121. if (textType == TextType.Text)
  122. {
  123. uiLabel.text = msg;
  124. }
  125. else if (textType == TextType.TextMesh)
  126. {
  127. meshLabel.text = msg;
  128. }
  129. else if (textType == TextType.TextMeshPro)
  130. {
  131. meshProLabel.text = msg;
  132. }
  133. else if (textType == TextType.TextMeshProUGUI)
  134. {
  135. meshProUILabel.text = msg;
  136. }
  137. else
  138. {
  139. UnityLog.Instance.LogError(textType.ToString());
  140. }
  141. }
  142. }
  143. catch
  144. {
  145. OnDestroy();
  146. }
  147. }
  148. /// <summary>
  149. /// 设置语言编号
  150. /// </summary>
  151. /// <param name="num"></param>
  152. public void SetLanuageNum(string num)
  153. {
  154. if (!string.IsNullOrEmpty(num))
  155. {
  156. this.languageNum = num;
  157. if (LanguageMgr.Instance.IsInit)
  158. {
  159. if (LanguageMgr.Instance.CurLanguage != null)
  160. {
  161. OnChangeLaunge(LanguageMgr.Instance.CurLanguage);
  162. }
  163. }
  164. else
  165. {
  166. if (TimerMgr.Instance)
  167. {
  168. TimerMgr.Instance.CreateTimer(() => { SetLanuageNum(num); }, 0.05f, 1);
  169. }
  170. else {
  171. TimerMgr.InitComplte += () =>
  172. {
  173. TimerMgr.Instance.CreateTimer(() => { SetLanuageNum(num); }, 0.05f, 1);
  174. };
  175. }
  176. }
  177. }
  178. else
  179. {
  180. //UnityLog.Instance.LogError(gameObject + "--" + num + " numkey is null!");
  181. }
  182. }
  183. }