还在用Unity开发游戏?那你就out了,试试用Unity做

一、前言

大家都支持Unity是用来做游戏开发,比如说做2D游戏、3D游戏,或者工业虚拟仿真软件的开发。

其他Unity可以做的有很多,比如答题系统。

本篇就介绍答题系统的开发

这个答题系统,可以从文本文档中提取题目和分数,然后绑定到UI上,在答题的过程中,自动判断分数,自动判断正确率。

目的是实现一个可快速导入到项目中使用的小模块。

二、效果图及工程下载

题目文档:
wwr.lanzoui.com/ihV6nphkzsf
密码:47z2

源工程:
wwr.lanzoui.com/i7wpaphkzuh

三、实现

3-1 界面搭建

首先,新建工程,然后摆UI,如下图所示:
在这里插入图片描述

3-2 读取文档

题目存放在txt文档中,首先,我们看一下结构:
在这里插入图片描述

每一行都是一道题目,然后题号、题目、选项、得分,都是用冒号进行分割的。

下一步就需要用脚本进行读取文档了。

新建脚本Answer.cs:编写代码:

读取文档:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
csharp复制代码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 = 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 加载题目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
csharp复制代码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;//第几题


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 按钮功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
csharp复制代码 /*****************按钮功能******************/
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 = "<color=#FFAB08FF>" +"正确答案是:"+ nM + "</color>";
break;
case 1://上一题
if (topicIndex > 0)
{
topicIndex--;
LoadAnswer();
}
else
{
tipsText.text = "<color=#27FF02FF>" + "前面已经没有题目了!" + "</color>";
}
break;
case 2://下一题
if (topicIndex < topicMax-1)
{
topicIndex++;
LoadAnswer();
}
else
{
tipsText.text = "<color=#27FF02FF>" + "哎呀!已经是最后一题了。" + "</color>";
}
break;
case 3://跳转
int x = int.Parse(jumpInput.text) - 1;
if (x >= 0 && x < topicMax)
{
topicIndex = x;
jumpInput.text = "";
LoadAnswer();
}
else
{
tipsText.text = "<color=#27FF02FF>" + "不在范围内!" + "</color>";
}
break;
}
}

3-5 题目对错判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
csharp复制代码/*****************题目对错判断******************/
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 = "<color=#27FF02FF>" + "恭喜你,答对了!" + "</color>";
isRight = true;
tipsbtn.SetActive(true);
}
else
{
tipsText.text = "<color=#FF0020FF>" + "对不起,答错了!" + "</color>";
isRight = false;
tipsbtn.SetActive(true);
}

//正确率计算
if (isAnserList[topicIndex])
{
tipsText.text = "<color=#FF0020FF>" + "这道题已答过!" + "</color>";
}
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
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
csharp复制代码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));

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 = "<color=#FFAB08FF>" +"正确答案是:"+ nM + "</color>";
break;
case 1://上一题
if (topicIndex > 0)
{
topicIndex--;
LoadAnswer();
}
else
{
tipsText.text = "<color=#27FF02FF>" + "前面已经没有题目了!" + "</color>";
}
break;
case 2://下一题
if (topicIndex < topicMax-1)
{
topicIndex++;
LoadAnswer();
}
else
{
tipsText.text = "<color=#27FF02FF>" + "哎呀!已经是最后一题了。" + "</color>";
}
break;
case 3://跳转
int x = int.Parse(jumpInput.text) - 1;
if (x >= 0 && x < topicMax)
{
topicIndex = x;
jumpInput.text = "";
LoadAnswer();
}
else
{
tipsText.text = "<color=#27FF02FF>" + "不在范围内!" + "</color>";
}
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 = "<color=#27FF02FF>" + "恭喜你,答对了!" + "</color>";
isRight = true;
tipsbtn.SetActive(true);
}
else
{
tipsText.text = "<color=#FF0020FF>" + "对不起,答错了!" + "</color>";
isRight = false;
tipsbtn.SetActive(true);
}

//正确率计算
if (isAnserList[topicIndex])
{
tipsText.text = "<color=#FF0020FF>" + "这道题已答过!" + "</color>";
}
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、上一题下一题,选项选择,跳转,按钮的功能实现

代码还是延期了一贯的简洁风格,希望你可以在这篇文章学到东西。

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%