using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Newtonsoft.Json; using TMPro; /* 自定义多语言组件-2代 */ public class TextAutoLanguage2 : MonoBehaviour { [SerializeField] string textKey; [SerializeField] RectTransform layoutRebuildObject; static LanguageEnum currentLanguageEnum = LanguageEnum.Chinese; static Dictionary languageDictionary = null; static HashSet textAutoLanguages = new HashSet(); public object[] 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>(Resources.Load("TextAutoLanguage2/" + fileName).text); currentLanguageEnum = languageEnum; PlayerPrefs.SetInt("Language", ((int)languageEnum)); foreach (var textAutoLanguage in textAutoLanguages) { try { textAutoLanguage.ApplyToText(); } catch (Exception e) { Debug.LogError(e.Message); } } } public static LanguageEnum GetLanguage() { Init(); return currentLanguageEnum; } public static string GetTextByKey(string textKey) { Init(); return languageDictionary[textKey]; } public static string GetTextByCNKey(string textKey) { Init(); if (currentLanguageEnum == LanguageEnum.Chinese) { return textKey; } string resText = null; languageDictionary.TryGetValue(textKey, out resText); if (resText == null) { return textKey; } return resText; } void Awake() { Init(); } void Start() { textAutoLanguages.Add(this); ApplyToText(); } 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(); if (textComp != null) { 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) { if(languageFontSize.fontSize > 0) textComp.fontSize = languageFontSize.fontSize; if(languageFontSize.lineSpacing > 0) textComp.lineSpacing = languageFontSize.lineSpacing; break; } } } //重置指定节点的布局 if (layoutRebuildObject) { LayoutRebuilder.ForceRebuildLayoutImmediate(layoutRebuildObject); } } else { TextMeshProUGUI textMeshProUGUI = GetComponent(); textMeshProUGUI.text = text; //如果是中文,设置一个间距 //if (currentLanguageEnum == LanguageEnum.Chinese) //{ // textMeshProUGUI.lineSpacing = 31; //} //如果有指定当前语言的字体大小,就更新字体大小,否则恢复默认字体大小 if (defaultFontSize == -1) { //首次记录组件的默认字体大小 defaultFontSize = (int)textMeshProUGUI.fontSize; } else { textMeshProUGUI.fontSize = defaultFontSize; } if (languageFontSizes.Length > 0) { foreach (var languageFontSize in languageFontSizes) { if (languageFontSize.language == currentLanguageEnum) { if(languageFontSize.fontSize >0) textMeshProUGUI.fontSize = languageFontSize.fontSize; if (languageFontSize.lineSpacing > 0) textMeshProUGUI.lineSpacing = languageFontSize.lineSpacing; break; } } } } } public string GetTextKey() { return this.textKey; } public void ApplyToText() { SetTextKey(textKey); Text text = GetComponent(); if(text) onApplyToNext?.Invoke(text); } public Action onApplyToNext; } [Serializable] public class LanguageFontSize { public LanguageEnum language; public int fontSize; public float lineSpacing; } public enum LanguageEnum { Chinese, English }