Unity数据持久化 之 一个通过2进制读取Excel并存储的轮子(2) (*****生成数据结构类的方式特别有趣****)

本文仅作笔记学习和分享,不用做任何商业用途

本文包括但不限于unity官方手册,unity唐老狮等教程知识,如有不足还请斧正​​

Unity数据持久化 之 一个通过2进制读取Excel并存储的轮子(1)-CSDN博客

本节内容

实现目标

通过已经得到的Excel表格数据,生成对应类对象(不赋值),一张表就是一个对象,其中包含了如下的字段

就像这样子

实现思路

上节已经获取了一个个单元格,那直接利用其中的字段进行字符串拼接

然后把这个字符串通过文件处理存到一个脚本文件之中就行了,自然就识别成了类对象了

实现过程

生成数据结构类的脚本的路径

cs 复制代码
  //数据结构类脚本存储
  private static string Data_Class_Path = Application.dataPath + "/Scripts/ExcelData/DataClass/";

获得表中的字段名和字段类型

cs 复制代码
   //生成数据结构类的方法
   private static void GenerateExcelDataClass(DataTable dataTable)
   {
       GetVariableNameRow(dataTable);
       GetVariableDataTypeRow(dataTable);


   }
   //获取表中行字段名字的方法
   private static DataRow GetVariableNameRow(DataTable dataTable)
   { 
   return dataTable.Rows[0];   //索引可修改
   }
   //获取表中行字段数据类型的方法
   private static DataRow GetVariableDataTypeRow(DataTable dataTable)
   {
       return dataTable.Rows[1];//索引可修改
   }

确认或创建存储数据结构脚本的路径

cs 复制代码
  DataRow dataRowName= GetVariableNameRow(dataTable);
  DataRow dataRowType= GetVariableDataTypeRow(dataTable);
   //判断存储该脚本的路径是否为空
   if(!Directory.Exists(Data_Class_Path))
       Directory.CreateDirectory(Data_Class_Path);

狠狠拼接字符串

cs 复制代码
  for (int i = 0; i < dataTable.Columns.Count; i++)
  {
      str += "  public" + dataRowType[0].ToString() + " " + dataRowName[i].ToString() + ";" + "\n";
  }

  str += "}";

存入先前的路径中,生成脚本

Windows.File-WriteAllBytes - Unity 脚本 API

cs 复制代码
 ///dataTable.TableName.ToString() =类名,".cs"=后缀名,str=内容
 File.WriteAllText(Data_Class_Path+dataTable.TableName.ToString()+".cs", str);

最终结果

cs 复制代码
using Excel;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using UnityEditor;
using UnityEngine;

public class ExcelTools 
{
    // 表格文件夹的路径
    private static string Excel_Path = Application.dataPath + "/Excel/";

    //数据结构类脚本存储
    private static string Data_Class_Path = Application.dataPath + "/Scripts/ExcelData/DataClass/";

    // 生成和读取表格的方法
    [MenuItem("Tool/GenerateExcel")]
    private static void GenerateExcelInfo()
    {
        // 创建或返回表格文件夹的路径
        DirectoryInfo directoryInfo = Directory.CreateDirectory(Excel_Path);
        // 获取文件夹中的所有文件
        FileInfo[] fileInfo = directoryInfo.GetFiles();
        // 数据表集合
        DataTableCollection dataTableCollection;

        for (int i = 0; i < fileInfo.Length; i++)
        {
            // 筛选出扩展名为 .xlsx 或 .xls 的文件
            if (fileInfo[i].Extension != ".xlsx" && fileInfo[i].Extension != ".xls")
                continue;

            // 打开文件流读取表格
            using (FileStream fs = fileInfo[i].Open(FileMode.Open, FileAccess.Read))
            {
                // 使用 IExcelDataReader 读取表格数据
                IExcelDataReader excelDataReader = ExcelReaderFactory.CreateOpenXmlReader(fs);
                dataTableCollection = excelDataReader.AsDataSet().Tables; // 转换为数据表集合
                fs.Close();
            }

            // 输出每个工作表的名称
            foreach (DataTable temp in dataTableCollection)
            {
                Debug.Log(temp.TableName);

                //生成数据结构类
                GenerateExcelDataClass(temp);
            }
           

        }
    }

    //生成数据结构类的方法
    private static void GenerateExcelDataClass(DataTable dataTable)
    {
       DataRow dataRowName= GetVariableNameRow(dataTable);
       DataRow dataRowType= GetVariableDataTypeRow(dataTable);
        //判断存储该脚本的路径是否为空
        if(!Directory.Exists(Data_Class_Path))
            Directory.CreateDirectory(Data_Class_Path);

        string str = "public class " + dataTable.TableName.ToString()+"\n{\n";

        for (int i = 0; i < dataTable.Columns.Count; i++)
        {
            str += "  public " + dataRowType[0].ToString() + " " + dataRowName[i].ToString() + ";" + "\n";
        }

        str += "}";

        ///dataTable.TableName.ToString() =类名,".cs"=后缀名,str=内容
        File.WriteAllText(Data_Class_Path+dataTable.TableName.ToString()+".cs", str);

    }
    //获取表中行字段名字的方法
    private static DataRow GetVariableNameRow(DataTable dataTable)
    { 
    return dataTable.Rows[0];   //索引可修改
    }
    //获取表中行字段数据类型的方法
    private static DataRow GetVariableDataTypeRow(DataTable dataTable)
    {
        return dataTable.Rows[1];//索引可修改
    }
}
相关推荐
我要吐泡泡了哦1 小时前
GAMES104:15 游戏引擎的玩法系统基础-学习笔记
笔记·学习·游戏引擎
__water8 小时前
『功能项目』回调函数处理死亡【54】
c#·回调函数·unity引擎
__water8 小时前
『功能项目』眩晕图标显示【52】
c#·unity引擎·动画事件
__water9 小时前
『功能项目』第二职业法师的平A【57】
c#·unity引擎·魔法球伤害传递
躺下睡觉~10 小时前
Unity-Transform类-父子关系
java·unity·游戏引擎
躺下睡觉~10 小时前
Unity-Transform类-缩放和看向
unity·游戏引擎
__water11 小时前
『功能项目』战士的伤害型技能【45】
c#·unity引擎·战士职业伤害型技能
君莫愁。12 小时前
【Unity】检测鼠标点击位置是否有2D对象
unity·c#·游戏引擎
Lingbug13 小时前
.Net日志组件之NLog的使用和配置
后端·c#·.net·.netcore
咩咩觉主13 小时前
Unity实战案例全解析:PVZ 植物卡片状态分析
unity·c#·游戏引擎