目录
一、作品展示
1、菜单界面:
2、答题界面:
3、学习模式界面:
二、代码展示:
1、菜单页面:
三个场景跳转按钮:
(1)学习党史按钮:
(2)答题测试按钮:
(3)退出系统按钮:
2、退出按钮:
5、学习界面代码:
(1)代码
4、答题界面代码:
(1)返回开始菜单按钮:
(2)答题处理主代码:
三、相应资源:
1、txt文件格式
2、如何修改题目内容
四、尾声
(注:由于特殊原因,原图无法展示,请谅解)
(注:由于特殊原因,原图无法展示,请谅解)
(注:由于特殊原因,原图无法展示,请谅解)
using UnityEngine.SceneManagement;
using UnityEngine;
public class EnterStudy : MonoBehaviour
{
public void EnterStudy_()
{
SceneManager.LoadScene("学习界面");
}
}
using UnityEngine.SceneManagement;
using UnityEngine;
public class EnterTest : MonoBehaviour
{
public void EnterTest_()
{
SceneManager.LoadScene("example");
}
}
using UnityEngine;
public class ExitSystem : MonoBehaviour
{
public void Quit()
{
Application.Quit();
}
}
using UnityEngine;
public class ExitSystem : MonoBehaviour
{
public void Quit()
{
Application.Quit();
}
}
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Text;
using System.Collections.Generic;
public class TXT_OPRATION : MonoBehaviour
{
//public TextAsset textTxt;
//读取文档
string[][] ArrayX;//题目数据
string[] lineArray;//读取到题目数据
public Text stuText;
string[] items = new string[7]{"题号:", "题目:", "A:", "B:", "C:", "D:", "答案:" };
void Start()
{
LoadTxt();
//Debug.Log(textTxt.text);
}
private void LoadTxt()
{
string UnityPath1 = Application.dataPath + "/StreamingAssets/题目5.txt";
string[] allLineText = File.ReadAllLines(UnityPath1);
for (int i = 0; i < allLineText.Length; i++)
{
Debug.Log(allLineText.Length);
}
ArrayX = new string[allLineText.Length][];
//把csv中的数据储存在二维数组中
for (int i = 0; i < allLineText.Length; i++)
{
ArrayX[i] = allLineText[i].Split(':');
}
//查看保存的题目数据
int k = 0;
string texts = "";
for (int i = 0; i < ArrayX.Length; i++)
{
//texts += "题号:";
for (int j = 0; j < ArrayX[i].Length; j++)
{
//Debug.Log(ArrayX[i][j]);
texts += items[k];
texts += ArrayX[i][j];
texts += '\n';
k++;
if (k == 7)
{
k = 0;
}
}
}
stuText.text = texts;
}
}
using UnityEngine.SceneManagement;
using UnityEngine;
public class returnMenu : MonoBehaviour
{
public void ReturnMenu()
{
SceneManager.LoadScene("登录界面");
}
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Text;
public class testAnswer : 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()
{
string UnityPath1 = Application.dataPath + "/StreamingAssets/题目5.txt";
string[] allLineText = File.ReadAllLines(UnityPath1);
for (int i = 0; i < allLineText.Length; i++)
{
Debug.Log(allLineText.Length);
}
ArrayX = new string[allLineText.Length][];
//把csv中的数据储存在二维数组中
for (int i = 0; i < allLineText.Length; i++)
{
ArrayX[i] = allLineText[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 = allLineText.Length;
for (int x = 0; x < topicMax; 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;
}
}
}
}
题号:题目:A选项:B选项:C选项:D选项:答案的序号
题号:题目:A选项:B选项:C选项:D选项:答案的序号
题号:题目:A选项:B选项:C选项:D选项:答案的序号
题号:题目:A选项:B选项:C选项:D选项:答案的序号
题号:题目:A选项:B选项:C选项:D选项:答案的序号
……
(注:最后一题的末尾不加回车,所有冒号都是英文格式)
这是本系统的一大亮点,也是Unity开发的一个神奇的特性。往往我们做的程序想要修改信息都要连接云服务器、数据库什么的,很少有能在本地直接修改的,并且修改完内置文件之后往往都会使程序崩溃,本系统使用的数据修改方法是将题目写在外部文档,使程序能够动态读取和修改题目信息。这种方式支持的文档格式可以是txt、xml、json,本系统用的是最简便的txt文本。
这是因为我们在Unity中新建一个StreamingAssets文件夹,这个文件夹中的内容可以在应用发布时原封不动地打包进去(不会被加密和压缩),一般用来存放二进制文件。
有了这个文件夹,我们就可以在打包完之后也可以进行文件中的修改(只限制于StreamingAssets文件夹)从而实现增删题目的功能。
唯一的限制就是添加修改题目要满足固定格式,不然读取会出现错误。
(按此路径找到题目文件)
E:\xx学习系统5.0plus\xx学习与答题系统_Data\StreamingAssets
诚挚感谢博主恬静的小魔龙 、
York_New和的文章令我受益匪浅。
参考文章:(8条消息) 【Unity3D应用案例系列】答题系统开发_恬静的小魔龙的博客-CSDN博客_unity答题系统https://blog.csdn.net/q764424567/article/details/117259582?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165191697316781483767112%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=165191697316781483767112&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_click~default-1-117259582-null-null.142%5Ev9%5Epc_search_result_control_group,157%5Ev4%5Econtrol&utm_term=unity%E7%AD%94%E9%A2%98&spm=1018.2226.3001.4187
(2条消息) Unity学习篇之txt文本文档的多种读写方式_York_New的博客-CSDN博客_unity读取文本https://blog.csdn.net/York_New/article/details/84302895?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-1-84302895-null-null.pc_agg_new_rank&utm_term=unity%E4%BF%AE%E6%94%B9txt&spm=1000.2123.3001.4430(4条消息) Unity学习篇之txt文本文档的多种读写方式_York_New的博客-CSDN博客_unity读取文本https://blog.csdn.net/York_New/article/details/84302895?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-1-84302895-null-null.pc_agg_new_rank&utm_term=unity%E4%BF%AE%E6%94%B9txt&spm=1000.2123.3001.4430
(2条消息) unity修改txt - CSDNhttps://www.csdn.net/tags/Mtjacg5sMTI3NjEtYmxvZwO0O0OO0O0O.html
最后放上作品链接,大家一起来学习吧!
链接:https://pan.baidu.com/s/1Em67E803tY_HHyEcg7tgCw
提取码:uf29