在Unity中使用Excel表开发单选题和多选题

前言:

去年还是小菜鸡的我写过在Unity中单选题和多选题的开发。现在进步一点点,这次可以直接编辑表格,在表格中增删改查数据即可,无需再对代码进行更改!
下载链接在文章末尾,需要的可以直接划到最后!

废话不多说,开始~

首先需要配置三个文件

  1. 读取表格的程序集:EPPlus
  2. 处理Json数据的程序集:Newtonsoft.Json
  3. 表格文件:question.xlsx

大概流程如下

创建StreamingAssets文件

  1. 首先我们在工程文件Assets文件下创建一个StreamingAssets(这里我们默认使用此路径为加载路径)在这里插入图片描述

  2. 我们在此文件夹下创建一个表格。这里我使用的是.xlsx在这里插入图片描述
    下面是表结构,大家如果要修改的话,记得同时修改Question.cs哦!
    在这里插入图片描述

创建Plugins文件

  1. 在Assets文件下创建一个名为Plugins文件夹
    在这里插入图片描述

  2. 将刚才上面说了两个程序集EPPlus和Newtonsoft.Json放入此文件下在这里插入图片描述

做完上面那些就可以导入我给大伙准备的脚本了

这里一共有三个脚本:

  • ExcelMgr.cs
  • Question.cs
  • UIQuesPanel.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using OfficeOpenXml;
using System.IO;
using System.Data;
using System;
using Newtonsoft.Json.Linq;
using DialogEntity;

namespace vvb_ExcelMgr
{
   
   
    public class ExcelMgr : MonoBehaviour
    {
   
   
        static ExcelPackage package;//表文件缓存
        public static ExcelWorkbook dialogWorkbook;//表工作簿
        public List<Question> questionList = new List<Question>();//题目缓存列表
        public string quesPackPath="question";//题目文件地址,文件后缀默认为".xlsx"
        private void Awake()
        {
   
   
            ReadExcel(quesPackPath, () =>
            {
   
   
                UIQuesPanel.quesList = GetQuesList(package);
                questionList = UIQuesPanel.quesList;//这里为了在inspecter中能看到读取到达数据
            });
        }
        /// <summary>
        /// 打开表缓存
        /// </summary>
        public static ExcelWorkbook ReadExcel(string excelPath, Action action)
        {
   
   
            //Debug.Log(Application.streamingAssetsPath);
            excelPath = Application.streamingAssetsPath + "/"+excelPath+".xlsx";
            try
            {
   
   
                using (package = new ExcelPackage(new FileStream(excelPath, FileMode.Open)))
                {
   
   
                    action?.Invoke();
                    return package.Workbook;
                }
            }
            catch (NullReferenceException e)
            {
   
   
                Debug.LogError("空指针异常:" + e);
                return null;
            }
            catch (IOException e)
            {
   
   
                Debug.LogError("文件打开异常:" + e);
                return null;
            }
            catch (Exception e)
            {
   
   
                Debug.LogError("其他异常:" + e);
                return null
评论 69
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值