Unity3D实战之答题系统的实现

目录

  • 一、前言
  • 三、实现
    • 1.界面搭建
    • 2.读取文档
    • 3.加载题目
    • 4.按钮功能
    • 5.题目对错判断
  • 四、后言

    一、前言 【Unity3D实战之答题系统的实现】这是本专栏系列的第一篇,答题系统的开发。
    这个答题系统,可以从文本文档中提取题目和分数,然后绑定到UI上,在答题的过程中,自动判断分数,自动判断正确率。
    目的是实现一个可快速导入到项目中使用的小模块。
    二、效果图及工程下载
    Unity3D实战之答题系统的实现
    文章图片

    题目文档密码:1234
    源工程

    三、实现
    1.界面搭建
    首先,新建工程,然后摆UI,如下图所示:
    Unity3D实战之答题系统的实现
    文章图片


    2.读取文档
    题目存放在txt文档中,首先,我们看一下结构:
    Unity3D实战之答题系统的实现
    文章图片

    每一行都是一道题目,然后题号、题目、选项、得分,都是用冒号进行分割的。
    下一步就需要用脚本进行读取文档了。
    新建脚本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); }}}

    可以看到,所有的题目数据都读取出来了:
    Unity3D实战之答题系统的实现
    文章图片


    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; //答题Togglepublic 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]; //选项}}}

    Unity3D实战之答题系统的实现
    文章图片

    题目正常加载:
    Unity3D实战之答题系统的实现
    文章图片


    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; }}


    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; }}}

    将按钮对象拖进卡槽中,运行程序即可:
    Unity3D实战之答题系统的实现
    文章图片

    完整代码如下:
    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; //答题Togglepublic 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、上一题下一题,选项选择,跳转,按钮的功能实现
    代码还是延期了一贯的简洁风格,希望你可以在这篇文章学到东西。
    以上就是Unity3D实战之答题系统的实现的详细内容,更多关于Unity3D答题系统的资料请关注脚本之家其它相关文章!

      推荐阅读