TextAutoLanguage2.cs 3.8 KB

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