155 lines
4.2 KiB
C#
155 lines
4.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Xml;
|
|
using UnityEditor;
|
|
|
|
namespace Thousandto.DIY
|
|
{
|
|
/// <summary>
|
|
/// 读取LanguageConverterExcel.xlsx
|
|
/// </summary>
|
|
public class LanguageConverterExcel : XmlReadExcel
|
|
{
|
|
/// <summary>
|
|
/// 插入到LanguageConverter表后生成的id
|
|
/// </summary>
|
|
public int ID { get; set; }
|
|
|
|
public const string ExcelPath = "../../Gamedata/Locate/LanguageConverter.xlsx";
|
|
|
|
private Dictionary<string, string[]> _dataDict = new Dictionary<string, string[]>();
|
|
|
|
private static LanguageConverterExcel _instance;
|
|
private static LanguageConverterExcel Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
_instance = new LanguageConverterExcel();
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
public static void Initialize()
|
|
{
|
|
_instance = null;
|
|
Instance.Parse();
|
|
}
|
|
|
|
public static void SaveAsExcel()
|
|
{
|
|
Instance.Save();
|
|
}
|
|
|
|
public override void Parse()
|
|
{
|
|
base.Parse(ExcelPath);
|
|
_dataDict.Clear();
|
|
try
|
|
{
|
|
//ReadData();
|
|
ConvertData();
|
|
}
|
|
catch(System.Exception ex)
|
|
{
|
|
string msg = ex.Message + "\r\n" + ex.StackTrace;
|
|
UnityEngine.Debug.LogError(msg);
|
|
EditorUtility.DisplayDialog("Alert", msg, "ok");
|
|
}
|
|
}
|
|
|
|
public override void Save()
|
|
{
|
|
base.Save(ExcelPath);
|
|
}
|
|
|
|
public static string GetLanguage(string chKey, int lanId)
|
|
{
|
|
if(Instance._dataDict.ContainsKey(chKey))
|
|
{
|
|
var valueArray = Instance._dataDict[chKey];
|
|
if(lanId >= valueArray.Length)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return valueArray[lanId];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static Dictionary<string, string[]> GetAllLanguage()
|
|
{
|
|
return Instance._dataDict;
|
|
}
|
|
|
|
public static Dictionary<string, string> GetAllLangauge(int lan)
|
|
{
|
|
Dictionary<string, string> retDict = new Dictionary<string, string>();
|
|
|
|
var itor = Instance._dataDict.Keys.GetEnumerator();
|
|
while(itor.MoveNext())
|
|
{
|
|
retDict.Add(itor.Current, Instance._dataDict[itor.Current][lan]);
|
|
}
|
|
|
|
return retDict;
|
|
}
|
|
|
|
public static void AddValue(string chStr, string valueStr, int lan, int lanCount)
|
|
{
|
|
//添加到Dictionary
|
|
string[] valueArray = null;
|
|
if(!Instance._dataDict.ContainsKey(chStr))
|
|
{
|
|
valueArray = new string[lanCount];
|
|
Instance._dataDict.Add(chStr, valueArray);
|
|
}
|
|
|
|
valueArray[lan] = valueStr;
|
|
|
|
//添加到xml
|
|
string[] dataArray = new string[lanCount];
|
|
dataArray[lan] = valueStr;
|
|
Instance._sheetData.AddRow(dataArray);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将行列数据转换为字典结构
|
|
/// </summary>
|
|
private void ConvertData()
|
|
{
|
|
var allRowData = _sheetData.GetAllData();
|
|
for (int i = 0; i < allRowData.Count; ++i)
|
|
{
|
|
var collums = allRowData[i].GetCollumDatas();
|
|
if (collums == null || collums.Count == 0 || collums[0] == null || collums[0].GetCollumNum() > 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
string key = collums[0].ShareValue;
|
|
|
|
if(_dataDict.ContainsKey(key))
|
|
{
|
|
//重复的key
|
|
UnityEngine.Debug.LogWarning("重复的Key: " + key);
|
|
continue;
|
|
}
|
|
|
|
string[] valueArray = allRowData[i].ToArray();
|
|
|
|
_dataDict.Add(key, valueArray);
|
|
}
|
|
}
|
|
|
|
[MenuItem("Test/excel")]
|
|
static void Test()
|
|
{
|
|
Initialize();
|
|
}
|
|
}
|
|
}
|