LanguageManager.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. public enum CurrentLanguage
  7. {
  8. Default,
  9. English,
  10. ChineseSimplified,
  11. ChineseTraditional,
  12. }
  13. public class MulLanStr
  14. {
  15. public string ID;
  16. public string Page;
  17. public MulLanStr(string pageAndID)
  18. {
  19. string[] strings = pageAndID.Split(new[] { "__" }, StringSplitOptions.None);
  20. Page = strings[0];
  21. ID = strings[1];
  22. }
  23. public override string ToString()
  24. {
  25. return Language.GetStr(Page, ID);
  26. }
  27. }
  28. public class LanguageManager : Regist
  29. {
  30. #region Config
  31. public static CurrentLanguage CurrentLanguage;
  32. public static Dictionary<Text, object[]> TextDic = new Dictionary<Text, object[]>();
  33. public static Dictionary<TextMesh, object[]> TextMeshDic = new Dictionary<TextMesh, object[]>();
  34. #endregion
  35. public static void Add(Text text, params object[] objs)
  36. {
  37. if (TextDic.ContainsKey(text))
  38. {
  39. TextDic[text] = objs;
  40. text.text = Translate(objs);
  41. }
  42. else
  43. {
  44. TextDic.Add(text, objs);
  45. text.text = Translate(objs);
  46. }
  47. }
  48. public static void Add(TextMesh textMesh, params object[] objs)
  49. {
  50. if (TextMeshDic.ContainsKey(textMesh))
  51. {
  52. TextMeshDic[textMesh] = objs;
  53. textMesh.text = Translate(objs);
  54. }
  55. else
  56. {
  57. TextMeshDic.Add(textMesh, objs);
  58. textMesh.text = Translate(objs);
  59. }
  60. }
  61. public static void SwitchLanguage(CurrentLanguage currentLanguage)
  62. {
  63. CurrentLanguage = currentLanguage;
  64. Language.SwitchLanguage(currentLanguage);
  65. Manager.SwitchLanguage();
  66. foreach (var kv in TextDic)
  67. {
  68. kv.Key.text = Translate(kv.Value);
  69. }
  70. foreach (var kv in TextMeshDic)
  71. {
  72. kv.Key.text = Translate(kv.Value);
  73. }
  74. foreach (var kv in AchieveManager.AchieveDic)
  75. {
  76. if (kv.Value.AchieveItem != null)
  77. {
  78. kv.Value.Lab.text = Achieve.GetDescription(kv.Value.Desc, kv.Value.TargetValue);
  79. }
  80. }
  81. for (int i = 0; i < Manager.SkillList.Count; i++)
  82. {
  83. Manager.SkillList[i].SwitchLanguage();
  84. }
  85. }
  86. public static string Translate(object[] objs)
  87. {
  88. string str = "";
  89. for (int i = 0; i < objs.Length; i++)
  90. {
  91. MulLanStr mulLanStr = objs[i] as MulLanStr;
  92. if (mulLanStr == null)
  93. {
  94. str += objs[i];
  95. }
  96. else
  97. {
  98. str += mulLanStr.ToString();
  99. }
  100. }
  101. return str;
  102. }
  103. }