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 }");
//GUIUtility.systemCopyBuffer = sb.ToString();
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檔案如下所示,沒有進行排版所有有點亂,但是無傷大雅啦,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/292591.html
標籤:其他
