TextAutoLanguage2.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. return currentLanguageEnum;
  40. }
  41. public static string GetTextByKey(string textKey) {
  42. Init();
  43. return languageDictionary[textKey];
  44. }
  45. public static string GetTextByCNKey(string textKey) {
  46. Init();
  47. if (currentLanguageEnum == LanguageEnum.Chinese) {
  48. return textKey;
  49. }
  50. string resText = null;
  51. languageDictionary.TryGetValue(textKey, out resText);
  52. if (resText == null) {
  53. return textKey;
  54. }
  55. return resText;
  56. }
  57. void Awake()
  58. {
  59. Init();
  60. }
  61. void Start()
  62. {
  63. textAutoLanguages.Add(this);
  64. ApplyToText();
  65. }
  66. void OnDestroy()
  67. {
  68. textAutoLanguages.Remove(this);
  69. }
  70. /**记录组件的默认字体大小 */
  71. private int defaultFontSize = -1;
  72. public void SetTextKey(string textKey)
  73. {
  74. this.textKey = textKey;
  75. string text = languageDictionary[textKey];
  76. if (textFormatArgs.Length > 0)
  77. {
  78. text = String.Format(text, textFormatArgs);
  79. }
  80. Text textComp = GetComponent<Text>();
  81. textComp.text = text;
  82. //如果有指定当前语言的字体大小,就更新字体大小,否则恢复默认字体大小
  83. if (defaultFontSize == -1) { //首次记录组件的默认字体大小
  84. defaultFontSize = textComp.fontSize;
  85. } else {
  86. textComp.fontSize = defaultFontSize;
  87. }
  88. if (languageFontSizes.Length > 0) {
  89. foreach (var languageFontSize in languageFontSizes)
  90. {
  91. if (languageFontSize.language == currentLanguageEnum) {
  92. textComp.fontSize = languageFontSize.fontSize;
  93. break;
  94. }
  95. }
  96. }
  97. //重置指定节点的布局
  98. if (layoutRebuildObject)
  99. {
  100. LayoutRebuilder.ForceRebuildLayoutImmediate(layoutRebuildObject);
  101. }
  102. }
  103. public string GetTextKey()
  104. {
  105. return this.textKey;
  106. }
  107. public void ApplyToText()
  108. {
  109. SetTextKey(textKey);
  110. }
  111. }
  112. [Serializable]
  113. public class LanguageFontSize {
  114. public LanguageEnum language;
  115. public int fontSize;
  116. }
  117. public enum LanguageEnum {
  118. Chinese, English
  119. }