LocalizedTextureCfgMgr.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. public class LocalizedSpriteCfg
  7. {
  8. public string key;
  9. public string Language;
  10. public string AssetPath;
  11. public string OtherSetting;
  12. public string SetNativeSize;
  13. public LocalizedSpriteCfg()
  14. {
  15. key = "";
  16. Language = "";
  17. AssetPath = "";
  18. OtherSetting = "";
  19. SetNativeSize = "0";
  20. }
  21. public bool GetSetNativeSize()
  22. {
  23. return !string.IsNullOrEmpty(SetNativeSize) && SetNativeSize != "0";
  24. }
  25. }
  26. public class LocalizedTextureCfgMgr : Singleton<LocalizedTextureCfgMgr>
  27. {
  28. private string baseName;
  29. private string baseKey;
  30. private string curLanguageKey;
  31. private string oldLanguageKey;
  32. private Dictionary<string, Dictionary<string, LocalizedSpriteCfg>> cfgs = new Dictionary<string, Dictionary<string, LocalizedSpriteCfg>>();
  33. private Dictionary<string, LocalizedSpriteCfg> curCfg;
  34. public override void Init()
  35. {
  36. baseName = "LocalizeTextureCfg";
  37. baseKey = "_base";
  38. curLanguageKey = baseKey;
  39. oldLanguageKey = baseKey;
  40. ReadConfig();
  41. }
  42. private void ReadConfig()
  43. {
  44. string cfgname = GetLanguageCfgName(curLanguageKey);
  45. if (!cfgs.ContainsKey(curLanguageKey))
  46. {
  47. Dictionary<string, Dictionary<string, string>> cfgdata = ConfigMgr.Instance.getTable(cfgname);
  48. if (cfgdata == null)
  49. {
  50. return;
  51. }
  52. Dictionary<string, LocalizedSpriteCfg> cfg = new Dictionary<string, LocalizedSpriteCfg>();
  53. foreach (var item in cfgdata)
  54. {
  55. LocalizedSpriteCfg localizedSpriteCfg = new LocalizedSpriteCfg();
  56. localizedSpriteCfg.key = item.Key;
  57. Dictionary<string, string> _value = item.Value;
  58. localizedSpriteCfg.Language = _value["Language"];
  59. localizedSpriteCfg.AssetPath = _value["AssetPath"];
  60. localizedSpriteCfg.OtherSetting = _value["OtherSetting"];
  61. localizedSpriteCfg.SetNativeSize = _value["SetNativeSize"];
  62. cfg.Add(item.Key, localizedSpriteCfg);
  63. }
  64. cfgs.Add(curLanguageKey, cfg);
  65. curCfg = cfg;
  66. }
  67. else
  68. {
  69. curCfg = cfgs[curLanguageKey];
  70. }
  71. }
  72. public void SetLanguage(string language)
  73. {
  74. if (!string.IsNullOrEmpty(language) && language != curLanguageKey)
  75. {
  76. curLanguageKey = language;
  77. ReadConfig();
  78. }
  79. }
  80. private string GetLanguageCfgName(string lg)
  81. {
  82. return $"{baseName}{lg}";
  83. }
  84. /// <summary>
  85. ///
  86. /// </summary>
  87. /// <param name="key"></param>
  88. /// <param name="notbase">要求不是base才返回</param>
  89. /// <returns></returns>
  90. public LocalizedSpriteCfg GetLocalizedSpriteCfg(string key, bool notbase = true)
  91. {
  92. if (notbase && curLanguageKey == oldLanguageKey)
  93. {
  94. return null;
  95. }
  96. if (curCfg == null)
  97. {
  98. ReadConfig();
  99. }
  100. if (curCfg == null)
  101. {
  102. return null;
  103. }
  104. if (curCfg.ContainsKey(key))
  105. {
  106. return curCfg[key];
  107. }
  108. return null;
  109. }
  110. public string GetLocalize(string key, bool notbase = false)
  111. {
  112. if (notbase && curLanguageKey == oldLanguageKey)
  113. {
  114. return "";
  115. }
  116. if (curCfg == null)
  117. {
  118. ReadConfig();
  119. }
  120. if (curCfg == null)
  121. {
  122. return "";
  123. }
  124. if (curCfg.ContainsKey(key))
  125. {
  126. return curCfg[key].Language;
  127. }
  128. return "";
  129. }
  130. }