【Unity-Excel】Excel数据处理(解析生成CSharp类/转Json/运行时读Json存入字典)

0 前言

Excel数据处理是游戏开发中很常见的一个问题,这篇文章实现了:
1.解析Excel生成对应CSharp类
2.Excel数据转Json数据
3.运行时读取Json数据存入字典

Excel为什么要转Json(我自己的理解):
1)方便运行时的数据读取(Json转自定义类型有插件方法可以用,很方便)
2)出于性能考虑

需要插件:
可以在网上找找,应该都有

在这里插入图片描述

1 效果预览

使用

Excel配置

在这里插入图片描述

使用方法
右键Excel文件或者在右键文件夹(转换文件夹下的所有Excel文件)

在这里插入图片描述

结果

CSharp类

在这里插入图片描述
JsonData

在这里插入图片描述
打印

在这里插入图片描述

在这里插入图片描述

2 代码

ExcelConfig.cs

这个脚本主要就是配置一些路径信息。

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class ExcelConfig
{
   
    private static string excelDataPath = "/Excel";
    public static string ExcelDataPath {
   
        get {
   
            var path = Application.dataPath + excelDataPath;
            if (!Directory.Exists(path)) {
   
                Directory.CreateDirectory(path);
            }
            return excelDataPath;
        }
    }

    private static string jsonDataPath = "/Scripts/Excel/JsonData";
    public static string JsonDataPath {
   
        get {
   
            var path = Application.dataPath + jsonDataPath;
            if (!Directory.Exists(path)) {
   
                Directory.CreateDirectory(path);
            }
            return jsonDataPath;
        }
    }

    private static string cSharpPath = "/Scripts/Excel/CSharp";
    public static string CSharpPath {
   
        get {
   
            var path = Application.dataPath + cSharpPath;
            if (!Directory.Exists(path)) {
   
                Directory.CreateDirectory(path);
            }
            return cSharpPath;
        }
    }
}

ExcelToCSharpClass.cs

这个脚本实现了转换。

using Excel;
using Newtonsoft.Json;
using OfficeOpenXml;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

public class ExcelToCSharpClass
{
   
    [MenuItem("Assets/ExcelTool/ExcelToCSharpClass")]
    public static void ExcelToCSharpClassMethod() {
   
        var selectedObject = Selection.activeObject;
        if (selectedObject == null) {
   
            Debug.LogError
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值