TextAutoLanguage2.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Newtonsoft.Json;
  6. public class TextAutoLanguage2 : MonoBehaviour
  7. {
  8. [SerializeField] string textKey;
  9. [SerializeField] RectTransform layoutRebuildObject;
  10. static LanguageEnum currentLanguageEnum = LanguageEnum.Chinese;
  11. static Dictionary<string, string> languageDictionary = null;
  12. static HashSet<TextAutoLanguage2> textAutoLanguages = new HashSet<TextAutoLanguage2>();
  13. public object[] textFormatArgs = {};
  14. [SerializeField] LanguageFontSize[] languageFontSizes = {};
  15. private static bool inited = false;
  16. public static void Init()
  17. {
  18. if (inited) return;
  19. inited = true;
  20. int id = PlayerPrefs.GetInt("Language", 0);
  21. ChangeLanguage((LanguageEnum) id);
  22. }
  23. public static void ChangeLanguage(LanguageEnum languageEnum)
  24. {
  25. string fileName = "cn";
  26. if (languageEnum == LanguageEnum.English) fileName = "en";
  27. languageDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(Resources.Load<TextAsset>("TextAutoLanguage2/" + fileName).text);
  28. currentLanguageEnum = languageEnum;
  29. PlayerPrefs.SetInt("Language", ((int)languageEnum));
  30. foreach (var textAutoLanguage in textAutoLanguages)
  31. {
  32. try {
  33. textAutoLanguage.ApplyToText();
  34. } catch (Exception e) { Debug.LogError(e.Message); }
  35. }
  36. }
  37. public static LanguageEnum GetLanguage()
  38. {
  39. Init();
  40. return currentLanguageEnum;
  41. }
  42. public static string GetTextByKey(string textKey) {
  43. Init();
  44. return languageDictionary[textKey];
  45. }
  46. public static string GetTextByCNKey(string textKey) {
  47. Init();
  48. if (currentLanguageEnum == LanguageEnum.Chinese) {
  49. return textKey;
  50. }
  51. string resText = null;
  52. languageDictionary.TryGetValue(textKey, out resText);
  53. if (resText == null) {
  54. return textKey;
  55. }
  56. return resText;
  57. }
  58. void Awake()
  59. {
  60. Init();
  61. }
  62. void Start()
  63. {
  64. textAutoLanguages.Add(this);
  65. ApplyToText();
  66. }
  67. void OnDestroy()
  68. {
  69. textAutoLanguages.Remove(this);
  70. }
  71. /**记录组件的默认字体大小 */
  72. private int defaultFontSize = -1;
  73. public void SetTextKey(string textKey)
  74. {
  75. this.textKey = textKey;
  76. string text = languageDictionary[textKey];
  77. if (textFormatArgs.Length > 0)
  78. {
  79. text = String.Format(text, textFormatArgs);
  80. }
  81. Text textComp = GetComponent<Text>();
  82. textComp.text = text;
  83. //如果有指定当前语言的字体大小,就更新字体大小,否则恢复默认字体大小
  84. if (defaultFontSize == -1) { //首次记录组件的默认字体大小
  85. defaultFontSize = textComp.fontSize;
  86. } else {
  87. textComp.fontSize = defaultFontSize;
  88. }
  89. if (languageFontSizes.Length > 0) {
  90. foreach (var languageFontSize in languageFontSizes)
  91. {
  92. if (languageFontSize.language == currentLanguageEnum) {
  93. textComp.fontSize = languageFontSize.fontSize;
  94. break;
  95. }
  96. }
  97. }
  98. //重置指定节点的布局
  99. if (layoutRebuildObject)
  100. {
  101. LayoutRebuilder.ForceRebuildLayoutImmediate(layoutRebuildObject);
  102. }
  103. }
  104. public string GetTextKey()
  105. {
  106. return this.textKey;
  107. }
  108. public void ApplyToText()
  109. {
  110. SetTextKey(textKey);
  111. }
  112. }
  113. [Serializable]
  114. public class LanguageFontSize {
  115. public LanguageEnum language;
  116. public int fontSize;
  117. }
  118. public enum LanguageEnum {
  119. Chinese, English
  120. }