一、目的
将表中的题目进行转换使用,最终还要发布成为webgl版本
二、使用步骤
1.引入库
添加Plugins文件:(PS:我忘记添加的是哪些了,貌似是Excel.dll,ICSharpCode.SharpZipLib.dll,System.Data.dll这些库)
链接:https://pan.baidu.com/s/1urZeVi5DUKpcKX_qduk6gQ
提取码:7wl4
2.转换数据
将excel表格转换为object
代码如下:
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
/// <summary>
/// 利用ScriptableObject创建资源文件
/// </summary>
public class BuildAsset : Editor
{
[MenuItem("BuildAsset/Build Scriptable Asset")]
public static void ExcuteBuild()
{
BookHolder holder = ScriptableObject.CreateInstance<BookHolder>();
//查询excel表中数据,赋值给asset文件
holder.menus = ExcelAccess.SelectMenuTable();
string path = "Assets/Resources/KaoTi.asset";
AssetDatabase.CreateAsset(holder, path);
AssetDatabase.Refresh();
Debug.Log("BuildAsset Success!");
}
}
#endif
2.读入数据
数据进行转换以后,可以使用代码直接进行调取。
using UnityEngine;
using System.Collections.Generic;
/// <summary>
/// 基于ScriptObject的BookHolder类
/// </summary>
public class BookHolder : ScriptableObject
{
public List<Menu> menus;
}
/// <summary>
/// 菜单实体类
/// </summary>
[System.Serializable]
public class Menu
{
public string m_Id;
public string m_Projects;
public string m_Mission;
public string m_Types;
public string m_TitleNum;
public string m_Questions;
public string m_Options;
public string m_Answer;
}
读取数据:
using UnityEngine;
using Excel;
using System.Data;
using System.IO;
using System.Collections.Generic;
public class ExcelAccess
{
public static string Excel = "KaoTi";
//查询menu表
public static List<Menu> SelectMenuTable()
{
string excelName = Excel + ".xlsx";
string sheetName = "sheet1";
DataRowCollection collect = ExcelAccess.ReadExcel(excelName, sheetName);
List<Menu> menuArray = new List<Menu>();
for (int i = 1; i < collect.Count; i++)
{
if (collect[i][1].ToString() == "") continue;
Menu menu = new Menu
{
m_Id = collect[i][0].ToString(),
m_Projects = collect[i][1].ToString(),
m_Mission = collect[i][2].ToString(),
m_Types = collect[i][3].ToString(),
m_TitleNum = collect[i][4].ToString(),
m_Questions = collect[i][5].ToString(),
m_Options = collect[i][6].ToString(),
m_Answer = collect[i][7].ToString()
};
menuArray.Add(menu);
}
return menuArray;
}
/// <summary>
/// 读取 Excel ; 需要添加 Excel.dll; System.Data.dll;
/// </summary>
/// <param name="excelName">excel文件名</param>
/// <param name="sheetName">sheet名称</param>
/// <returns>DataRow的集合</returns>
static DataRowCollection ReadExcel(string excelName, string sheetName)
{
string path = Application.dataPath + "/" + excelName;
FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
DataSet result = excelReader.AsDataSet();
//int columns = result.Tables[0].Columns.Count;
//int rows = result.Tables[0].Rows.Count;
//tables可以按照sheet名获取,也可以按照sheet索引获取
//return result.Tables[0].Rows;
return result.Tables[sheetName].Rows;
}
}
调取数据:(此代码挂在项目场景中)
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 读取KaoTi的scriptObject文件
/// 使用Resources直接读取
/// </summary>
public class ReadHolders : MonoBehaviour
{
readonly string assetName = "KaoTi";
void Awake()
{
if (DanLi.GetDL.ReadHolders == null)
{
DanLi.GetDL.ReadHolders = this;
}
}
void Start()
{
BookHolder asset = Resources.Load<BookHolder>(assetName);
foreach (Menu gd in asset.menus)
{
Debug.Log(gd.m_Id + " " + gd.m_Projects + " " + gd.m_Mission + " " + gd.m_Types + " " + gd.m_TitleNum + " " + gd.m_Questions + " " + gd.m_Options + " " + gd.m_Answer + "\n");
}
}
}