TextAutoLanguage2.cs 3.3 KB

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