IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Unity中根据Excel配表产生C#配置类 -> 正文阅读

[游戏开发]Unity中根据Excel配表产生C#配置类

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

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-08-08 11:53:00  更:2021-08-08 11:53:04 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 21:15:06-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码