Unity中根据Excel配表产生C#配置类
在实习的时候需要开发一个工具类,具体要求是:
- 策划在配表中填写 elementId , outputFunction 和 functionParam,具体格式如下图所示(elementId在第一列所以略去);
- 需求是在C#中读取xlxs文件,然后替换里面的常数(A - > 1000, B -> 100),保留非常数部分(如source.attack)
- 然后在Unity中通过菜单栏,生成一个cs文件,里面有一个静态方法,输入elementId,source和target,获取对应的输出。

Unity3D 读取 Excel 参考下面这篇文章 https://blog.51cto.com/myselfdream/2494149?source=dra 由于读入Excel文件的格式都是string,我们需要将sting类型变量中的A替换成常数,将source.attack等非常数信息进行保留,最后将生成的字符串输出到一个cs文件中,完成配置,代码如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using FlexFramework.Excel;
using UnityEditor;
using UnityEngine;
public class GameManager : MonoBehaviour
{
#if UNITY_EDITOR
[MenuItem("Tools/由xlxs产生配置文件")]
#endif
private static void CopyText()
{
StringBuilder sb = new StringBuilder();
sb.Append("namespace Gameplay.PVE.Config { public class PveOutputFunction{");
sb.Append("public static float GetOutput(UnitData source, UnitData target, int elementId){");
sb.Append("\nswitch(elementId){");
List<string> functions = new List<string>();
List<List<string>> res = LoadGuideData("D:\\Config\\trunk\\3xlsx\\4BattleUnit\\rpg_element.xlsx");
foreach (var row in res)
{
Dictionary<string, string> tempDict = new Dictionary<string, string>();
if (row.Count == 0 || row.Count == 1)
{
continue;
}
if (row.Count == 2)
{
functions.Add(Parse(row[0], row[1], tempDict));
}
if (row.Count == 3)
{
string[] tempList = row[2].Split(new char[] {';', '='});
for (int i = 0; i < tempList.Length - 1; i = i + 2)
{
tempDict.Add(tempList[i], tempList[i + 1]);
}
functions.Add(Parse(row[0], row[1], tempDict));
}
functions.Add("\n");
}
foreach (var function in functions)
{
sb.Append(function);
}
sb.Append("default: return 0;");
sb.Append("} \n } \n } \n }");
CreateFile(Application.dataPath, "pveConfig.cs", sb.ToString());
}
public static string Parse(string elementID, string outputFunction, Dictionary<string, string> functionPara)
{
char[] separator = {'+', '-', '*', '/', '%', '(', ')'};
string[] paraList = outputFunction.Split(separator, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < paraList.Length; i++)
{
if (functionPara.ContainsKey(paraList[i]))
{
outputFunction = outputFunction.Replace(paraList[i], functionPara[paraList[i]]);
}
}
outputFunction = "case " + elementID + ":\n" + "\treturn " + outputFunction + ";";
return outputFunction;
}
private static List<List<string>> LoadGuideData(string path)
{
var book = new WorkBook(File.ReadAllBytes(path));
List<Row> rowData = new List<Row>(book[0]);
List<List<string>> result = new List<List<string>>();
for (int j = 5; j < rowData .Count; j++)
{
List<string> temp = new List<string>();
string str = rowData[j][0].Text;
temp.Add(str);
for (int i = 9; i < rowData[j].Count; i++)
{
str = rowData[j][i].Text;
temp.Add(str);
}
result.Add(temp);
}
return result;
}
public static void CreateFile(string path, string name, string info)
{
FileInfo t = new FileInfo(path + "//" + name);
StreamWriter sw = t.CreateText();
sw.WriteLine(info);
sw.Close();
sw.Dispose();
}
}
最后产生的cs文件如下所示,没有进行排版所有有点乱,但是无伤大雅啦。 
|