123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- using BeinLab.Util;
- using PublicTools.XMLDataBase;
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace XRTool.Util
- {
- /// <summary>
- /// 语言控制器
- /// 提供语言切换时事件
- /// 提供语言包读取,解析
- /// 语言包放到stream包中
- /// </summary>
- public class LanguageMgr : Singleton<LanguageMgr>
- {
- /// <summary>
- /// 改变语言的事件
- /// </summary>
- public event Action<LanguagePackConf> changeLanuage;
- /// <summary>
- /// 当前的语言
- /// </summary>
- private LanguagePackConf curLanguage;
- /// <summary>
- /// 语言包列表
- /// </summary>
- private List<LanguagePackConf> languagePackList;
- private string tableName;
- private TableInterface table;
- public int LanguageCount
- {
- get
- {
- if (languagePackList != null)
- {
- return languagePackList.Count;
- }
- return 0;
- }
- }
- //private int languageIndex = 0;
- /// <summary>
- /// 当开始引用的时候创建实例
- /// 读取配置表
- /// </summary>
- public LanguageMgr()
- {
- GameSession.OnFocusGame += OnFocusGame;
- InitLanguage();
- }
- private void InitLanguage()
- {
- if (BuildConfigMgr.Instance.IsInit)
- {
- if (TimerMgr.Instance)
- {
- TimerMgr.Instance.CreateTimer(() =>
- {
- ReadLanguage(Path.Combine(ResourcesManager.LocalPath, BuildConfig.Instance.languagePath));
- }, 0.5f);
- }
- else
- {
- TimerMgr.InitComplte += () =>
- {
- TimerMgr.Instance.CreateTimer(() =>
- {
- ReadLanguage(Path.Combine(ResourcesManager.LocalPath, BuildConfig.Instance.languagePath));
- }, 0.5f);
- };
- }
- }
- else
- {
- UnityLog.Instance.LogError("配置文件丢失!");
- }
- }
- private void OnFocusGame()
- {
- if (!IsInit)
- {
- if (TimerMgr.Instance)
- {
- TimerMgr.Instance.CreateTimer(OnFocusGame, 1f);
- }
- else
- {
- TimerMgr.InitComplte += () =>
- {
- TimerMgr.Instance.CreateTimer(OnFocusGame, 1f);
- };
- }
- return;
- }
- if (CurLanguage == null)
- {
- if (languagePackList == null || languagePackList.Count < SystemSettingMgr.Instance.settings.Language + 1)
- {
- UnityLog.Instance.LogError("没有找到语言包");
- InitLanguage();
- return;
- }
- else
- {
- CurLanguage = languagePackList[SystemSettingMgr.Instance.settings.Language];
- }
- }
- if (CurLanguage != null)
- {
- if (CurLanguage.LanguageMap == null)
- {
- CurLanguage.ReadLanguageMap();
- }
- changeLanuage?.Invoke(CurLanguage);
- IsInit = true;
- }
- }
- private bool isInit = false;
- public LanguagePackConf CurLanguage { get => curLanguage; set => curLanguage = value; }
- public bool IsInit { get => isInit; set => isInit = value; }
- /// <summary>
- /// 读取语言包
- /// </summary>
- public void ReadLanguage(string path)
- {
- //languageIndex = SystemSettingMgr.Instance.settings.Language;
- tableName = typeof(LanguagePackConf).Name;
- #if UNITY_EDITOR || !UNITY_ANDROID
- table = XSql.Instance.OpenTable(path, tableName, ".xml", true);
- OpenTable();
- #else
- string fullPath = Path.Combine(path, tableName + ".xml");
- if (GameSession.Instance)
- {
- GameSession.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List<LanguagePackConf> packList) =>
- {
- InitPackage(packList);
- }));
- }
- else
- {
- GameSession.InitComplte += () =>
- {
- GameSession.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List<LanguagePackConf> packList) =>
- {
- InitPackage(packList);
- }));
- };
- }
- #endif
- }
- /// <summary>
- /// 打开语言包配置表
- /// </summary>
- public void OpenTable()
- {
- if (!table.Open())
- {
- table.Create(tableName);
- UnityLog.Instance.Log("create table" + tableName);
- }
- //languagePackList = table.FindAllData<LanguagePackConf>();
- InitPackage(table.FindAllData<LanguagePackConf>());
- }
- public void InitPackage(List<LanguagePackConf> packList)
- {
- languagePackList = packList;
- if (languagePackList == null || languagePackList.Count < SystemSettingMgr.Instance.settings.Language + 1)
- {
- UnityLog.Instance.LogError("没有找到语言包");
- }
- else
- {
- CurLanguage = languagePackList[SystemSettingMgr.Instance.settings.Language];
- if (CurLanguage.LanguageMap == null)
- {
- CurLanguage.ReadLanguageMap();
- }
- changeLanuage?.Invoke(CurLanguage);
- IsInit = true;
- }
- }
- /// <summary>
- /// 选中某一个语言时,默认是中文
- /// </summary>
- /// <param name="index"></param>
- public void SelectLanguage(int index)
- {
- if (index >= 0 && index != SystemSettingMgr.Instance.settings.Language && index < languagePackList.Count)
- {
- SystemSettingMgr.Instance.settings.Language = index;
- CurLanguage = languagePackList[index];
- if (CurLanguage.LanguageMap == null)
- {
- CurLanguage.ReadLanguageMap();
- }
- changeLanuage?.Invoke(CurLanguage);
- SystemSettingMgr.Instance.SaveSettings();
- }
- else
- {
- UnityLog.Instance.Log("未切换语言" + index);
- }
- }
- /// <summary>
- /// 通过Key值获取对应的语言
- /// </summary>
- /// <param name="prikey"></param>
- /// <returns></returns>
- public LanguageConf GetMessage(string prikey)
- {
- if (!string.IsNullOrEmpty(prikey))
- {
- prikey = prikey.Trim();
- if (!prikey.StartsWith(BuildConfig.Instance.prefix))
- {
- prikey = BuildConfig.Instance.prefix + prikey;
- }
- if (CurLanguage != null)
- {
- return CurLanguage.GetLanguage(prikey);
- }
- }
- return null;
- }
- }
- }
|