一、Json选择题题目
- 先再json文本写入选择题题目,选项,和答案
二、制作选择题面板
- 创建一个image作为背景叫BG,在image下面创建一个button开始按钮StarButton
- 在BG下面创建一个text文本显示单项选择题
- 在BG下面再创建一个image作为做选择题时的面板,叫Select
- 在Select下创建一个text文本作为显示选择题题目,叫title
- 在Select下创建四个toggle作为选项
- 在Select下创建一个button下一题按钮NextButton
- 在BG下创建一个text文本显示得分GetScore,
- 效果
- 可以先去把最长的题目和选项先放到text文本里面,调整一下大小
- 然后将Select和GetScore和显示单项选择题的text失活
- 为了保证toggle只能选择一个,在Select添加toggle group组件,然后再四个toggle里的Group属性选择Select。
- 然后四个toggle里的IsOn属性取消勾选
三、脚本编写
读取Json文本
- 之前写过json读取文章,这里不在解释
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class ObjectVo
{
public string title;
public List<string> myContent= new List<string>();
public string answer;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class FataVO
{
public List<ObjectVo> test = new List<ObjectVo>();
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ReadJson : MonoBehaviour {
FataVO data;
public GameObject selectPanel;//选择题界面
public GameObject selectSingle;//单项选择题
public GameObject startButton;
public Text title;
public Toggle[] toggles=new Toggle[4];
public Button nextButton;
public GameObject getScore;
int index = 0;
bool isNext = true;
List<string> str = new List<string>();
string ans = "";
int score=0;
void Start ()
{
TextAsset text = Resources.Load<TextAsset>("MyJson");
string s= text.text;
data = JsonUtility.FromJson<FataVO>(s);
}
public void ShowSelectPanel()
{
selectPanel.SetActive(true);
selectSingle.SetActive(true);
title.text = data.test[index].title;
for (int i = 0; i < 4; i++)
{
toggles[i].transform.GetChild(1).GetComponent<Text>().text = data.test[index].myContent[i];
}
startButton.SetActive(false);
isNext = false;
}
public void NextButtonClick()
{
str.Add(ans);
for (int i = 0; i < toggles.Length; i++)
{
if (toggles[i].isOn)
{
isNext = true;
}
}
if (isNext)
{
index++;
if (index == 10)
{
selectPanel.SetActive(false);
selectSingle.SetActive(false);
getScore.SetActive(true);
for (int i = 0; i < 10; i++)
{
if (str[i]==data.test[i].answer)
{
score += 10;
}
}
getScore.GetComponent<Text>().text = "得分:"+score;
return;
}
title.text = data.test[index].title;
for (int i = 0; i < 4; i++)
{
toggles[i].transform.GetChild(1).GetComponent<Text>().text = data.test[index].myContent[i];
toggles[i].isOn = false;
toggles[i].transform.GetChild(0).GetComponent<Image>().color = Color.white;
}
if (index == 9)
{
nextButton.transform.GetChild(0).GetComponent<Text>().text = "答题完毕";
}
isNext = false;
}
}
public void T1(bool b)
{
if (b)
{
toggles[0].transform.GetChild(0).GetComponent<Image>().color = Color.green;
ans = "A";
}
else
{
toggles[0].transform.GetChild(0).GetComponent<Image>().color = Color.white;
}
}
public void T2(bool b)
{
if (b)
{
toggles[1].transform.GetChild(0).GetComponent<Image>().color = Color.green;
ans = "B";
}
else
{
toggles[1].transform.GetChild(0).GetComponent<Image>().color = Color.white;
}
}
public void T3(bool b)
{
if (b)
{
toggles[2].transform.GetChild(0).GetComponent<Image>().color = Color.green;
ans = "C";
}
else
{
toggles[2].transform.GetChild(0).GetComponent<Image>().color = Color.white;
}
}
public void T4(bool b)
{
if (b)
{
toggles[3].transform.GetChild(0).GetComponent<Image>().color = Color.green;
ans = "D";
}
else
{
toggles[3].transform.GetChild(0).GetComponent<Image>().color = Color.white;
}
}
}
- 把脚本挂在不是失活的物体上就行,然后该拖拽的拖拽
成品
json选择题