123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using Newtonsoft.Json;
- using PublicTools.XMLDataBase;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using XRTool.UI;
- 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;
- /// <summary>
- /// 初始化设置语言下标为-1,代表尚未读取到任何语言包
- /// </summary>
- 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<LanguagePackConf> LanguagePackList { get => languagePackList; set => languagePackList = value; }
- /// <summary>
- /// 当开始引用的时候创建实例
- /// 读取配置表
- /// </summary>
- public LanguageMgr()
- {
- //InitLanguage();
- if (DataConfMgr<LanguagePackConf>.Instance.TableHelper == null)
- {
- ;
- DataConfMgr<LanguagePackConf>.Instance.OpenData(Path.Combine(Application.streamingAssetsPath, "Language"));
- }
- if (!DataConfMgr<LanguagePackConf>.Instance.TableHelper.isInit)
- {
- DataConfMgr<LanguagePackConf>.Instance.TableHelper.readConfComplete += OnReadConfComplete;
- DataConfMgr<LanguagePackConf>.Instance.OpenTable("Language");
- }
- else
- {
- OnReadConfComplete();
- }
- }
- private void OnReadConfComplete()
- {
- LanguagePackList = DataConfMgr<LanguagePackConf>.Instance.TableHelper.DataList;
- if (LanguagePackList == null || LanguagePackList.Count < 1)
- {
- UnityLog.LogError("InitPackage +没有找到语言包");
- }
- }
- /// <summary>
- /// 设置指定的语言
- /// </summary>
- /// <param name="lan"></param>
- 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);
- }
- /// <summary>
- /// 选中某一个语言时,默认是中文
- /// </summary>
- /// <param name="index"></param>
- 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);
- }
- /// <summary>
- /// 通过Key值获取对应的语言
- /// </summary>
- /// <param name="prikey"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|