赞
踩
demo下载:https://github.com/JSumC/LanguageExcel
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using OfficeOpenXml;
- using UnityEngine;
- using UnityEngine.UI;
- namespace LanguageExcel
- {
- public class LETool : MonoBehaviour
- {
- public static language Language=language.CN;
- [Header("表格文件夹")]
- public static string URL = Application.streamingAssetsPath;
-
- public static string ExcelName = "LE";
- private static bool m_loaded;
- public static event Action OnLangaugeChanged ;
-
-
-
- public class plot
- {
- public string IN,CN,EN,JP,FR,DE,ID,TH,TW;
- }
- public enum language
- {
- IN, CN,EN,JP,FR,DE,ID,TH,TW
- }
-
- public static List<plot> S_Plots= new List<plot>();
- public virtual void OnEnable()
- {
- initialization();
- }
-
- public void initialization()
- {
- if (!m_loaded)
- {
- LoadExcel();
- m_loaded = true;
- }
-
- }
-
- void LoadExcel()
- {
- foreach (var VARIABLE in ReadFile())
- {
- FileInfo fileInfo = new FileInfo(VARIABLE);
- if (VARIABLE.Contains(ExcelName))
- {
- using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
- {
- ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
-
- for (int Left = 2; Left < worksheet.Dimension.End.Row + 1; Left++) //根据行数遍历
- {
-
- if (worksheet.Cells[Left, 1].Value.ToString().Length>0)
- {
- plot Plot = new plot();
- Plot.IN= worksheet.Cells[Left, 1]?.Value?.ToString();
- Plot.CN= worksheet.Cells[Left, 2]?.Value?.ToString();
- Plot.EN= worksheet.Cells[Left, 3]?.Value?.ToString();
- Plot.JP= worksheet.Cells[Left, 4]?.Value?.ToString();
- Plot.FR= worksheet.Cells[Left, 5]?.Value?.ToString();
- Plot.DE = worksheet.Cells[Left, 6]?.Value?.ToString();
- Plot.ID = worksheet.Cells[Left, 7]?.Value?.ToString();
- Plot.TH = worksheet.Cells[Left, 8]?.Value?.ToString();
- Plot.TW = worksheet.Cells[Left, 9]?.Value?.ToString();
- S_Plots.Add(Plot);
- }
-
-
-
-
- }
- }
-
-
- }
- }
-
- }
- static List<string> ReadFile()
- {
- List<string> files = GetFiles(URL, "*.xlsx");
-
-
- List<string> GetFiles(string directory, string pattern)
- {
- List<string> files = new List<string>();
- foreach (var item in Directory.GetFiles(directory, pattern))
- {
- files.Add(item);
- }
-
- foreach (var item in Directory.GetDirectories(directory))
- {
- files.AddRange(GetFiles(item, pattern));
- }
-
- return files;
- }
-
- return files;
- }
-
- public static string Get(string t)
- {
- plot selectedPlot = S_Plots.FirstOrDefault(p => p.IN == t);
- string languageField = Language.ToString();
- string plotString = (string)selectedPlot.GetType().GetField(languageField).GetValue(selectedPlot);
- return plotString;
- }
-
- /// <summary>
- /// 切换到指定语言
- /// </summary>
- /// <param name="t"></param>
- public static void SetLanguage(language t)
- {
- if (t!=Language)
- {
- Language = t;
- OnLangaugeChanged?.Invoke();
- }
-
- }
- /// <summary>
- /// 切换到下个语言
- /// </summary>
- public static void SetLanguage()
- {
- int currentLanguageIndex = (int)Language;
- int totalLanguages = Enum.GetValues(typeof(language)).Length;
- int nextLanguageIndex = (currentLanguageIndex + 1) % totalLanguages;
- Language = (language)nextLanguageIndex;
- OnLangaugeChanged?.Invoke();
- }
-
- public static void SetText(Text t, string str)
- {
- t.text = Get(str);
- }
- }
- }
效果图:
测试用代码:
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using LanguageExcel;
- using UnityEngine;
- using UnityEngine.UI;
-
- public class Demo : MonoBehaviour
- {
- public Button ChangeLanguageBTN;
- public Button ChangeStringBTN;
- public Text ShowText;
- public static string Targetstr="你好";//目标字符串
- private void OnEnable()
- {
- LETool.SetText(ShowText, Targetstr);
- LETool.OnLangaugeChanged += ()=>LETool.SetText(ShowText, Targetstr);
- ChangeLanguageBTN.onClick.AddListener(() =>
- {
- LETool.SetLanguage();
- });
- ChangeStringBTN.onClick.AddListener(() =>
- {
- Targetstr = Targetstr == "你好" ? "再见" : "你好";
- LETool.SetText(ShowText, Targetstr);
- });
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。