UILocalizeScript.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using System;
  2. using System.Collections;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class UILocalizeScript : MonoBehaviour
  7. {
  8. public string m_key;
  9. private string mContent;
  10. private object[] m_Values;
  11. private Text mText;
  12. private TextMeshProUGUI mTextMeshProUGUI;
  13. bool hasSeted = false;
  14. private void Awake()
  15. {
  16. mText = GetComponent<Text>();
  17. mTextMeshProUGUI = GetComponent<TextMeshProUGUI>();
  18. Inner_SetContent(m_key);
  19. EventMgr.AddEventListener<int>(ECoreEventType.EID_LANGUAGE_CHANGE, OnLanguageChange);
  20. }
  21. private void OnLanguageChange(CoreEvent<int> e)
  22. {
  23. OnChangeLang();
  24. }
  25. void OnEnable()
  26. {
  27. #if UNITY_EDITOR
  28. string old = mContent;
  29. Inner_SetContent(m_key);
  30. if (old != mContent)
  31. Refresh();
  32. #endif
  33. }
  34. void Start()
  35. {
  36. if (hasSeted) return;
  37. if (!string.IsNullOrEmpty(m_key))
  38. {
  39. Inner_SetContent(m_key);
  40. }
  41. Refresh();
  42. }
  43. private void OnDestroy()
  44. {
  45. EventMgr.RemoveEventListener<int>(ECoreEventType.EID_LANGUAGE_CHANGE, OnLanguageChange);
  46. }
  47. public void OnChangeLang()
  48. {
  49. if (!string.IsNullOrEmpty(m_key))
  50. {
  51. Inner_SetContent(m_key);
  52. Refresh();
  53. }
  54. }
  55. void Refresh()
  56. {
  57. if (mText == null)
  58. {
  59. mText = GetComponent<Text>();
  60. }
  61. if (!mTextMeshProUGUI)
  62. {
  63. mTextMeshProUGUI = GetComponent<TextMeshProUGUI>();
  64. }
  65. if (!string.IsNullOrEmpty(mContent))
  66. {
  67. string content;
  68. if (m_Values != null && m_Values.Length > 0)
  69. {
  70. content = string.Format(mContent, m_Values);
  71. }
  72. else
  73. {
  74. content = mContent;
  75. }
  76. if (mText)
  77. {
  78. mText.text = content;
  79. }
  80. if (mTextMeshProUGUI)
  81. {
  82. mTextMeshProUGUI.text = content;
  83. }
  84. }
  85. }
  86. void RefreshWithColor(string colorText)
  87. {
  88. if (mText == null)
  89. {
  90. mText = GetComponent<Text>();
  91. }
  92. if (!mTextMeshProUGUI)
  93. {
  94. mTextMeshProUGUI = GetComponent<TextMeshProUGUI>();
  95. }
  96. if (!string.IsNullOrEmpty(mContent))
  97. {
  98. string content;
  99. if (m_Values != null && m_Values.Length > 0)
  100. {
  101. content = string.Format(mContent, m_Values);
  102. }
  103. else
  104. {
  105. content = mContent;
  106. }
  107. colorText = colorText.Replace("%s", "{0}");
  108. content = string.Format(colorText, content);
  109. if (mText)
  110. {
  111. mText.text = content;
  112. }
  113. if (mTextMeshProUGUI)
  114. {
  115. mTextMeshProUGUI.text = content;
  116. }
  117. }
  118. else
  119. {
  120. CleanContent();
  121. }
  122. }
  123. public void SetContent(string contentKey)
  124. {
  125. hasSeted = true;
  126. if (!string.IsNullOrEmpty(contentKey))
  127. {
  128. m_key = contentKey;
  129. Inner_SetContent(m_key);
  130. Refresh();
  131. }
  132. else
  133. {
  134. CleanContent();
  135. }
  136. }
  137. public void CleanContent()
  138. {
  139. m_key = "";
  140. if (mText)
  141. {
  142. mText.text = "";
  143. }
  144. if (mTextMeshProUGUI)
  145. {
  146. mTextMeshProUGUI.text = "";
  147. }
  148. }
  149. public void SetValues(object[] values)
  150. {
  151. hasSeted = true;
  152. if (m_Values != values)
  153. {
  154. m_Values = values;
  155. Refresh();
  156. }
  157. }
  158. public void SetContentAndValues(string contentKey, object[] values)
  159. {
  160. hasSeted = true;
  161. bool isChanged = false;
  162. if (!string.IsNullOrEmpty(contentKey))
  163. {
  164. m_key = contentKey;
  165. Inner_SetContent(m_key);
  166. isChanged = true;
  167. }
  168. if (m_Values != values)
  169. {
  170. m_Values = values;
  171. isChanged = true;
  172. }
  173. if (isChanged)
  174. {
  175. Refresh();
  176. }
  177. }
  178. public void SetContentWithColor(string contentKey, string colorText)
  179. {
  180. hasSeted = true;
  181. if (!string.IsNullOrEmpty(contentKey))
  182. {
  183. m_key = contentKey;
  184. Inner_SetContent(m_key);
  185. RefreshWithColor(colorText);
  186. }
  187. else
  188. {
  189. CleanContent();
  190. }
  191. }
  192. public void SetValuesWithColor(object[] values, string colorText)
  193. {
  194. hasSeted = true;
  195. if (m_Values != values)
  196. {
  197. m_Values = values;
  198. RefreshWithColor(colorText);
  199. }
  200. }
  201. public void SetContentAndValuesWithColor(string contentKey, object[] values, string colorText)
  202. {
  203. hasSeted = true;
  204. bool isChanged = false;
  205. if (!string.IsNullOrEmpty(contentKey))
  206. {
  207. m_key = contentKey;
  208. Inner_SetContent(m_key);
  209. isChanged = true;
  210. }
  211. if (m_Values != values)
  212. {
  213. m_Values = values;
  214. isChanged = true;
  215. }
  216. if (isChanged)
  217. {
  218. RefreshWithColor(colorText);
  219. }
  220. }
  221. private void Inner_SetContent(string contentKey)
  222. {
  223. mContent = I18N.T(contentKey);
  224. mContent = mContent.Replace("\\n", "\n");
  225. }
  226. }