123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- using Excel;
- using PublicTools.XMLDataBase;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Linq;
- using UnityEditor;
- using UnityEngine;
- namespace XRTool.Util
- {
-
-
-
- public class ExcelToXml : EditorWindow
- {
-
-
-
- private string excelPath = "External/Studio2配置表.xlsx";
-
-
-
-
-
-
-
-
-
-
-
-
-
- private string rule = "0,PriKey|2,Message|3,AudioPath";
- private int startRow = 1;
- private bool isReadEnd;
- private int endRow = int.MaxValue;
-
-
-
- private bool isForeceUpdate;
- [MenuItem("XRTools/ExcelToXML")]
- static void ExcelToXML()
- {
- if (BuildConfigMgr.Instance.IsInit)
- {
- if (BuildConfig.Instance)
- {
- ExcelToXml bw = GetWindow(typeof(ExcelToXml), false, "Excel转XML", true) as ExcelToXml;
- bw.ShowPopup();
- bw.autoRepaintOnSceneChange = true;
- }
- }
- }
-
-
-
- private void OnGUI()
- {
- if (!BuildConfigMgr.Instance.IsInit)
- {
- return;
- }
-
- excelPath = EditorGUILayout.TextField("Excel路径", excelPath);
-
- BuildConfig.Instance.languagePath = EditorGUILayout.TextField("输出路径",
- BuildConfig.Instance.languagePath);
-
-
- rule = EditorGUILayout.TextField("转化规则", rule);
- startRow = EditorGUILayout.IntField("起始行", startRow);
- isReadEnd = EditorGUILayout.BeginToggleGroup("启用终止行", isReadEnd);
- endRow = EditorGUILayout.IntField("终止行", endRow);
- EditorGUILayout.EndToggleGroup();
- BuildConfig.Instance.prefix = EditorGUILayout.TextField("主键前缀", BuildConfig.Instance.prefix);
- isForeceUpdate = EditorGUILayout.ToggleLeft("强制刷新", isForeceUpdate);
-
- if (GUILayout.Button("转换", GUILayout.Width(100)))
- {
- ConversionToXML();
- }
- }
-
-
-
- public void ConversionToXML()
- {
- string path = Application.dataPath;
- path = path.Substring(0, path.Length - ("Assets".Length + 1));
- path = path + "/" + excelPath;
- if (!File.Exists(path))
- {
- UnityLog.Instance.LogError(path + " is null");
- return;
- }
-
- FileStream stream = File.Open(path, FileMode.OpenOrCreate);
- IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
- DataSet dataSet = excelReader.AsDataSet();
- stream.Close();
- excelReader.Close();
- if (dataSet == null)
- {
- UnityLog.Instance.LogError(path + " 读取失败!");
- stream.Close();
- excelReader.Close();
- return;
- }
-
- DataColumnCollection columns = dataSet.Tables[0].Columns;
-
- DataRowCollection rows = dataSet.Tables[0].Rows;
-
- List<string> elementList = new List<string>(rule.Split('|'));
- Dictionary<int, string> elementHash = new Dictionary<int, string>();
- for (int i = 0; i < elementList.Count; i++)
- {
-
- string[] keys = elementList[i].Split(',');
- elementHash.Add(int.Parse(keys[0]), keys[1]);
- }
- foreach (var item in elementHash)
- {
- UnityLog.Instance.Log(item.Key + "" + item.Value);
- }
-
- int maxRow = isReadEnd && endRow < rows.Count ? endRow : rows.Count;
-
- string realDataPath = Path.Combine(Application.streamingAssetsPath, BuildConfig.Instance.languagePath);
- if (!Directory.Exists(realDataPath))
- {
- Directory.CreateDirectory(realDataPath);
- }
-
-
- try
- {
-
- if (true)
- {
-
- string languagePack = dataSet.Tables[0].Rows[0][elementHash.Keys.ToArray()[1]].ToString();
- LanguagePackConf conf = AutoAddPack(realDataPath, languagePack);
- if (conf == null)
- {
- UnityLog.Instance.LogError("转换语言包失败" + languagePack);
- return;
- }
- string tableName = typeof(LanguageConf).Name;
- XSql.Instance.CloseTable(tableName);
- var table = XSql.Instance.OpenTable(realDataPath + "/" + conf.PackagePath, tableName, ".xml", true);
- if (!table.Open())
- {
- table.Create(tableName);
- }
- for (int i = startRow; i < maxRow; i++)
- {
-
- Dictionary<string, string> valuePairs = new Dictionary<string, string>();
- foreach (var item in elementHash)
- {
-
- string keyValue = dataSet.Tables[0].Rows[i][item.Key].ToString();
- if (string.IsNullOrEmpty(keyValue))
- {
- break;
- }
- if (item.Value == "PriKey")
- {
- int num = -1;
- if (int.TryParse(keyValue, out num))
- {
- keyValue = BuildConfig.Instance.prefix + keyValue;
- }
- }
- valuePairs.Add(item.Value, keyValue);
- }
-
- LanguageConf t = ClazzFactory.ClazzPack<LanguageConf>(valuePairs);
- if (t == null)
- {
- UnityLog.Instance.LogError(i + " cant insert!");
- continue;
- }
- table.InsertData(t);
- }
- table.Close();
- }
- else
- {
- Debug.LogError("Error open " + realDataPath);
- }
- }
- catch (Exception ex)
- {
- Debug.LogError(ex.ToString());
- }
-
- AssetDatabase.Refresh();
- }
- public LanguagePackConf AutoAddPack(string path, string key)
- {
- string[] msg = key.Split('/');
- string tableName = typeof(LanguagePackConf).Name;
- var table = XSql.Instance.OpenTable(path, tableName, ".xml", true);
- if (!table.Open())
- {
- table.Create(tableName);
- }
- LanguagePackConf pack = table.FindData<LanguagePackConf>(key);
- if (pack == null)
- {
- pack = new LanguagePackConf();
- pack.Language = msg[0];
- pack.PackagePath = msg[1];
- table.InsertData(pack);
- }
- else
- {
- table.UpdateData(pack);
- }
- table.Save();
- return pack;
- }
- }
- }
|