Unity3D实战之答题系统的实现
⽬录
⼀、前⾔
三、实现
1.界⾯搭建
2.读取⽂档
3.加载题⽬
4.按钮功能
5.题⽬对错判断
四、后⾔
⼀、前⾔
这是本专栏系列的第⼀篇,答题系统的开发。
这个答题系统,可以从⽂本⽂档中提取题⽬和分数,然后绑定到UI上,在答题的过程中,⾃动判断分数,⾃动判断正确率。⽬的是实现⼀个可快速导⼊到项⽬中使⽤的⼩模块。
⼆、效果图及⼯程下载
密码:1234
三、实现
1.界⾯搭建
⾸先,新建⼯程,然后摆UI,如下图所⽰:
2.读取⽂档
题⽬存放在txt⽂档中,⾸先,我们看⼀下结构:
每⼀⾏都是⼀道题⽬,然后题号、题⽬、选项、得分,都是⽤冒号进⾏分割的。
下⼀步就需要⽤脚本进⾏读取⽂档了。
新建脚本Answer.cs:编写代码:
读取⽂档:
using System.Collections.Generic;
using UnityEngine;
public class Answer : MonoBehaviour
{
//读取⽂档
string[][] ArrayX;
string[] lineArray;
private int topicMax = 0;//最⼤题数
private List<bool> isAnserList = new List<bool>();//存放是否答过题的状态
void Start()
{
TextCsv();
}
/*****************读取txt数据******************/
void TextCsv()
//读取csv⼆进制⽂件
TextAsset binAsset = Resources.Load("YW", typeof(TextAsset)) as TextAsset; //读取每⼀⾏的内容
lineArray = 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.加载题⽬
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;typeof array
public class Answer : MonoBehaviour
{
//读取⽂档
string[][] ArrayX;//题⽬数据
string[] lineArray;//读取到题⽬数据
private int topicMax = 0;//最⼤题数
private List<bool> isAnserList = new List<bool>();//存放是否答过题的状态
/
/加载题⽬
public GameObject tipsbtn;//提⽰按钮
public Text tipsText;//提⽰信息
public List<Toggle> toggleList;//答题Toggle
public Text indexText;//当前第⼏题
public Text TM_Text;//当前题⽬
public List<Text> DA_TextList;//选项
private int topicIndex = 0;//第⼏题
void Start()
{
TextCsv();
LoadAnswer();
}
/*****************读取txt数据******************/
void TextCsv()
{
//读取csv⼆进制⽂件
TextAsset binAsset = Resources.Load("YW", typeof(TextAsset)) as TextAsset; //读取每⼀⾏的内容
lineArray = 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);
< = "";
for (int x = 0; x < 4; x++)
{
toggleList[x].isOn = false;
}
< = "第" + (topicIndex + 1) + "题:";//第⼏题
= ArrayX[topicIndex][1];//题⽬
int idx = ArrayX[topicIndex].Length - 3;//有⼏个选项
for (int x = 0; x < idx; x++)
{
DA_TextList[x].text = ArrayX[topicIndex][x + 2];//选项
}
}
}
题⽬正常加载:
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;
}
< = "<color=#FFAB08FF>" +"正确答案是:"+ nM + "</color>";
break;
case 1://上⼀题
if (topicIndex > 0)
{
topicIndex--;
LoadAnswer();
}
else
{
< = "<color=#27FF02FF>" + "前⾯已经没有题⽬了!" + "</color>"; }
break;
case 2://下⼀题
if (topicIndex < topicMax-1)
{
topicIndex++;
}
else
{
< = "<color=#27FF02FF>" + "哎呀!已经是最后⼀题了。" + "</color>";
}
break;
case 3://跳转
int x = int.) - 1;
if (x >= 0 && x < topicMax)
{
topicIndex = x;
< = "";
LoadAnswer();
}
else
{
< = "<color=#27FF02FF>" + "不在范围内!" + "</color>";
}
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)
{
< = "<color=#27FF02FF>" + "恭喜你,答对了!" + "</color>";
isRight = true;
tipsbtn.SetActive(true);
}
else
{
< = "<color=#FF0020FF>" + "对不起,答错了!" + "</color>";
isRight = false;
tipsbtn.SetActive(true);
}
//正确率计算
if (isAnserList[topicIndex])
{
< = "<color=#FF0020FF>" + "这道题已答过!" + "</color>";
}
else
{
anserint++;
if (isRight)
{
isRightNum++;
}
isAnserList[topicIndex] = true;
< = "正确率:" + ((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<bool> isAnserList = new List<bool>();//存放是否答过题的状态
//加载题⽬
public GameObject tipsbtn;//提⽰按钮
public Text tipsText;//提⽰信息
public List<Toggle> toggleList;//答题Toggle
public Text indexText;//当前第⼏题
public Text TM_Text;//当前题⽬
public List<Text> 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)); Click.AddListener(() => Select_Answer(0));
}
/*****************读取txt数据******************/
void TextCsv()
{
//读取csv⼆进制⽂件
TextAsset binAsset = Resources.Load("YW", typeof(TextAsset)) as TextAsset;
//读取每⼀⾏的内容
lineArray = 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()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论