LanguageMgr.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using Newtonsoft.Json;
  2. using PublicTools.XMLDataBase;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEngine;
  7. using XRTool.UI;
  8. namespace XRTool.Util
  9. {
  10. /// <summary>
  11. /// 语言控制器
  12. /// 提供语言切换时事件
  13. /// 提供语言包读取,解析
  14. /// 语言包放到stream包中
  15. /// </summary>
  16. public class LanguageMgr : Singleton<LanguageMgr>
  17. {
  18. /// <summary>
  19. /// 改变语言的事件
  20. /// </summary>
  21. public event Action<LanguagePackConf> changeLanuage;
  22. /// <summary>
  23. /// 当前的语言包配置
  24. /// </summary>
  25. private LanguagePackConf curLanguage;
  26. /// <summary>
  27. /// 语言包列表
  28. /// </summary>
  29. private List<LanguagePackConf> languagePackList;
  30. /// <summary>
  31. /// 初始化设置语言下标为-1,代表尚未读取到任何语言包
  32. /// </summary>
  33. private int lanIndex = -1;
  34. public int LanguageCount
  35. {
  36. get
  37. {
  38. if (LanguagePackList != null)
  39. {
  40. return LanguagePackList.Count;
  41. }
  42. return 0;
  43. }
  44. }
  45. public LanguagePackConf CurLanguage { get => curLanguage; private set => curLanguage = value; }
  46. public int LanIndex { get => lanIndex; private set => lanIndex = value; }
  47. public List<LanguagePackConf> LanguagePackList { get => languagePackList; set => languagePackList = value; }
  48. /// <summary>
  49. /// 当开始引用的时候创建实例
  50. /// 读取配置表
  51. /// </summary>
  52. public LanguageMgr()
  53. {
  54. //InitLanguage();
  55. if (DataConfMgr<LanguagePackConf>.Instance.TableHelper == null)
  56. {
  57. ;
  58. DataConfMgr<LanguagePackConf>.Instance.OpenData(Path.Combine(Application.streamingAssetsPath, "Language"));
  59. }
  60. if (!DataConfMgr<LanguagePackConf>.Instance.TableHelper.isInit)
  61. {
  62. DataConfMgr<LanguagePackConf>.Instance.TableHelper.readConfComplete += OnReadConfComplete;
  63. DataConfMgr<LanguagePackConf>.Instance.OpenTable("Language");
  64. }
  65. else
  66. {
  67. OnReadConfComplete();
  68. }
  69. }
  70. private void OnReadConfComplete()
  71. {
  72. LanguagePackList = DataConfMgr<LanguagePackConf>.Instance.TableHelper.DataList;
  73. if (LanguagePackList == null || LanguagePackList.Count < 1)
  74. {
  75. UnityLog.LogError("InitPackage +没有找到语言包");
  76. }
  77. }
  78. /// <summary>
  79. /// 设置指定的语言
  80. /// </summary>
  81. /// <param name="lan"></param>
  82. public void SelectLanguage(string lan)
  83. {
  84. if (LanguagePackList == null)
  85. {
  86. UnityLog.LogError("SelectLanguage 语言包读取失败" + lan);
  87. return;
  88. }
  89. for (int i = 0; i < LanguagePackList.Count; i++)
  90. {
  91. if (LanguagePackList[i].Language == lan)
  92. {
  93. SelectLanguage(i);
  94. return;
  95. }
  96. }
  97. SelectLanguage(0);
  98. }
  99. /// <summary>
  100. /// 选中某一个语言时,默认是中文
  101. /// </summary>
  102. /// <param name="index"></param>
  103. public void SelectLanguage(int index)
  104. {
  105. if (LanguagePackList == null)
  106. {
  107. UnityLog.LogError("SelectLanguage 语言包读取失败" + index);
  108. return;
  109. }
  110. UnityLog.Log("SelectLanguage=>" + index);
  111. if (index >= 0 && index < LanguagePackList.Count && LanIndex != index)
  112. {
  113. var lar = LanguagePackList[index];
  114. if (CurLanguage != null && CurLanguage != lar)
  115. {
  116. CurLanguage.LanguageMap.Clear();
  117. CurLanguage.ChineseMap.Clear();
  118. CurLanguage.OnReadComplete -= OnReadComplete;
  119. }
  120. LanIndex = index;
  121. lar.OnReadComplete -= OnReadComplete;
  122. lar.OnReadComplete += OnReadComplete;
  123. lar.ReadLanguageMap();
  124. if (LanIndex == 0)
  125. {
  126. // SafetyAreaLanguageManager.Instance.ChangeLanguage(SafetyAreaLanguageEnum.Chinese);
  127. }
  128. else
  129. {
  130. // SafetyAreaLanguageManager.Instance.ChangeLanguage(SafetyAreaLanguageEnum.English);
  131. }
  132. }
  133. else
  134. {
  135. UnityLog.Log(LanguagePackList.Count + "未切换语言" + LanIndex + "index=>" + index);
  136. }
  137. }
  138. private void OnReadComplete(LanguagePackConf lar)
  139. {
  140. CurLanguage = lar;
  141. /////虽然可以即时完成,但还是故意做个延时,有个反应的时间
  142. //TimerMgr.Instance.CreateTimer(() =>
  143. //{
  144. //}, 0.2f);
  145. changeLanuage?.Invoke(lar);
  146. }
  147. /// <summary>
  148. /// 通过Key值获取对应的语言
  149. /// </summary>
  150. /// <param name="prikey"></param>
  151. /// <returns></returns>
  152. public LanguageConf GetMessage(string prikey)
  153. {
  154. if (CurLanguage == null || CurLanguage.LanguageMap.Count < 1)
  155. {
  156. UnityLog.LogError("LanguageConf=>null map:" + CurLanguage);
  157. return null;
  158. }
  159. if (!string.IsNullOrEmpty(prikey))
  160. {
  161. if (!prikey.StartsWith("T"))
  162. {
  163. prikey = "T" + prikey;
  164. }
  165. if (CurLanguage != null)
  166. {
  167. return CurLanguage.GetLanguage(prikey);
  168. }
  169. }
  170. LanguageConf la = new LanguageConf();
  171. la.message = prikey;
  172. UnityLog.LogError("LanguageConf=>null map:" + la.Message);
  173. return la;
  174. }
  175. }
  176. }