效果图:
代码(使用了插件EPPlus)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using OfficeOpenXml;
using System.IO;
using UnityEngine.Networking;
using UnityEngine.UI;
using Random = UnityEngine.Random;
public class EXCELReader : MonoBehaviour
{
public static List<QuesionClass> QuesionsList=new List<QuesionClass>();
public class QuesionClass
{
public string question, image, answer, analysis,BIJI;
}
public GameObject ButtonPrefab,ButtonParent;
public InputField BIJI;
public static string ReadingExcel;//正在读取的表格
#region 按钮初始化
public Button Full,Save,Delete,Answer,Next;
public GameObject InputFiledOBJ,ShowAnswer,ShowTip;
private bool IsFull;
private List<int> SavingQuestions = new List<int>();//存储看过的题目
private void ButtonUpdate()
{
Full.onClick.AddListener(() =>
{
InputFiledOBJ.transform.position =
IsFull ? new Vector3(683.0f, -126.0f, 0.0f) : new Vector3(683.0f, 284.0f, 0.0f);
IsFull = !IsFull;
});
Save.onClick.AddListener(() =>
{
FileInfo fileInfo = new FileInfo(Application.streamingAssetsPath+"\\Questions\\题库我的收藏.xlsx");
Debug.Log(fileInfo);
using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
{
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
int Row = worksheet.Dimension.End.Row+1;
worksheet.Cells[Row, 1].Value =
EXCELReader.QuesionsList[EXCELReader.RandomNum].question;
worksheet.Cells[Row, 2].Value =
EXCELReader.QuesionsList[EXCELReader.RandomNum].image;
worksheet.Cells[Row, 3].Value =
EXCELReader.QuesionsList[EXCELReader.RandomNum].answer;
worksheet.Cells[Row, 4].Value =
EXCELReader.QuesionsList[EXCELReader.RandomNum].analysis;
worksheet.Cells[Row, 5].Value =
InputFiledOBJ.GetComponent<InputField>().text;
excelPackage.Save();
//取得第一行第一列的数据
}
});
Delete.onClick.AddListener(() =>
{
FileInfo fileInfo = new FileInfo(Application.streamingAssetsPath+"\\Questions\\"+EXCELReader.ReadingExcel);
Debug.Log(fileInfo);
using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
{
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
if ( worksheet.Dimension.End.Row>1)
{
Debug.Log( worksheet.Cells[RandomNum+2, 5].Value.ToString());
worksheet.DeleteRow(EXCELReader.RandomNum+2);
excelPackage.Save();
}
}
MakeQuestions(EXCELReader.ReadingExcel);
UpdateBoard();
});
Answer.onClick.AddListener(() =>
{
ShowAnswer.SetActive(true);
ShowTip.SetActive(true);
});
Next.onClick.AddListener(UpdateBoard);
}
#endregion
void Start()
{
MakeButtons();
ButtonUpdate();
}
/// <summary>
/// 根据读取到的表格制作Button
/// </summary>
void MakeButtons()
{
foreach (var VARIABLE in ReadFile())
{
if (VARIABLE.Contains("题库"))
{
GameObject go = Instantiate(ButtonPrefab, ButtonParent.transform);
go.name = VARIABLE.Substring(VARIABLE.LastIndexOf("题库"),VARIABLE.Length-VARIABLE.LastIndexOf("题库"));
string g= go.name.Replace("题库", "");
g = g.Replace(".xlsx", "");
go.transform.Find("Text").GetComponent<Text>().text =g;
go.GetComponent<Button>().onClick.AddListener(() =>
{
MakeQuestions(go.name);
UpdateBoard();
ReadingExcel = go.name;
});
}
}
}
/// <summary>
/// 创建一个题库
/// </summary>
void MakeQuestions(string URL)
{
QuesionsList.Clear();
SavingQuestions.Clear();
//获取Excel文件的信息
foreach (var VARIABLE in ReadFile())
{
if (VARIABLE.Contains(URL))
{
FileInfo fileInfo = new FileInfo(VARIABLE);
//通过Excel表格的文件信息,打开Excel表格
//使用using(){}语句命令,在结束时自动关闭文件
using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
{
//读取Excel中的第一张表, 注意EPPlus的索引值是从1开始的
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
//取得第一行第一列的数据
for (int Left = 2; Left < worksheet.Dimension.End.Row+1; Left++)//根据行数遍历
{
QuesionClass Q = new QuesionClass();
Q.question = worksheet.Cells[Left, 1].Value.ToString();
Q.image= worksheet.Cells[Left, 2].Value.ToString();
Q.answer= worksheet.Cells[Left, 3].Value.ToString();
Q.analysis= worksheet.Cells[Left, 4].Value.ToString();
try//笔记可能为空
{
Q.BIJI= worksheet.Cells[Left, 5].Value.ToString();
}
catch (NullReferenceException e)
{
Q.BIJI = "";
}
QuesionsList.Add(Q);
Debug.Log(QuesionsList[0].answer);
}
}
}
}
}
/// <summary>
/// 获取文件的方法
/// </summary>
List<string> ReadFile()
{
List<string> files = GetFiles(Application.streamingAssetsPath+"\\Questions", "*.xlsx");
List<string> GetFiles(string directory, string pattern)
{
List<string> files = new List<string>();
foreach (var item in Directory.GetFiles(directory, pattern))
{
files.Add(item);
}
foreach (var item in Directory.GetDirectories(directory))
{
files.AddRange(GetFiles(item, pattern));
}
return files;
}
return files;
}
public GameObject BoardPrefab;
int savingRD=0;
public static int RandomNum =0;
/// <summary>
/// 更新展示板
/// </summary>
void UpdateBoard()
{
ShowAnswer.SetActive(false);
ShowTip.SetActive(false);
if (QuesionsList.Count==0)
{
Debug.LogError("题目为空");
return;
}
RandomNum = Random.Range(0, QuesionsList.Count);
BoardPrefab.transform.Find("题目").GetComponent<Text>().font = HelperClass.Fonts[HelperClass.ChoseingFont];
BoardPrefab.transform.Find("答案").GetComponent<Text>().font = HelperClass.Fonts[HelperClass.ChoseingFont];
BoardPrefab.transform.Find("解析").GetComponent<Text>().font = HelperClass.Fonts[HelperClass.ChoseingFont];
BoardPrefab.transform.Find("题目").GetComponent<Text>().text = QuesionsList[RandomNum].question;
BoardPrefab.transform.Find("答案").GetComponent<Text>().text = QuesionsList[RandomNum].answer;
BoardPrefab.transform.Find("解析").GetComponent<Text>().text = QuesionsList[RandomNum].analysis;
BIJI.text = QuesionsList[RandomNum].BIJI;
if ( QuesionsList[RandomNum].image!="None")
{
Debug.Log("LoadIMG");
StartCoroutine(FetchLocalPicture( BoardPrefab.transform.Find("图片").GetComponent<Image>(),Application.streamingAssetsPath+"\\Questions\\picture\\"+ QuesionsList[RandomNum].image));
}
else
{
BoardPrefab.transform.Find("图片").GetComponent<Image>().sprite = null;
}
QuesionsList.Remove(QuesionsList[RandomNum]);
}
/// <summary>
/// 读取图片的方法
/// </summary>
/// <param name="image"></param>
/// <param name="url"></param>
/// <returns></returns>
IEnumerator FetchLocalPicture(Image image, string url) {
var uri = new System.Uri(Path.Combine(url));
using (UnityWebRequest uwr = UnityWebRequest.Get(uri)) {
DownloadHandlerTexture downloadHandlerTexture = new DownloadHandlerTexture(true);
uwr.downloadHandler = downloadHandlerTexture;
yield return uwr.SendWebRequest();
if (uwr.result == UnityWebRequest.Result.Success)
{
int width = Screen.width;
int height = Screen.height;
Texture2D texture = new Texture2D(width, height);
texture = downloadHandlerTexture.texture;
Sprite sprite = Sprite.Create(texture,
new Rect(0, 0, 1182, 667),
new Vector2(0.5f, 0.5f));
image.sprite = sprite;
Resources.UnloadUnusedAssets();
}
}
}
}
组件布局: