|
|
@@ -0,0 +1,118 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using UnityEngine;
|
|
|
+using UnityEngine.UI;
|
|
|
+using Newtonsoft.Json;
|
|
|
+
|
|
|
+public class TextAutoLanguage2 : MonoBehaviour
|
|
|
+{
|
|
|
+ [SerializeField] string textKey;
|
|
|
+ [SerializeField] RectTransform layoutRebuildObject;
|
|
|
+ static LanguageEnum currentLanguageEnum = LanguageEnum.Chinese;
|
|
|
+ static Dictionary<string, string> languageDictionary = null;
|
|
|
+ static HashSet<TextAutoLanguage2> textAutoLanguages = new HashSet<TextAutoLanguage2>();
|
|
|
+ public string[] textFormatArgs = {};
|
|
|
+ [SerializeField] LanguageFontSize[] languageFontSizes = {};
|
|
|
+
|
|
|
+ private static bool inited = false;
|
|
|
+ public static void Init()
|
|
|
+ {
|
|
|
+ if (inited) return;
|
|
|
+ inited = true;
|
|
|
+ int id = PlayerPrefs.GetInt("Language", 0);
|
|
|
+ ChangeLanguage((LanguageEnum) id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void ChangeLanguage(LanguageEnum languageEnum)
|
|
|
+ {
|
|
|
+ string fileName = "cn";
|
|
|
+ if (languageEnum == LanguageEnum.English) fileName = "en";
|
|
|
+ languageDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(Resources.Load<TextAsset>("TextAutoLanguage2/" + fileName).text);
|
|
|
+ currentLanguageEnum = languageEnum;
|
|
|
+ PlayerPrefs.SetInt("Language", ((int)languageEnum));
|
|
|
+ foreach (var textAutoLanguage in textAutoLanguages)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ textAutoLanguage.ApplyText();
|
|
|
+ } catch (Exception e) { Debug.LogError(e.Message); }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static LanguageEnum GetLanguage()
|
|
|
+ {
|
|
|
+ return currentLanguageEnum;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static string GetTextByKey(string textKey) {
|
|
|
+ Init();
|
|
|
+ return languageDictionary[textKey];
|
|
|
+ }
|
|
|
+
|
|
|
+ void Awake()
|
|
|
+ {
|
|
|
+ Init();
|
|
|
+ }
|
|
|
+
|
|
|
+ void Start()
|
|
|
+ {
|
|
|
+ textAutoLanguages.Add(this);
|
|
|
+ ApplyText();
|
|
|
+ }
|
|
|
+
|
|
|
+ void OnDestroy()
|
|
|
+ {
|
|
|
+ textAutoLanguages.Remove(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**记录组件的默认字体大小 */
|
|
|
+ private int defaultFontSize = -1;
|
|
|
+ public void SetTextKey(string textKey)
|
|
|
+ {
|
|
|
+ this.textKey = textKey;
|
|
|
+ string text = languageDictionary[textKey];
|
|
|
+ if (textFormatArgs.Length > 0)
|
|
|
+ {
|
|
|
+ text = String.Format(text, textFormatArgs);
|
|
|
+ }
|
|
|
+ Text textComp = GetComponent<Text>();
|
|
|
+ textComp.text = text;
|
|
|
+ //如果有指定当前语言的字体大小,就更新字体大小,否则恢复默认字体大小
|
|
|
+ if (defaultFontSize == -1) { //首次记录组件的默认字体大小
|
|
|
+ defaultFontSize = textComp.fontSize;
|
|
|
+ } else {
|
|
|
+ textComp.fontSize = defaultFontSize;
|
|
|
+ }
|
|
|
+ if (languageFontSizes.Length > 0) {
|
|
|
+ foreach (var languageFontSize in languageFontSizes)
|
|
|
+ {
|
|
|
+ if (languageFontSize.language == currentLanguageEnum) {
|
|
|
+ textComp.fontSize = languageFontSize.fontSize;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //重置指定节点的布局
|
|
|
+ if (layoutRebuildObject)
|
|
|
+ {
|
|
|
+ LayoutRebuilder.ForceRebuildLayoutImmediate(layoutRebuildObject);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public string GetTextKey()
|
|
|
+ {
|
|
|
+ return this.textKey;
|
|
|
+ }
|
|
|
+
|
|
|
+ void ApplyText()
|
|
|
+ {
|
|
|
+ SetTextKey(textKey);
|
|
|
+ }
|
|
|
+}
|
|
|
+[Serializable]
|
|
|
+public class LanguageFontSize {
|
|
|
+ public LanguageEnum language;
|
|
|
+ public int fontSize;
|
|
|
+}
|
|
|
+public enum LanguageEnum {
|
|
|
+ Chinese, English
|
|
|
+}
|