欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > Unity Epplus读取excel表并存入So文件举例

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

2025/4/19 10:10:02 来源:https://blog.csdn.net/2301_77947509/article/details/145369216  浏览:    关键词: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文件,然后点击你的菜单栏拓展 如果格式正确就能直接读取出来了

 

 

 

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词