当前位置:   article > 正文

unity的多语言配置工具_unity多语言方案

unity多语言方案

demo下载:https://github.com/JSumC/LanguageExcel

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using OfficeOpenXml;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace LanguageExcel
  9. {
  10. public class LETool : MonoBehaviour
  11. {
  12. public static language Language=language.CN;
  13. [Header("表格文件夹")]
  14. public static string URL = Application.streamingAssetsPath;
  15. public static string ExcelName = "LE";
  16. private static bool m_loaded;
  17. public static event Action OnLangaugeChanged ;
  18. public class plot
  19. {
  20. public string IN,CN,EN,JP,FR,DE,ID,TH,TW;
  21. }
  22. public enum language
  23. {
  24. IN, CN,EN,JP,FR,DE,ID,TH,TW
  25. }
  26. public static List<plot> S_Plots= new List<plot>();
  27. public virtual void OnEnable()
  28. {
  29. initialization();
  30. }
  31. public void initialization()
  32. {
  33. if (!m_loaded)
  34. {
  35. LoadExcel();
  36. m_loaded = true;
  37. }
  38. }
  39. void LoadExcel()
  40. {
  41. foreach (var VARIABLE in ReadFile())
  42. {
  43. FileInfo fileInfo = new FileInfo(VARIABLE);
  44. if (VARIABLE.Contains(ExcelName))
  45. {
  46. using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
  47. {
  48. ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
  49. for (int Left = 2; Left < worksheet.Dimension.End.Row + 1; Left++) //根据行数遍历
  50. {
  51. if (worksheet.Cells[Left, 1].Value.ToString().Length>0)
  52. {
  53. plot Plot = new plot();
  54. Plot.IN= worksheet.Cells[Left, 1]?.Value?.ToString();
  55. Plot.CN= worksheet.Cells[Left, 2]?.Value?.ToString();
  56. Plot.EN= worksheet.Cells[Left, 3]?.Value?.ToString();
  57. Plot.JP= worksheet.Cells[Left, 4]?.Value?.ToString();
  58. Plot.FR= worksheet.Cells[Left, 5]?.Value?.ToString();
  59. Plot.DE = worksheet.Cells[Left, 6]?.Value?.ToString();
  60. Plot.ID = worksheet.Cells[Left, 7]?.Value?.ToString();
  61. Plot.TH = worksheet.Cells[Left, 8]?.Value?.ToString();
  62. Plot.TW = worksheet.Cells[Left, 9]?.Value?.ToString();
  63. S_Plots.Add(Plot);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. static List<string> ReadFile()
  71. {
  72. List<string> files = GetFiles(URL, "*.xlsx");
  73. List<string> GetFiles(string directory, string pattern)
  74. {
  75. List<string> files = new List<string>();
  76. foreach (var item in Directory.GetFiles(directory, pattern))
  77. {
  78. files.Add(item);
  79. }
  80. foreach (var item in Directory.GetDirectories(directory))
  81. {
  82. files.AddRange(GetFiles(item, pattern));
  83. }
  84. return files;
  85. }
  86. return files;
  87. }
  88. public static string Get(string t)
  89. {
  90. plot selectedPlot = S_Plots.FirstOrDefault(p => p.IN == t);
  91. string languageField = Language.ToString();
  92. string plotString = (string)selectedPlot.GetType().GetField(languageField).GetValue(selectedPlot);
  93. return plotString;
  94. }
  95. /// <summary>
  96. /// 切换到指定语言
  97. /// </summary>
  98. /// <param name="t"></param>
  99. public static void SetLanguage(language t)
  100. {
  101. if (t!=Language)
  102. {
  103. Language = t;
  104. OnLangaugeChanged?.Invoke();
  105. }
  106. }
  107. /// <summary>
  108. /// 切换到下个语言
  109. /// </summary>
  110. public static void SetLanguage()
  111. {
  112. int currentLanguageIndex = (int)Language;
  113. int totalLanguages = Enum.GetValues(typeof(language)).Length;
  114. int nextLanguageIndex = (currentLanguageIndex + 1) % totalLanguages;
  115. Language = (language)nextLanguageIndex;
  116. OnLangaugeChanged?.Invoke();
  117. }
  118. public static void SetText(Text t, string str)
  119. {
  120. t.text = Get(str);
  121. }
  122. }
  123. }

效果图:

测试用代码:

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using LanguageExcel;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class Demo : MonoBehaviour
  8. {
  9. public Button ChangeLanguageBTN;
  10. public Button ChangeStringBTN;
  11. public Text ShowText;
  12. public static string Targetstr="你好";//目标字符串
  13. private void OnEnable()
  14. {
  15. LETool.SetText(ShowText, Targetstr);
  16. LETool.OnLangaugeChanged += ()=>LETool.SetText(ShowText, Targetstr);
  17. ChangeLanguageBTN.onClick.AddListener(() =>
  18. {
  19. LETool.SetLanguage();
  20. });
  21. ChangeStringBTN.onClick.AddListener(() =>
  22. {
  23. Targetstr = Targetstr == "你好" ? "再见" : "你好";
  24. LETool.SetText(ShowText, Targetstr);
  25. });
  26. }
  27. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/106802
推荐阅读
相关标签
  

闽ICP备14008679号