UILocalizeScript.cs 5.1 KB

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