【Unity3D应用案例系列】答题系统开发
发布日期:2021-06-30 11:40:14
浏览次数:2
分类:技术文章
本文共 11738 字,大约阅读时间需要 39 分钟。
推荐阅读
一、前言
这是本专栏系列的第一篇,答题系统的开发。
这个答题系统,可以从文本文档中提取题目和分数,然后绑定到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 ListisAnserList = 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 ListisAnserList = 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、上一题下一题,选项选择,跳转,按钮的功能实现代码还是延期了一贯的简洁风格,希望你可以在这篇文章学到东西。
转载地址:https://itmonon.blog.csdn.net/article/details/117259582 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年05月01日 20时56分28秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
一个数据项目
2021-07-03
java的酒店房间管理系统
2021-07-03
基于Java的截图工具
2021-07-03
基于JAVA_JSP电子书下载系统
2021-07-03
基于java出租车计价器设计与实现
2021-07-03
基于java的B2C的网上拍卖系统
2021-07-03
基于java的百货中心供应链管理系统
2021-07-03
基于java的保险业务管理系统的设计与实现
2021-07-03
基于java的采购管理系统
2021-07-03
基于java的仓库管理系统的设计与实现
2021-07-03
基于java的城市管理系统
2021-07-03
十二时辰篇:这该死的 996
2021-07-03
2021最新 上海互联网公司排名
2021-07-03
字节vs快手!取消大小周之战
2019-04-30
送一个闲置显示器!
2019-04-30
Oracle 行转列 pivot函数基本用法
2019-04-30
Oracle 行转列 动态出转换的列
2019-04-30
Oracle 显式游标的简单案例
2019-04-30
Oracle字符串分隔符替换(替换奇数个或偶数个)
2019-04-30
Oracle 利用 UTL_SMTP 包发送邮件
2019-04-30