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

news/2024/7/21 5:28:00 标签: c#, unity, unity3d, excel, 游戏开发

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


http://www.niftyadmin.cn/n/1241782.html

相关文章

Day314.外观模式享元模式 -Java设计模式

外观模式 外观类就是总控开关&#xff0c;去管理聚合的所有类 一、影院管理项目 组建一个家庭影院&#xff1a; DVD 播放器、投影仪、自动屏幕、环绕立体声、爆米花机,要求完成使用家庭影院的功能&#xff0c;其过程为&#xff1a; 直接用遥控器&#xff1a;统筹各设备开关…

Qt例子学习笔记 - Examples/Qt-6.2.0/assistant/simpletextviewer

main.cpp #include "mainwindow.h"#include <QApplication> //QApplication 专门为 QGuiApplication 提供了一些基于 QWidget 的应用程序所需的功能。 // 它处理特定于小部件的初始化、完成。 //对于任何使用 Qt 的 GUI 应用程序&#xff0c;无论应用程序在任…

Lua 知识点整理

Lua 知识点整理 经过几个月的学习和使用&#xff0c;对 Lua 有了更深的理解&#xff0c;以下整理参考自 Programming in Lua 这本书&#xff0c;挑出来一些常用的知识点以及重写了部分示例代码&#xff0c;最后有一些不错的文章推荐。 文章目录第一篇 语法第三章 表达式3.6 ta…

Day315.代理模式 -Java设计模式

代理模式 一、 代理模式(Proxy) 1、代理模式的基本介绍 代理模式&#xff1a;为一个对象提供一个替身&#xff0c;以控制对这个对象的访问。即通过代理对象访问目标对象.这样做的好处是: 可以在目标对象实现的基础上,增强额外的功能操作,即扩展目标对象的功能。 被代理的对象…

Day316.模版模式命令模式 -Java

模板模式 一、豆浆制作问题 编写制作豆浆的程序&#xff0c;说明如下: 1)制作豆浆的流程 选材—>添加配料—>浸泡—>放到豆浆机打碎 2)通过添加不同的配料&#xff0c;可以制作出不同口味的豆浆 3)选材、浸泡和放到豆浆机打碎这几个步骤对于制作每种口味的豆浆都是一…

企业级的RocketMQ集群如何进行权限机制的控制?

企业级的RocketMQ集群如何进行权限机制的控制&#xff1f; 一、前言 RocketMQ的权限控制的功能&#xff0c;简单来说&#xff0c;如果一个公司有很多技术团队&#xff0c;每个技术团队都会使用RocketMQ集群中的部分Topic&#xff0c;那么此时可能就会有一个问题了&#xff0c…

RocketMQ集群如何打开消息轨迹的追踪?

RocketMQ集群如何打开消息轨迹的追踪&#xff1f; 对于一个消息&#xff0c;我想要知道&#xff0c;这个消息是什么时候从哪个Producer发送出来的&#xff1f;他在Broker端是进入到了哪个Topic里去的&#xff1f;他在消费者层面是被哪个Consumer什么时候消费出来的&#xff1f…

Day317.访问者模式 -Java

访问者模式 这个模式用的很少&#xff0c;《设计模式》的作者评价为&#xff1a; 大多情况下&#xff0c;你不需要使用访问者模式&#xff0c;但是一旦需要使用它时&#xff0c;那就真的需要使用了 一、测评系统的需求 1)将观众分为男人和女人&#xff0c;对歌手进行测评&…