Unity Json文本实现单项选择题


一、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选择题


  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity中,可以使用JsonUtility类将数据以Json格式存储起来。具体实现步骤如下: 1. 创建一个数据类,该类包含要存储的数据属性。例如,我们创建一个Person类,包含name和age两个属性。 ``` public class Person { public string name; public int age; } ``` 2. 创建一个保存和读取Json的方法。这里我们创建两个静态方法来实现,一个用于保存数据,一个用于读取数据。 ``` using UnityEngine; using System.IO; public static class JsonHelper { public static void SaveToJson<T>(T data, string filePath) { string jsonData = JsonUtility.ToJson(data); File.WriteAllText(filePath, jsonData); } public static T LoadFromJson<T>(string filePath) { string jsonData = File.ReadAllText(filePath); return JsonUtility.FromJson<T>(jsonData); } } ``` 3. 在保存数据时,调用SaveToJson方法,将数据和文件路径作为参数传入即可。 ``` Person person = new Person(); person.name = "Tom"; person.age = 25; string filePath = Application.persistentDataPath + "/person.json"; JsonHelper.SaveToJson(person, filePath); ``` 4. 在读取数据时,调用LoadFromJson方法,将文件路径作为参数传入即可。 ``` string filePath = Application.persistentDataPath + "/person.json"; Person person = JsonHelper.LoadFromJson<Person>(filePath); Debug.Log("Name: " + person.name + ", Age: " + person.age); ``` 注意,Unity中存储Json文件需要使用Application.persistentDataPath路径,保证在不同设备上都能正常读取。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

X在学了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值