TextAutoLanguage2.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Newtonsoft.Json;
  6. using TMPro;
  7. /* 自定义多语言组件-2代 */
  8. public class TextAutoLanguage2 : MonoBehaviour
  9. {
  10. [SerializeField] string textKey;
  11. [SerializeField] RectTransform layoutRebuildObject;
  12. static LanguageEnum currentLanguageEnum = LanguageEnum.Chinese;
  13. static Dictionary<string, string> languageDictionary = null;
  14. static HashSet<TextAutoLanguage2> textAutoLanguages = new HashSet<TextAutoLanguage2>();
  15. public object[] textFormatArgs = {};
  16. [SerializeField] LanguageFontSize[] languageFontSizes = {};
  17. private static bool inited = false;
  18. // 语言变更后的事件
  19. public static event Action<LanguageEnum> OnLanguageChanged;
  20. public static void Init()
  21. {
  22. if (inited) return;
  23. inited = true;
  24. int id = PlayerPrefs.GetInt("Language", 0);
  25. ChangeLanguage((LanguageEnum) id);
  26. }
  27. public static void ChangeLanguage(LanguageEnum languageEnum)
  28. {
  29. string fileName = "cn";
  30. if (languageEnum == LanguageEnum.English) fileName = "en";
  31. languageDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(Resources.Load<TextAsset>("TextAutoLanguage2/" + fileName).text);
  32. currentLanguageEnum = languageEnum;
  33. PlayerPrefs.SetInt("Language", ((int)languageEnum));
  34. foreach (var textAutoLanguage in textAutoLanguages)
  35. {
  36. try {
  37. textAutoLanguage.ApplyToText();
  38. } catch (Exception e) { Debug.LogError(e.Message); }
  39. }
  40. // 触发语言变更的事件
  41. OnLanguageChanged?.Invoke(languageEnum);
  42. }
  43. public static LanguageEnum GetLanguage()
  44. {
  45. Init();
  46. return currentLanguageEnum;
  47. }
  48. public static string GetTextByKey(string textKey) {
  49. Init();
  50. return languageDictionary[textKey];
  51. }
  52. public static string GetTextByCNKey(string textKey) {
  53. Init();
  54. if (currentLanguageEnum == LanguageEnum.Chinese) {
  55. return textKey;
  56. }
  57. string resText = null;
  58. languageDictionary.TryGetValue(textKey, out resText);
  59. if (resText == null) {
  60. return textKey;
  61. }
  62. return resText;
  63. }
  64. void Awake()
  65. {
  66. Init();
  67. }
  68. void Start()
  69. {
  70. textAutoLanguages.Add(this);
  71. ApplyToText();
  72. }
  73. void OnDestroy()
  74. {
  75. textAutoLanguages.Remove(this);
  76. }
  77. /**记录组件的默认字体大小 */
  78. private int defaultFontSize = -1;
  79. public void SetTextKey(string textKey)
  80. {
  81. this.textKey = textKey;
  82. string text = languageDictionary[textKey];
  83. if (textFormatArgs.Length > 0)
  84. {
  85. text = String.Format(text, textFormatArgs);
  86. }
  87. Text textComp = GetComponent<Text>();
  88. if (textComp != null) {
  89. textComp.text = text;
  90. //如果有指定当前语言的字体大小,就更新字体大小,否则恢复默认字体大小
  91. if (defaultFontSize == -1)
  92. { //首次记录组件的默认字体大小
  93. defaultFontSize = textComp.fontSize;
  94. }
  95. else
  96. {
  97. textComp.fontSize = defaultFontSize;
  98. }
  99. if (languageFontSizes.Length > 0)
  100. {
  101. foreach (var languageFontSize in languageFontSizes)
  102. {
  103. if (languageFontSize.language == currentLanguageEnum)
  104. {
  105. if(languageFontSize.fontSize > 0) textComp.fontSize = languageFontSize.fontSize;
  106. if(languageFontSize.lineSpacing > 0) textComp.lineSpacing = languageFontSize.lineSpacing;
  107. break;
  108. }
  109. }
  110. }
  111. //重置指定节点的布局
  112. if (layoutRebuildObject)
  113. {
  114. LayoutRebuilder.ForceRebuildLayoutImmediate(layoutRebuildObject);
  115. }
  116. } else {
  117. TextMeshProUGUI textMeshProUGUI = GetComponent<TextMeshProUGUI>();
  118. textMeshProUGUI.text = text;
  119. //如果是中文,设置一个间距
  120. //if (currentLanguageEnum == LanguageEnum.Chinese)
  121. //{
  122. // textMeshProUGUI.lineSpacing = 31;
  123. //}
  124. //如果有指定当前语言的字体大小,就更新字体大小,否则恢复默认字体大小
  125. if (defaultFontSize == -1)
  126. { //首次记录组件的默认字体大小
  127. defaultFontSize = (int)textMeshProUGUI.fontSize;
  128. }
  129. else
  130. {
  131. textMeshProUGUI.fontSize = defaultFontSize;
  132. }
  133. if (languageFontSizes.Length > 0)
  134. {
  135. foreach (var languageFontSize in languageFontSizes)
  136. {
  137. if (languageFontSize.language == currentLanguageEnum)
  138. {
  139. if(languageFontSize.fontSize >0) textMeshProUGUI.fontSize = languageFontSize.fontSize;
  140. if (languageFontSize.lineSpacing > 0) textMeshProUGUI.lineSpacing = languageFontSize.lineSpacing;
  141. break;
  142. }
  143. }
  144. }
  145. }
  146. }
  147. public string GetTextKey()
  148. {
  149. return this.textKey;
  150. }
  151. public void ApplyToText()
  152. {
  153. SetTextKey(textKey);
  154. Text text = GetComponent<Text>();
  155. if(text) onApplyToNext?.Invoke(text);
  156. }
  157. public Action<Text> onApplyToNext;
  158. }
  159. [Serializable]
  160. public class LanguageFontSize {
  161. public LanguageEnum language;
  162. public int fontSize;
  163. public float lineSpacing;
  164. }
  165. public enum LanguageEnum {
  166. Chinese, English
  167. }