Json の複数選択の質問: https://blog.csdn.net/falsedewuxin/article/details/129653639?spm=1001.2014.3001.5501
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": "A"
},
{
"title": "2.下列描述错误的是()",
"selectAnswer": [ "A.类不可以多重继承而接口可以", "B.抽象类自身可以定义成员而接口不可以", "C.抽象类和接口都不能被实例化", "D.一个类可以有多个基类和多个基接口" ],
"answer": "D"
},
{
"title": "3.下列关于构造函数的描述正确的是()",
"selectAnswer": [ "A.构造函数可以声明返回类型", "B.构造函数不可以用private修饰", "C.构造函数必须与类名相同", "D.构造函数不能带参数" ],
"answer": "C"
},
{
"title": "4.装箱、拆箱操作发生在: ()",
"selectAnswer": [ "A.类与对象之间", "B.对象与对象之间", "C.引用类型与值类型之间", "D.引用类型与引用类型之间" ],
"answer": "C"
},
{
"title": "5.下列选项中可以被渲染的纹理是()",
"selectAnswer": [ "A.Texture", "B.Movie Texture", "C.RenderTexture", "D.Texture2D" ],
"answer": "C"
}
]
}
json 形式が正しいかどうかを確認します: https://www.bejson.com/
4. トグル グループ コンポーネントをキャンバスに追加し、[スイッチ オフを許可] プロパティをチェックして 4 つのトグルを選択し、キャンバスを 4 つのトグルのトグル コンポーネントのグループ プロパティにドラッグします。
5. スクリプトをキャンバスにマウントし、対応するオブジェクトをスクリプトのプロパティにドラッグします。
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>();
//记录答题时的临时答案
string ans = null;
//拿题库里面的答案
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 = "A";
else
ans=null;
}
public void Bt(bool isSelect)
{
if (isSelect)
ans = "B";
else
ans = null;
}
public void Ct(bool isSelect)
{
if (isSelect)
ans = "C";
else
ans = null;
}
public void Dt(bool isSelect)
{
if (isSelect)
ans = "D";
else
ans = null;
}
//下一题
public void NextSelect()
{
answers.RemoveAt(currentIndex);
answers.Insert(currentIndex, ans);
currentIndex++;
ans = null;
if (currentIndex < allAnswers.Count)
{
titleText.text = data.selects[currentIndex].title;
for (int i = 0; i < toggles.Length; i++)
{
toggles[i].transform.GetChild(1).GetComponent<Text>().text = data.selects[currentIndex].selectAnswer[i];
}
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 = null;
titleText.text = data.selects[currentIndex].title;
for (int i = 0; i < toggles.Length; i++)
{
toggles[i].transform.GetChild(1).GetComponent<Text>().text = data.selects[currentIndex].selectAnswer[i];
}
ShowAnswer(currentIndex);
}
//显示当前题目是否已经作答
public void ShowAnswer(int index)
{
for (int i = 0; i < toggles.Length; i++)
{
toggles[i].isOn = false;
}
switch (answers[index])
{
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>();
}
6. さまざまなトグルとボタンに対応するメソッドをスクリプトに追加します。
トグル
ボタン
7.Unityを実行するだけです
注: バグがある場合は修正してください
結論: 抱きしめる木は髪の毛の先に生まれ、9 階建てのプラットフォームは土の山から始まり、千里の道も一歩から始まります。