当前位置: 首页 > news >正文

Unity Epplus读取excel表并存入So文件举例

目录

        此篇需要你有一定的阅读代码的能力,不然点开了也不知道在做什么 

这是读表工具 

So文件这么写

 使用


        此篇需要你有一定的阅读代码的能力,不然点开了也不知道在做什么 

        在此之前你需要知道epplus是干什么的,然后知道其基本api,其实我之前写过一篇关于这个插件的说明

        【插件】【干货】用EPPlus在Unity中读写Excel表_epplus unity-CSDN博客

读表工具 

                 十分简单,然后又写了一个存入so文件类,涉及到一个所谓脏标记的api,箭头读取该excel表必不可少的重要的信息

using System.Collections.Generic;
using System.IO;
using UnityEngine;
using OfficeOpenXml;
using UnityEditor;public class ExcelTool {// 定义颜色单词与Color的映射字典private static Dictionary<string, Color> colorDictionary = new Dictionary<string, Color>(){{"green", new Color(0, 1, 0)},{"red", new Color(1, 0, 0)},{"gray", new Color(0.5f, 0.5f, 0.5f)},{"yellow", new Color(1, 1, 0)},{"clear", new Color(0.5f, 0.5f, 0.5f, 0.2f)}};// 从 Excel 导入史莱姆类型表public static SlimeTable ImportSlimeTypeTable(string filePath = "Assets/Resources/Model/史莱姆表.xlsx") {SlimeTable slimeTypeTable = ScriptableObject.CreateInstance<SlimeTable>();using (ExcelPackage excelPackage = new ExcelPackage(new FileInfo(filePath))) {ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];int rowCount = worksheet.Dimension.Rows;for (int i = 2; i <= rowCount; i++) {SlimeTypeData data = new SlimeTypeData();int.TryParse(worksheet.Cells[i, 1].Value?.ToString(), out data.ID);data.ID = i;if (System.Enum.TryParse(worksheet.Cells[i, 2].Value?.ToString(), out E_SlimeType slimeType)) {data.E_SlimeTypeData = slimeType;}if (System.Enum.TryParse(worksheet.Cells[i, 3].Value?.ToString(), out E_Recip recip)) {data.E_RecipData = recip;}string colorString = worksheet.Cells[i, 4].Value?.ToString();if (!string.IsNullOrEmpty(colorString)) {colorString = colorString.ToLower(); // 转换为小写,确保与字典中的键匹配if (colorDictionary.TryGetValue(colorString, out Color color)) {data.colorData = color;}else {Debug.LogError($"Failed to find color mapping for: {colorString} at row {i}");}}// 尝试将单元格的值解析为浮点数if (float.TryParse(worksheet.Cells[i, 5].Value?.ToString(), out float huangeyTime)) {data.huangeyTime = huangeyTime;}else {Debug.LogError($"Failed to parse huangeyTime at row {i}");}slimeTypeTable.dataList.Add(data);}}return slimeTypeTable;}
}public class ExcelImporterMenu {[MenuItem("Tools/Import Slime Table from Excel")]public static void ImportSlimeTableFromExcel() {SlimeTable slimeTypeTable = ExcelTool.ImportSlimeTypeTable();if (slimeTypeTable != null && slimeTypeTable.dataList.Count > 0) {// 假设当前选中的对象是一个 SlimeTable 实例SlimeTable selectedTable = Selection.activeObject as SlimeTable;if (selectedTable != null) {selectedTable.dataList = slimeTypeTable.dataList;EditorUtility.SetDirty(selectedTable);AssetDatabase.SaveAssets();Debug.Log("史莱姆表已成功从 Excel 导入");}else {Debug.LogWarning("请先在项目窗口中选中一个 SlimeTable 实例");}}else {Debug.LogError("导入史莱姆表时出现问题,请检查文件路径和文件内容");}}
}

So文件

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;// 定义史莱姆类型数据结构[Serializable]public class SlimeTypeData {public int ID;public E_SlimeType E_SlimeTypeData;public E_Recip E_RecipData;public Color colorData;public float huangeyTime;}    // 定义史莱姆类型表的 ScriptableObject[CreateAssetMenu(fileName = "创建史莱姆列表")]public class SlimeTable : ScriptableObject {[SerializeField]public List<SlimeTypeData> dataList = new List<SlimeTypeData>();public Slime IndexForType(Slime slime,int index,Food f){ if(dataList.Count!=0) {SlimeTypeData data = dataList[index];slime.gameObject.name = data.E_SlimeTypeData.ToString();f.foodType = data.E_RecipData;slime.E_SlimeType = data.E_SlimeTypeData;slime.SpriteRd.color = data.colorData;slime.MaxHuangryValue = data.huangeyTime;slime.CurHuangryTimer = slime.MaxHuangryValue;}return slime;}
}

 使用

选择你创建出来的SO文件,然后点击你的菜单栏拓展 如果格式正确就能直接读取出来了

 

 

 

 

http://www.lryc.cn/news/527013.html

相关文章:

  • 连接 OpenAI 模型:基础操作
  • [ Spring ] Spring Cloud Alibaba Message Stream Binder for RocketMQ 2025
  • ubuntu 更新24LTS中断导致“系统出错且无法恢复,请联系系统管理员”
  • 力扣-链表-203 移除链表元素
  • Unity中关于实现 管道水流+瀑布流动+大肠蠕动效果笔记
  • 宏_wps_宏修改word中所有excel表格的格式_设置字体对齐格式_删除空行等
  • Linux——网络(udp)
  • Oracle-Java JDBC 连接超时之后的认知纠正
  • 自定义数据集使用框架的线性回归方法对其进行拟合
  • 15天基础内容-5
  • 82,【6】BUUCTF WEB .[CISCN2019 华东南赛区]Double Secret
  • Android WebView 中网页被劫持的原因及解决方案
  • 特朗普政府将开展新网络攻击
  • 快递代取项目Uniapp+若依后端管理
  • arcgis短整型变为长整型的处理方式
  • 06、Redis相关概念:缓存击穿、雪崩、穿透、预热、降级、一致性等
  • 嵌入式基础 -- PCIe 控制器中断管理之MSI与MSI-X简介
  • websocket实现
  • unity学习20:time相关基础 Time.time 和 Time.deltaTime
  • 【C++】特殊类设计、单例模式与类型转换
  • scratch七彩六边形 2024年12月scratch三级真题 中国电子学会 图形化编程 scratch三级真题和答案解析
  • 代码随想录刷题day16|(哈希表篇)349.两个数组的交集
  • Synology 群辉NAS安装(6)安装mssql
  • 2025年美赛B题-结合Logistic阻滞增长模型和SIR传染病模型研究旅游可持续性-成品论文
  • Hook 函数
  • 蓝桥杯模拟算法:蛇形方阵
  • DeepSeek-R1解读:纯强化学习,模型推理能力提升的新范式?
  • 深度解析:基于Vue 3的教育管理系统架构设计与优化实践
  • 【PyTorch】3.张量类型转换
  • Spring Boot整合JavaMail实现邮件发送