TextAutoLanguage2.cs 3.5 KB

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