Unity中根据Excel配表产生C#配置类

3 篇文章 0 订阅

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文件如下所示,没有进行排版所有有点乱,但是无伤大雅啦。
在这里插入图片描述

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值