一、前言
大家都支持Unity是用來做游戲開發,比如說做2D游戲、3D游戲,或者工業虛擬仿真軟件的開發。
其他Unity可以做的有很多,比如答題系統。
本篇就介紹答題系統的開發
這個答題系統,可以從文本文檔中提取題目和分數,然后綁定到UI上,在答題的過程中,自動判斷分數,自動判斷正確率。
目的是實現一個可快速導入到項目中使用的小模塊。
二、效果圖及工程下載
題目文檔:
https://wwr.lanzoui.com/ihV6nphkzsf
密碼:47z2
源工程:
https://wwr.lanzoui.com/i7wpaphkzuh
三、實現
3-1 界面搭建
首先,新建工程,然后擺UI,如下圖所示:
3-2 讀取文檔
題目存放在txt文檔中,首先,我們看一下結構:
每一行都是一道題目,然后題號、題目、選項、得分,都是用冒號進行分割的。
下一步就需要用腳本進行讀取文檔了。
新建腳本Answer.cs:編寫代碼:
讀取文檔:
using System.Collections.Generic; using UnityEngine; public class Answer : MonoBehaviour { //讀取文檔 string[][] ArrayX; string[] lineArray; private int topicMax = 0;//最大題數 private List isAnserList = new List();//存放是否答過題的狀態 void Start() { TextCsv(); } /*****************讀取txt數據******************/ void TextCsv() { //讀取csv二進制文件 TextAsset binAsset = Resources.Load("YW", typeof(TextAsset)) as TextAsset; //讀取每一行的內容 lineArray = binAsset.text.Split('\r'); //創建二維數組 ArrayX = new string[lineArray.Length][]; //把csv中的數據儲存在二維數組中 for (int i = 0; i < lineArray.Length; i++) { ArrayX[i] = lineArray[i].Split(':'); } //查看保存的題目數據 for (int i = 0; i < ArrayX.Length; i++) { for (int j = 0; j < ArrayX[i].Length; j++) { Debug.Log(ArrayX[i][j]); } } //設置題目狀態 topicMax = lineArray.Length; for (int x = 0; x < topicMax + 1; x++) { isAnserList.Add(false); } } }
可以看到,所有的題目數據都讀取出來了:
3-3 加載題目
using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Answer : MonoBehaviour { //讀取文檔 string[][] ArrayX;//題目數據 string[] lineArray;//讀取到題目數據 private int topicMax = 0;//最大題數 private List isAnserList = new List();//存放是否答過題的狀態 //加載題目 public GameObject tipsbtn;//提示按鈕 public Text tipsText;//提示信息 public List toggleList;//答題Toggle public Text indexText;//當前第幾題 public Text TM_Text;//當前題目 public List DA_TextList;//選項 private int topicIndex = 0;//第幾題 void Start() { TextCsv(); LoadAnswer(); } /*****************讀取txt數據******************/ void TextCsv() { //讀取csv二進制文件 TextAsset binAsset = Resources.Load("YW", typeof(TextAsset)) as TextAsset; //讀取每一行的內容 lineArray = binAsset.text.Split('\r'); //創建二維數組 ArrayX = new string[lineArray.Length][]; //把csv中的數據儲存在二維數組中 for (int i = 0; i < lineArray.Length; i++) { ArrayX[i] = lineArray[i].Split(':'); } //設置題目狀態 topicMax = lineArray.Length; for (int x = 0; x < topicMax + 1; x++) { isAnserList.Add(false); } } /*****************加載題目******************/ void LoadAnswer() { tipsbtn.SetActive(false); tipsText.text = ""; for (int x = 0; x < 4; x++) { toggleList[x].isOn = false; } indexText.text = "第" + (topicIndex + 1) + "題:";//第幾題 TM_Text.text = ArrayX[topicIndex][1];//題目 int idx = ArrayX[topicIndex].Length - 3;//有幾個選項 for (int x = 0; x < idx; x++) { DA_TextList[x].text = ArrayX[topicIndex][x + 2];//選項 } } }
題目正常加載:
3-4 按鈕功能
/*****************按鈕功能******************/ void Select_Answer(int index) { switch (index) { case 0://提示 int idx = ArrayX[topicIndex].Length - 1; int n = int.Parse(ArrayX[topicIndex][idx]); string nM = ""; switch (n) { case 1: nM = "A"; break; case 2: nM = "B"; break; case 3: nM = "C"; break; case 4: nM = "D"; break; } tipsText.text = "" +"正確答案是:"+ nM + ""; break; case 1://上一題 if (topicIndex > 0) { topicIndex--; LoadAnswer(); } else { tipsText.text = "" + "前面已經沒有題目了!" + ""; } break; case 2://下一題 if (topicIndex < topicMax-1) { topicIndex++; LoadAnswer(); } else { tipsText.text = "" + "哎呀!已經是最后一題了。" + ""; } break; case 3://跳轉 int x = int.Parse(jumpInput.text) - 1; if (x >= 0 && x < topicMax) { topicIndex = x; jumpInput.text = ""; LoadAnswer(); } else { tipsText.text = "" + "不在范圍內!" + ""; } break; } }
3-5 題目對錯判斷
/*****************題目對錯判斷******************/ void AnswerRightRrongJudgment(bool check,int index) { if (check) { //判斷題目對錯 bool isRight; int idx = ArrayX[topicIndex].Length - 1; int n = int.Parse(ArrayX[topicIndex][idx]) - 1; if (n == index) { tipsText.text = "" + "恭喜你,答對了!" + ""; isRight = true; tipsbtn.SetActive(true); } else { tipsText.text = "" + "對不起,答錯了!" + ""; isRight = false; tipsbtn.SetActive(true); } //正確率計算 if (isAnserList[topicIndex]) { tipsText.text = "" + "這道題已答過!" + ""; } else { anserint++; if (isRight) { isRightNum++; } isAnserList[topicIndex] = true; TextAccuracy.text = "正確率:" + ((float)isRightNum / anserint * 100).ToString("f2") + "%"; } //禁用掉選項 for (int i = 0; i < toggleList.Count; i++) { toggleList[i].interactable = false; } } }
將按鈕對象拖進卡槽中,運行程序即可:
完整代碼如下:
using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Answer : MonoBehaviour { //讀取文檔 string[][] ArrayX;//題目數據 string[] lineArray;//讀取到題目數據 private int topicMax = 0;//最大題數 private List isAnserList = new List();//存放是否答過題的狀態 //加載題目 public GameObject tipsbtn;//提示按鈕 public Text tipsText;//提示信息 public List toggleList;//答題Toggle public Text indexText;//當前第幾題 public Text TM_Text;//當前題目 public List DA_TextList;//選項 private int topicIndex = 0;//第幾題 //按鈕功能及提示信息 public Button BtnBack;//上一題 public Button BtnNext;//下一題 public Button BtnTip;//消息提醒 public Button BtnJump;//跳轉題目 public InputField jumpInput;//跳轉題目 public Text TextAccuracy;//正確率 private int anserint = 0;//已經答過幾題 private int isRightNum = 0;//正確題數 void Awake() { TextCsv(); LoadAnswer(); } void Start() { toggleList[0].onValueChanged.AddListener((isOn) => AnswerRightRrongJudgment(isOn,0)); toggleList[1].onValueChanged.AddListener((isOn) => AnswerRightRrongJudgment(isOn,1)); toggleList[2].onValueChanged.AddListener((isOn) => AnswerRightRrongJudgment(isOn,2)); toggleList[3].onValueChanged.AddListener((isOn) => AnswerRightRrongJudgment(isOn,3)); BtnTip.onClick.AddListener(() => Select_Answer(0)); BtnBack.onClick.AddListener(() => Select_Answer(1)); BtnNext.onClick.AddListener(() => Select_Answer(2)); BtnJump.onClick.AddListener(() => Select_Answer(3)); } /*****************讀取txt數據******************/ void TextCsv() { //讀取csv二進制文件 TextAsset binAsset = Resources.Load("YW", typeof(TextAsset)) as TextAsset; //讀取每一行的內容 lineArray = binAsset.text.Split('\r'); //創建二維數組 ArrayX = new string[lineArray.Length][]; //把csv中的數據儲存在二維數組中 for (int i = 0; i < lineArray.Length; i++) { ArrayX[i] = lineArray[i].Split(':'); } //設置題目狀態 topicMax = lineArray.Length; for (int x = 0; x < topicMax + 1; x++) { isAnserList.Add(false); } } /*****************加載題目******************/ void LoadAnswer() { for (int i = 0; i < toggleList.Count; i++) { toggleList[i].isOn = false; } for (int i = 0; i < toggleList.Count; i++) { toggleList[i].interactable = true; } tipsbtn.SetActive(false); tipsText.text = ""; indexText.text = "第" + (topicIndex + 1) + "題:";//第幾題 TM_Text.text = ArrayX[topicIndex][1];//題目 int idx = ArrayX[topicIndex].Length - 3;//有幾個選項 for (int x = 0; x < idx; x++) { DA_TextList[x].text = ArrayX[topicIndex][x + 2];//選項 } } /*****************按鈕功能******************/ void Select_Answer(int index) { switch (index) { case 0://提示 int idx = ArrayX[topicIndex].Length - 1; int n = int.Parse(ArrayX[topicIndex][idx]); string nM = ""; switch (n) { case 1: nM = "A"; break; case 2: nM = "B"; break; case 3: nM = "C"; break; case 4: nM = "D"; break; } tipsText.text = "" +"正確答案是:"+ nM + ""; break; case 1://上一題 if (topicIndex > 0) { topicIndex--; LoadAnswer(); } else { tipsText.text = "" + "前面已經沒有題目了!" + ""; } break; case 2://下一題 if (topicIndex < topicMax-1) { topicIndex++; LoadAnswer(); } else { tipsText.text = "" + "哎呀!已經是最后一題了。" + ""; } break; case 3://跳轉 int x = int.Parse(jumpInput.text) - 1; if (x >= 0 && x < topicMax) { topicIndex = x; jumpInput.text = ""; LoadAnswer(); } else { tipsText.text = "" + "不在范圍內!" + ""; } break; } } /*****************題目對錯判斷******************/ void AnswerRightRrongJudgment(bool check,int index) { if (check) { //判斷題目對錯 bool isRight; int idx = ArrayX[topicIndex].Length - 1; int n = int.Parse(ArrayX[topicIndex][idx]) - 1; if (n == index) { tipsText.text = "" + "恭喜你,答對了!" + ""; isRight = true; tipsbtn.SetActive(true); } else { tipsText.text = "" + "對不起,答錯了!" + ""; isRight = false; tipsbtn.SetActive(true); } //正確率計算 if (isAnserList[topicIndex]) { tipsText.text = "" + "這道題已答過!" + ""; } else { anserint++; if (isRight) { isRightNum++; } isAnserList[topicIndex] = true; TextAccuracy.text = "正確率:" + ((float)isRightNum / anserint * 100).ToString("f2") + "%"; } //禁用掉選項 for (int i = 0; i < toggleList.Count; i++) { toggleList[i].interactable = false; } } } }
四、后言
整體來看,只使用了一個場景,一個腳本,就完成了答題系統。
步驟如下:
1、讀取文檔
2、解析文檔保存數據

3、根據數據加載題目
4、上一題下一題,選項選擇,跳轉,按鈕的功能實現
代碼還是延期了一貫的簡潔風格,希望你可以在這篇文章學到東西。
5G游戲 unity 數據結構
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。