還在Unity開發游戲?那你就out了,試試用Unity做一個答題系統吧

      網友投稿 910 2022-05-29

      一、前言

      大家都支持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、解析文檔保存數據

      還在用Unity開發游戲?那你就out了,試試用Unity做一個答題系統吧

      3、根據數據加載題目

      4、上一題下一題,選項選擇,跳轉,按鈕的功能實現

      代碼還是延期了一貫的簡潔風格,希望你可以在這篇文章學到東西。

      5G游戲 unity 數據結構

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:RK3399平臺開發系列講解(系統篇)1.26、目標檢測模型Nanodet的訓練和轉換總結
      下一篇:使用PowerDesigner做數據庫設計(二)
      相關文章
      久久亚洲美女精品国产精品| 久久亚洲AV无码精品色午夜麻| 国产v亚洲v天堂无码网站| 亚洲人成无码www久久久| 日本系列1页亚洲系列| 亚洲爆乳AAA无码专区| 久久久久se色偷偷亚洲精品av | 亚洲性线免费观看视频成熟| 亚洲国产成人超福利久久精品 | 亚洲AⅤ无码一区二区三区在线| 亚洲AV永久无码天堂影院| 亚洲乱妇老熟女爽到高潮的片| 亚洲中文字幕无码中文| 亚洲日韩亚洲另类激情文学| 中文字幕在线日亚洲9| 亚洲人成网站色7799| 鲁死你资源站亚洲av| 国内成人精品亚洲日本语音| 亚洲av中文无码| 亚洲一区二区三区AV无码| 亚洲欧洲日产国码av系列天堂 | 亚洲永久网址在线观看| 亚洲精品自偷自拍无码| 亚洲JIZZJIZZ妇女| 精品久久亚洲一级α| 亚洲免费在线观看| 亚洲中文字幕无码久久2017| 狠狠色伊人亚洲综合成人| 亚洲一区中文字幕久久| 91嫩草亚洲精品| 亚洲综合一区国产精品| 国产精品亚洲一区二区在线观看| 国产成人亚洲精品无码AV大片| 亚洲精品国自产拍在线观看| 国产综合亚洲专区在线| 亚洲AV日韩AV永久无码绿巨人 | vvvv99日韩精品亚洲| 亚洲综合久久夜AV | 亚洲熟妇无码AV在线播放| 婷婷久久久亚洲欧洲日产国码AV| 亚洲精品第一国产综合精品|