123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- using BeinLab.Util;
- using PublicTools.XMLDataBase;
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace XRTool.Util
- {
-
-
-
-
-
-
- public class LanguageMgr : Singleton<LanguageMgr>
- {
-
-
-
- public event Action<LanguagePackConf> changeLanuage;
-
-
-
- private LanguagePackConf curLanguage;
-
-
-
- private List<LanguagePackConf> languagePackList;
- private string tableName;
- private TableInterface table;
- public int LanguageCount
- {
- get
- {
- if (languagePackList != null)
- {
- return languagePackList.Count;
- }
- return 0;
- }
- }
-
-
-
-
-
- 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; }
-
-
-
- public void ReadLanguage(string path)
- {
-
- 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
- }
-
-
-
- public void OpenTable()
- {
- if (!table.Open())
- {
- table.Create(tableName);
- UnityLog.Instance.Log("create table" + tableName);
- }
-
- 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;
- }
- }
-
-
-
-
- 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);
- }
- }
-
-
-
-
-
- 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;
- }
- }
- }
|