| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- 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<string, string> languageDictionary = null;
- static HashSet<TextAutoLanguage2> textAutoLanguages = new HashSet<TextAutoLanguage2>();
- public object[] textFormatArgs = {};
- [SerializeField] LanguageFontSize[] languageFontSizes = {};
- private static bool inited = false;
- // 语言变更后的事件
- public static event Action<LanguageEnum> OnLanguageChanged;
- 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.ApplyToText();
- } catch (Exception e) { Debug.LogError(e.Message); }
- }
- // 触发语言变更的事件
- OnLanguageChanged?.Invoke(languageEnum);
- }
- 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<Text>();
- 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>();
- 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<Text>();
- if(text) onApplyToNext?.Invoke(text);
- }
- public Action<Text> onApplyToNext;
- }
- [Serializable]
- public class LanguageFontSize {
- public LanguageEnum language;
- public int fontSize;
- public float lineSpacing;
- }
- public enum LanguageEnum {
- Chinese, English
- }
|