using Newtonsoft.Json; using PublicTools.XMLDataBase; using System; using System.Collections.Generic; using System.IO; using UnityEngine; using XRTool.UI; namespace XRTool.Util { /// /// 语言控制器 /// 提供语言切换时事件 /// 提供语言包读取,解析 /// 语言包放到stream包中 /// public class LanguageMgr : Singleton { /// /// 改变语言的事件 /// public event Action changeLanuage; /// /// 当前的语言包配置 /// private LanguagePackConf curLanguage; /// /// 语言包列表 /// private List languagePackList; /// /// 初始化设置语言下标为-1,代表尚未读取到任何语言包 /// private int lanIndex = -1; public int LanguageCount { get { if (LanguagePackList != null) { return LanguagePackList.Count; } return 0; } } public LanguagePackConf CurLanguage { get => curLanguage; private set => curLanguage = value; } public int LanIndex { get => lanIndex; private set => lanIndex = value; } public List LanguagePackList { get => languagePackList; set => languagePackList = value; } /// /// 当开始引用的时候创建实例 /// 读取配置表 /// public LanguageMgr() { //InitLanguage(); if (DataConfMgr.Instance.TableHelper == null) { ; DataConfMgr.Instance.OpenData(Path.Combine(Application.streamingAssetsPath, "Language")); } if (!DataConfMgr.Instance.TableHelper.isInit) { DataConfMgr.Instance.TableHelper.readConfComplete += OnReadConfComplete; DataConfMgr.Instance.OpenTable("Language"); } else { OnReadConfComplete(); } } private void OnReadConfComplete() { LanguagePackList = DataConfMgr.Instance.TableHelper.DataList; if (LanguagePackList == null || LanguagePackList.Count < 1) { UnityLog.LogError("InitPackage +没有找到语言包"); } } /// /// 设置指定的语言 /// /// public void SelectLanguage(string lan) { if (LanguagePackList == null) { UnityLog.LogError("SelectLanguage 语言包读取失败" + lan); return; } for (int i = 0; i < LanguagePackList.Count; i++) { if (LanguagePackList[i].Language == lan) { SelectLanguage(i); return; } } SelectLanguage(0); } /// /// 选中某一个语言时,默认是中文 /// /// public void SelectLanguage(int index) { if (LanguagePackList == null) { UnityLog.LogError("SelectLanguage 语言包读取失败" + index); return; } UnityLog.Log("SelectLanguage=>" + index); if (index >= 0 && index < LanguagePackList.Count && LanIndex != index) { var lar = LanguagePackList[index]; if (CurLanguage != null && CurLanguage != lar) { CurLanguage.LanguageMap.Clear(); CurLanguage.ChineseMap.Clear(); CurLanguage.OnReadComplete -= OnReadComplete; } LanIndex = index; lar.OnReadComplete -= OnReadComplete; lar.OnReadComplete += OnReadComplete; lar.ReadLanguageMap(); if (LanIndex == 0) { // SafetyAreaLanguageManager.Instance.ChangeLanguage(SafetyAreaLanguageEnum.Chinese); } else { // SafetyAreaLanguageManager.Instance.ChangeLanguage(SafetyAreaLanguageEnum.English); } } else { UnityLog.Log(LanguagePackList.Count + "未切换语言" + LanIndex + "index=>" + index); } } private void OnReadComplete(LanguagePackConf lar) { CurLanguage = lar; /////虽然可以即时完成,但还是故意做个延时,有个反应的时间 //TimerMgr.Instance.CreateTimer(() => //{ //}, 0.2f); changeLanuage?.Invoke(lar); } /// /// 通过Key值获取对应的语言 /// /// /// public LanguageConf GetMessage(string prikey) { if (CurLanguage == null || CurLanguage.LanguageMap.Count < 1) { UnityLog.LogError("LanguageConf=>null map:" + CurLanguage); return null; } if (!string.IsNullOrEmpty(prikey)) { if (!prikey.StartsWith("T")) { prikey = "T" + prikey; } if (CurLanguage != null) { return CurLanguage.GetLanguage(prikey); } } LanguageConf la = new LanguageConf(); la.message = prikey; UnityLog.LogError("LanguageConf=>null map:" + la.Message); return la; } } }