using System; using System.Collections.Generic; using Excel2Json; using Fort23.Core; using Fort23.UTool; using UnityEngine; using UnityEngine.UI; using Utility; namespace Core.Language { public class LanguageManager : Singleton, ITextTool { public enum LanguageType { LanguageChineseConfig = 0, LanguageEnglishConfig, LanguageDEConfig, LanguageFRConfig, LanguageRUConfig, LanguageTHConfig, /// /// 越南語 /// LanguageVNConfig, } public TextAsset blockWord; private FontSettingConfig fontSettingConfig; //private LanguageChineseConfig textConfig; private Map allLangugeInfo = new Map(); // public Map allLoginLangugeInfo = new Map(); public Map fonts = new Map(); public LanguageType currLanguageType; private FontSettingConfig.LanguageConfig currConfig; private bool isLaod; public async CTask InitManager() { Debug.Log("开始FontSettingConfig"); AssetHandle assetBundle = await AssetBundleLoadManager.Instance.LoadAssetAsyncTask("FontSettingConfig.asset"); fontSettingConfig = assetBundle.AssetObject(); if (fontSettingConfig != null) { fontSettingConfig.Init(); } else { Debug.Log("加载失败FontSettingConfig"); } UGUITextTool.TextTool = this; await SwitchTextConfig(LanguageType.LanguageChineseConfig, null); } public async CTask SwitchTextConfig(LanguageType languageType, System.Action callBack) { if (currLanguageType == languageType && isLaod) { callBack?.Invoke(); return; } isLaod = true; this.currLanguageType = languageType; Debug.Log("SwitchTextConfig"); currConfig = fontSettingConfig.GetLanguageConfig(languageType); Debug.Log("SwitchTextConfig" + currConfig.fontConfig.Count); string configJson = languageType.ToString() + ".json"; AssetHandle assetBundle = await AssetBundleLoadManager.Instance.LoadAssetAsyncTask(configJson); TextAsset ta = assetBundle.AssetObject(); LanguageChineseConfigHolder configDataHolder = JsonManager.FromJson(ta.text); assetBundle.Release(); configDataHolder.Init(); allLangugeInfo.Clear(); List configList = configDataHolder.configList; for (int i = 0; i < configList.Count; i++) { allLangugeInfo.Add(configList[i].ID, configList[i]); } // configJson = "StartGame" + languageType + ".json"; // assetBundle = // await AssetBundleLoadManager.Instance.LoadAssetAsyncTask(configJson); // ta = assetBundle.AssetObject(); // StartGameLanguageChineseConfigHolder configDataHolder2 = JsonManager.FromJson(ta.text); // assetBundle.Release(); // configDataHolder2.Init(); // allLoginLangugeInfo.Clear(); // List configList2 = configDataHolder2.configList; // for (int i = 0; i < configList2.Count; i++) // { // allLoginLangugeInfo.Add(configList2[i].ID, configList2[i]); // } callBack?.Invoke(); // AIhelpService.Instance.ChangeLanguage(); } public int GetFontIndix(string fontName) { if (fontSettingConfig == null) { return 0; } for (int i = 0; i < fontSettingConfig.languageConfig.Count; i++) { List fontConfig = fontSettingConfig.languageConfig[i].fontConfig; for (int j = 0; j < fontConfig.Count; j++) { if (fontConfig[j].fontName.Equals(fontName)) { return fontConfig[j].index; } } } return 0; } // public void SetFont(UILabel label) // { // if (label.trueTypeFont == null) // { // return; // } // // string fontName = label.trueTypeFont.name; // int index = GetFontIndix(fontName); // if (currConfig == null) // { // return; // } // // for (int i = 0; i < currConfig.fontConfig.Count; i++) // { // if (currConfig.fontConfig[i].index == index) // { // fontName = currConfig.fontConfig[i].fontName; // break; // } // } // // if (fonts.ContainsKey(fontName)) // { // label.trueTypeFont = fonts[fontName]; // if (label.defaultConfig != null) // { // label.defaultConfig.defaultFont = label.trueTypeFont; // } // } // // LogTool.LogError("没有字体"+ currConfig.languageType.ToString()+" "+ index); // } // public void SetFont(UILabel uilabel, string fontName) // { // if (fonts.ContainsKey(fontName)) // { // uilabel.trueTypeFont = fonts[fontName]; // } // else // { // LoadResourceTask task = AssetBundleLoadManager.Instance.AddAssetTask(fontName, delegate(Object obj, object data) // { // if (!fonts.ContainsKey(fontName)) // { // fonts.Add(fontName, (Font) obj); // } // // uilabel.trueTypeFont = (Font) obj; // }, erroCallBack: delegate() { LogTool.Log("没找到这个字体"); }); // } // } public string Text(int id) { //return "-ok"; LanguageChineseConfig icd = new LanguageChineseConfig(); if (allLangugeInfo.TryGetValue(id, out icd)) { return icd.txt.Replace("\\n", "\n"); } return "无" + id; } public string Text(int id, params object[] data) { //return "-ok"; LanguageChineseConfig icd = new LanguageChineseConfig(); if (allLangugeInfo.TryGetValue(id, out icd)) { string d = icd.txt; d = StringForString(d, data); return d; } return "无"; } public string Text2(int id, string[] data) { //return "-ok"; LanguageChineseConfig icd = new LanguageChineseConfig(); if (allLangugeInfo.TryGetValue(id, out icd)) { string d = icd.txt; d = StringForString(d, data); return d; } return "无"; } public string Text2(int id, float[] data) { //return "-ok"; LanguageChineseConfig icd = new LanguageChineseConfig(); if (allLangugeInfo.TryGetValue(id, out icd)) { string d = icd.txt; d = StringForString(d, data); return d; } return "无"; } private string Text(int id, Array data) { //return "-ok"; LanguageChineseConfig icd = new LanguageChineseConfig(); if (allLangugeInfo.TryGetValue(id, out icd)) { string d = icd.txt; d = StringForString(d, data); return d; } return "无"; } public static string StringForString(string message, Array par) { try { if (par != null) { switch (par.Length) { case 0: return message; case 1: return string.Format(message, par.GetValue(0)); case 2: return string.Format(message, par.GetValue(0), par.GetValue(1)); case 3: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2)); case 4: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3)); case 5: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3), par.GetValue(4)); case 6: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3), par.GetValue(4), par.GetValue(5)); case 7: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3), par.GetValue(4), par.GetValue(5), par.GetValue(6)); case 8: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3), par.GetValue(4), par.GetValue(5), par.GetValue(6), par.GetValue(7)); case 9: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3), par.GetValue(4), par.GetValue(5), par.GetValue(6), par.GetValue(7), par.GetValue(8)); case 10: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3), par.GetValue(4), par.GetValue(5), par.GetValue(6), par.GetValue(7), par.GetValue(8), par.GetValue(9)); case 11: return string.Format(message, par.GetValue(0), par.GetValue(1), par.GetValue(2), par.GetValue(3), par.GetValue(4), par.GetValue(5), par.GetValue(6), par.GetValue(7), par.GetValue(8), par.GetValue(9), par.GetValue(10)); } } } catch (Exception e) { LogTool.Exception(e); return message; } return message; } /// /// 用于临时写,便于后期统一替换. /// /// /// public string Text(string tepStr, params object[] data) { if (tepStr == "") return ""; return StringForString(tepStr, data); } // public string ConfigFontText(UILabel label, string text) // { // if (text == null || text == FixationString.empty || text.Length < 3) // { // return text; // } // // if (text[0] != '[') // { // return text; // } // // if (text[2] != '=') // { // return text; // } // // char charData = text[1]; // System.Text.StringBuilder builder = null; // int endIndex = 0; // // builder = new System.Text.StringBuilder(); // int i = 3; // bool isPP = false; // for (; i < text.Length; i++) // { // char c = text[i]; // if (c != ']') // { // builder.Append(c); // } // else // { // isPP = true; // break; // } // } // // if (!isPP) // { // return text; // } // // string valueStr = builder.ToString(); // endIndex = i; // // switch (charData) // { // case 'S': // case 's': //大小 // int size = int.Parse(valueStr); // label.fontSize = size; // break; // case 'F': // case 'f': //字体 // SetFont(label, valueStr); // break; // case 'X': // case 'x': //x间距 // int x = int.Parse(valueStr); // label.spacingX = x; // break; // case 'Y': // case 'y': //y间距 // int y = int.Parse(valueStr); // label.spacingY = y; // break; // case 'C': // case 'c': //y间距 // int c = int.Parse(valueStr); // if (c == 1) // { // label.fontStyle = FontStyle.Bold; // } // // if (c == 2) // { // label.fontStyle = FontStyle.Normal; // } // // break; // case 'K': // case 'k': //y间距 // int k = int.Parse(valueStr); // if (k == 1) // { // label.fontStyle = FontStyle.Italic; // } // // if (k == 2) // { // label.fontStyle = FontStyle.Normal; // } // // break; // } // // if (builder != null) // { // text = text.Remove(0, endIndex + 1); // return ConfigFontText(label, text); // } // // return text; // } public string GetLanguageMassge(int id) { return Text(id); } } }