知識問答完整專案資源下載鏈接:
https://download.csdn.net/download/qq_34421469/15078659
以下為關鍵代碼:
讀取Excel轉成指定資料型別的List集合
using Excel;
using System.Data;
using System.IO;
using System.Collections.Generic;
public class ReadExcel
{
/// <summary>
/// 讀取Excel轉List(List型別可變)
/// </summary>
/// <param name="excelPath"></param>
/// <returns></returns>
public static List<ExcelData> ReadToList(string excelPath)
{
FileStream stream = File.Open(excelPath, FileMode.Open, FileAccess.Read, FileShare.Read);
IExcelDataReader excelReader;
if (excelPath.Split('.')[1] == "xls")
{
//讀取 *.xls 檔案
excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else
{
//讀取 *.xlsx 檔案
excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
DataSet result = excelReader.AsDataSet();
int rows = result.Tables[0].Rows.Count;
int cols = result.Tables[0].Columns.Count;
List<ExcelData> excelDatas = new List<ExcelData>();
ExcelData excelData = new ExcelData();
for (int i = 1; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
switch (result.Tables[0].Rows[0][j].ToString())
{
case ExcelDataIndex.ID:
excelData.ID = result.Tables[0].Rows[i][j].ToString();
break;
case ExcelDataIndex.Category:
excelData.Category = result.Tables[0].Rows[i][j].ToString();
break;
case ExcelDataIndex.Question:
excelData.Question = result.Tables[0].Rows[i][j].ToString();
break;
case ExcelDataIndex.OptionA:
excelData.OptionA = result.Tables[0].Rows[i][j].ToString();
break;
case ExcelDataIndex.OptionB:
excelData.OptionB = result.Tables[0].Rows[i][j].ToString();
break;
case ExcelDataIndex.OptionC:
excelData.OptionC = result.Tables[0].Rows[i][j].ToString();
break;
case ExcelDataIndex.OptionD:
excelData.OptionD= result.Tables[0].Rows[i][j].ToString();
break;
case ExcelDataIndex.Answer:
excelData.Answer = result.Tables[0].Rows[i][j].ToString();
break;
}
}
excelDatas.Add(excelData);
excelData = new ExcelData();
}
return excelDatas;
}
}
通過獲取到的題庫,實作簡單知識問答游戲(答題結束有分數,等級等內容)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Button = UnityEngine.UI.Button;
public class PrintExcel : MonoBehaviour {
public static PrintExcel instance;
public TextAsset json;
public List<DepenceTableData> listdata;
public Text txt_Ques; //題目內容
public Text txt_QuesCount; //當前題目數量
public GameObject selectItem; //選項
public Text txt_A; //選項A內容
public Text txt_B; //選項B內容
public Text txt_C; //選項C內容
public Text txt_D; //選項D內容
public Text txt_CountDown; //倒計時文本
public GameObject img_Score; //得分UI
public AudioSource audioSource;
public AudioClip[] audioClips;
public Texture[] TorF_Icon; //0:Empty 1:Right 2:Wrong
string _mAnswer = ""; //正確答案
// 設定出題的數量
int queCount;
//出題范圍
int startNum;
int endNum;
//用來存盤Inspector面板上的的賦值
int oldStartNum;
[HideInInspector]
public string selectName="";
private int index;
private int grade=0; //答題得分
private int rightCount = 0; //答對題數量
private int wrongCount = 0; //答錯題數量
private int currentQueCount = 0; //當前題目數量
private AudioSource audio_PressBtn; //點擊按鈕音效
private void Awake()
{
audio_PressBtn = selectItem.GetComponent<AudioSource>();
if (instance == null)
{
instance = this;
}
}
public void Init()
{
listdata = DoExcel.Load(json); //讀取題目
queCount = 20;
startNum = 1;
endNum = listdata.Count;
oldStartNum = endNum;
grade = 0;
rightCount = 0;
wrongCount = 0;
currentQueCount = 0;
txt_A.text = "";
txt_B.text = "";
txt_C.text = "";
txt_D.text = "";
txt_Ques.text = "";
txt_QuesCount.text = currentQueCount + "/20";
//顯示題目
SelectQues();
}
/// <summary>
/// 隨機獲取題目串列
/// </summary>
/// <returns></returns>
public DepenceTableData GetDate()
{
index = Random.Range(startNum, endNum);
--endNum;
DepenceTableData date = listdata[index];
Debug.Log(date);
//移除已經選過的題目 以防出現重復題目
listdata.RemoveAt(index);
return date;
}
/// <summary>
/// 獲取到題目顯示
/// </summary>
public void SelectQues()
{
queCount--;
currentQueCount++;
DepenceTableData data = GetDate();
txt_Ques.text= data.Ques;
txt_QuesCount.text = currentQueCount + "/20";
txt_A.text = data.A;
txt_B.text = data.B;
txt_C.text = data.C;
txt_D.text = data.D;
_mAnswer = data.answer;
SetBtnInteractable(true);
}
/// <summary>
/// 判斷答案是否正確
/// </summary>
/// <param name="selectBtn">用戶選擇的選項</param>
public void isAnswer(string name)
{
selectName = name;
SetBtnInteractable(false);
if (name == _mAnswer)
{
SetBtnColor(name, 1);
PlayEffectAudio(0); //音效
img_Score.SetActive(true);
rightCount++;
grade += 5;
}
else
{
SetBtnColor(name, 2);
SetBtnColor(_mAnswer, 1);
PlayEffectAudio(1); //音效
wrongCount++;
}
Debug.Log("當前答對" + rightCount + "題,答錯" + wrongCount + "題");
StartCoroutine(WaitForNextQues()); //下一題
}
/// <summary>
/// 下一題
/// </summary>
/// <returns></returns>
IEnumerator WaitForNextQues()
{
yield return new WaitForSeconds(1);
if (queCount > 0)
{
txt_CountDown.text = "3";
yield return new WaitForSeconds(1);
txt_CountDown.text = "2";
yield return new WaitForSeconds(1);
txt_CountDown.text = "1";
yield return new WaitForSeconds(1);
txt_CountDown.text = "";
img_Score.SetActive(false);
SetBtnColor(selectName, 0);
SetBtnColor(_mAnswer, 0);
SelectQues();
}
else
{
yield return new WaitForSeconds(2);
SetBtnColor(selectName, 0);
SetBtnColor(_mAnswer, 0);
End();
}
}
/// <summary>
/// 答題結束
/// </summary>
public void End()
{
int rank = 0;
if (grade == 100)
{
//答題通過
rank = 1;
}
else if (grade >= 70 && grade < 100)
{
//答題通過
rank = 2;
}
else if (grade < 70 && grade >= 60)
{
//答題通過
rank = 3;
}
else if (grade < 60)
{
//答題未通過
rank = 4;
}
StartCoroutine(UIManager.instance.EndOfAnswer(grade, rank));
}
/// <summary>
/// 判斷對錯
/// </summary>
/// <param name="name"></param>
/// <param name="i"></param>
public void SetBtnColor(string name,int i)
{
GameObject.Find(name).GetComponentInChildren<RawImage>().texture = TorF_Icon[i];
}
/// <summary>
/// 設定音效
/// </summary>
/// <param name="clip"></param>
void PlayEffectAudio(int clip)
{
audioSource.clip = audioClips[clip];
audioSource.Play();
}
void SetBtnInteractable(bool isInter)
{
for(int i = 0; i < selectItem.transform.childCount; i++)
{
selectItem.transform.GetChild(i).GetComponent<Button>().interactable = isInter;
if (selectItem.transform.GetChild(i).GetComponentInChildren<Text>().text == "")
{
selectItem.transform.GetChild(i).GetComponent<Button>().interactable = false;
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/257216.html
標籤:其他
上一篇:WC2021 菊蒻yxy的題解
