1.做一个简单的UI答题面板和得分面板
2.新建DataTest文件夹,在该文件夹中新建Json文件MyData
注:该Json文件的编码格式应该为UTF-8,否则会中文乱码(可以创建好json文件后,使用记事本打开,然后另存为的时候,改变编码),如果已经另存为并且将编码改为UTF-8,回到unity点击文件查看发现仍然乱码,可以尝试新建word文件,将word文件改为json文件,再用记事本另存为UTF-8编码格式
3.将题目数据放入json中(注:答案是随便写的)
{
"selects": [
{
"title": "1.c#的数据类型有()",
"selectAnswer": [ "A.值类型和调用类型", "B.值类型和引用类型", "C.引用类型和关系类型", "D.关系类型和调用类型" ],
"answer": "AD"
},
{
"title": "2.下列描述错误的是()",
"selectAnswer": [ "A.类不可以多重继承而接口可以", "B.抽象类自身可以定义成员而接口不可以", "C.抽象类和接口都不能被实例化", "D.一个类可以有多个基类和多个基接口" ],
"answer": "CD"
},
{
"title": "3.下列关于构造函数的描述正确的是()",
"selectAnswer": [ "A.构造函数可以声明返回类型", "B.构造函数不可以用private修饰", "C.构造函数必须与类名相同", "D.构造函数不能带参数" ],
"answer": "ABC"
},
{
"title": "4.装箱、拆箱操作发生在: ()",
"selectAnswer": [ "A.类与对象之间", "B.对象与对象之间", "C.引用类型与值类型之间", "D.引用类型与引用类型之间" ],
"answer": "ACD"
},
{
"title": "5.下列选项中可以被渲染的纹理是()",
"selectAnswer": [ "A.Texture", "B.Movie Texture", "C.RenderTexture", "D.Texture2D" ],
"answer": "ABCD"
}
]
}
检验json格式是否正确:https://www.bejson.com/
4.挂载脚本到Canvas上,并将对应的物体拖到脚本属性中
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class JsonTest : MonoBehaviour
{
Selections data;
//题目
public Text titleText;
//选项
public Toggle[] toggles = new Toggle[4];
//记录自己选择的所有答案
List<string> answers = new List<string>();
//记录答题时的临时答案
List<string> ans = new List<string>();
//拿题库里面的答案
List<string> allAnswers = new List<string>();
int currentIndex = 0;
public Button last;
public Button next;
//得分
int score = 0;
//得分面板
public GameObject scorePanel;
// Start is called before the first frame update
void Start()
{
//Application.dataPath这个只对PC有效
string path = Application.dataPath + "/DataTest/MyData.json";
string str = File.ReadAllText(path);
data = JsonUtility.FromJson<Selections>(str);
//记录题库中的所有答案
for (int i = 0; i < data.selects.Count; i++)
{
allAnswers.Add(data.selects[i].answer);
}
for (int i = 0; i < allAnswers.Count; i++)
{
answers.Add(null);
}
ShowSelect(currentIndex);
}
//加载题目
public void ShowSelect(int index)
{
//显示题目
titleText.text = data.selects[index].title;
//显示题目
for (int i = 0; i < toggles.Length; i++)
{
toggles[i].transform.GetChild(1).GetComponent<Text>().text = data.selects[index].selectAnswer[i];
}
}
//检测选项是否点击
public void At(bool isSelect)
{
if (isSelect)
ans.Add("A");
else
ans.Remove("A");
}
public void Bt(bool isSelect)
{
if (isSelect)
ans.Add("B");
else
ans.Remove("B");
}
public void Ct(bool isSelect)
{
if (isSelect)
ans.Add("C");
else
ans.Remove("C");
}
public void Dt(bool isSelect)
{
if (isSelect)
ans.Add("D");
else
ans.Remove("D");
}
//整理每道题的答案
public string GetAnswer(List<string> DA)
{
DA.Sort();
string str="";
for(int i = 0; i < DA.Count; i++)
{
str+=DA[i];
}
return str;
}
//下一题
public void NextSelect()
{
answers.RemoveAt(currentIndex);
answers.Insert(currentIndex, GetAnswer(ans));
currentIndex++;
ans.Clear();
if (currentIndex < allAnswers.Count)
{
ShowSelect(currentIndex);
ShowAnswer(currentIndex);
}
else
{
for (int i = 0; i < allAnswers.Count; i++)
{
if (allAnswers[i].Equals(answers[i]))
{
score += 20;
}
}
scorePanel.SetActive(true);
scorePanel.transform.GetChild(0).GetComponent<Text>().text = "得分:" + score;
}
}
//上一题
public void LastSelect()
{
currentIndex--;
ans.Clear();
ShowSelect(currentIndex);
ShowAnswer(currentIndex);
}
//显示当前题目是否已经作答
public void ShowAnswer(int index)
{
for (int i = 0; i < toggles.Length; i++)
{
toggles[i].isOn = false;
}
string tempAns = answers[index];
if(tempAns != null)
{
for (int i = 0; i < tempAns.Length; i++)
{
switch (tempAns[i])
{
case 'A':
toggles[0].isOn = true;
break;
case 'B':
toggles[1].isOn = true;
break;
case 'C':
toggles[2].isOn = true;
break;
case 'D':
toggles[3].isOn = true;
break;
default:
break;
}
}
}
}
//再次答题按钮
public void AgainSelect()
{
answers.Clear();
for (int i = 0; i < allAnswers.Count; i++)
{
answers.Add(null);
}
score = 0;
currentIndex = 0;
ShowSelect(currentIndex);
ShowAnswer(currentIndex);
scorePanel.SetActive(false);
}
// Update is called once per frame
void Update()
{
//隐藏和显示上一题的按钮
if (currentIndex > 0)
{
last.gameObject.SetActive(true);
}
else
{
last.gameObject.SetActive(false);
}
if (currentIndex < allAnswers.Count-1)
{
next.transform.GetChild(0).GetComponent<Text>().text = "下一题";
}
else
{
next.transform.GetChild(0).GetComponent<Text>().text = "结束答题";
}
}
}
[System.Serializable]
public class SelectSingle
{
//名称要和SelectData.json中selects中的属性名中的一一对应
public string title;
public List<string> selectAnswer = new List<string>();
public string answer;
}
[System.Serializable]
public class Selections
{
//这里的selects必须和SelectData.json中的selects中一样才行
public List<SelectSingle> selects = new List<SelectSingle>();
}
5.为不同的toggle和按钮添加脚本中对应的方法
toggle
button
6.运行unity即可
注:如有bug,欢迎指正
结语:合抱之木,生于毫末;九层之台,起于累土;千里之行,始于足下。