Unity Excel导出CSV配置文件(一)

该博客介绍了使用Python实现Unity批量Excel转CSV的功能,包括指定导出Sheet、开始行,区分服务器与客户端等。还详细说明了使用方法,如设置导出Excel表、填写数据、Unity导出方法、批量导出及设置开始行等,最后给出完整工程链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


【配置表】Unity表管理_读取CSV文件(二)

功能

1、批量Excel转CSV
2、指定导出Sheet(注释|导出文件名)
3、可指定导出开始行(默认3)
4、导出区分服务器与客户端

注意:导表使用python写的,数值类型默认导出为浮点型,处理需注意。【配置表】Unity表管理_读取CSV文件(二)

  • 目前支持数据 : 表中写法
    int
    short
    byte
    float
    double
    bool
    string
    enum : 枚举字符
    Vector3 : 3,3,3
    Vector2 : 2,2
    int[] : , 分割
    float[] :,分割
    Vector3[] : ,分割Vector3数据 |分割元素(例:0.3,0.3,0.3|3,3,3)
    Vector2[] : ,分割Vector3数据 |分割元素(例:0.2,0.2|2,2)
    Dictionary<int, int> : ,分割键值对 |分割元素(例:1,-1|2,-2)
    Dictionary<int, float> : ,分割键值对 |分割元素(例:1,0.1|2,0.2)

使用方法

1、设置导出Excel表

Excel设置导出Sheet格式为:注释|导出文件名。
导出Sheet
将Excel放到“Tools\Config\Excel”文件夹中。

2、填写导出数据

  • 由于在 python 中设置的导出行为:3 所以只会导出第三行以下数据(包括第三行)
    导出csv填写方式
  • 当前第三行为“字段名”行,必须与C#数据结构中的字段名所对应(如果未对应则使用C#字段顺序对应表中数据顺序)。
  • “字段名”行之下必须是数据。(“字段名”行可以在python中指定,“字段名”行之上属于注释)。
  • 数据填写方式上面有。

3、Unity导出方法

设置导出位置在: EditorToolsExtension.cs

private static void ExcelToCSV() {

    //导出位置
    string new_path = Application.dataPath + "/Resources/csv";
    string src_path = Environment.CurrentDirectory + "/Tools/Config/OutCSV";

    EditorUtility.DisplayProgressBar("Excel", "导出 Excel", 0.5f);
    string logInfo = ProcessUtils.ProcessCommand(Environment.CurrentDirectory + "/Tools/ExcelToCSV/ExportCSV.bat");

    if (logInfo.Contains("all operation finish successful")) {
        Debug.Log(logInfo);
        DelectDir(new_path);
        CopyDir(src_path, new_path);
        EditorUtility.ClearProgressBar();
        EditorUtility.DisplayDialog("success", "excel 导出成功", "ok");
        AssetDatabase.Refresh();
    }
    EditorUtility.ClearProgressBar();
}

Unity 中使用 Ctrl+E 导出。

4、批量导出

运行 ExportCSV.bat 批量导出。(Excel位置:Tools\Config\Excel 导出位置: Tools\Config\OutCSV)

5、设置导出开始行

设置导出开始行位置 excel_to_csv.py -> START_ROW (文件位置:Tools\ExcelToCSV)

Python代码

import xlrd
import csv
import os

#Tools path
root_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
#Config path
config_path = os.path.join(root_path,"Config")
#Excel path
excel_path = os.path.join(config_path,"Excel")
#OutCSV path
outCSV_path = os.path.join(config_path,"OutCSV")

#识别后缀
EXPORT_EXT = [
    ".xlsx",
    ".xls",
]

#Table 开始行
START_ROW = 3

def make_dir():
    if not os.path.isdir(config_path):
        os.mkdir(config_path)
    if not os.path.isdir(config_path):
        os.mkdir(config_path)
    if not os.path.isdir(excel_path):
        os.mkdir(excel_path)
    if not os.path.isdir(outCSV_path):
        os.mkdir(outCSV_path)
    file_list = os.listdir(outCSV_path)
    for _file in file_list:
        os.remove(os.path.join(outCSV_path,_file))

def get_excel_list():

    #获取Excel文件列表
    excel_file_list = []
    file_list = os.listdir(excel_path)
    
    for file_name in file_list:
        if os.path.splitext(file_name)[1] in EXPORT_EXT and not file_name.startswith("~"):
            excel_file_list.append(file_name)

    return excel_file_list


def read_excel(excel_name):

    #读取Excel文件中的页
    workbook = xlrd.open_workbook(os.path.join(excel_path,excel_name))

    for sheet in workbook.sheets():
        s_index = sheet.name.find('|')
        if s_index == -1: continue
        csv_name = sheet.name[s_index + 1 :]
        table_to_csv(csv_name,sheet)
        print("export "+csv_name+" successful!")

def table_to_csv(csv_file_name,table):

    #生成csv文件
    with open(os.path.join(outCSV_path,csv_file_name)+".csv", 'w', encoding='utf-8',newline='') as f
        write = csv.writer(f)
        #从第n行数据开始读
        for rows_read in range(START_ROW - 1,table.nrows):
            write.writerow(table.row_values(rows_read))

if __name__ == '__main__':

    make_dir()
    excel_list = get_excel_list()

    for file_name in excel_list:
        read_excel(file_name)

    print("all operation finish successful")

    exit(0)

完整工程

MacOS使用C#调用Python报错ModuleNotFoundError
gitee:https://gitee.com/horooo/UnityExcelToCSV

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值