我使用Json/LitJson文件将数据库问题和答案读入我的测验。
它在Unity上工作时没有任何错误,但在我将其构建到APK中后,它在Android设备上无法正常运行,我启动它后测验没有出来。我使用的是MI 3 Android手机,然后我也在BlueStacks上尝试了它。它在两个平台上都不工作(好吧,它们都是Android)。
我不太确定是安卓设备的问题还是我读Json文件的代码问题。下面是我读Json文件的代码。感谢任何帮助。谢谢。
void Start(){//start reading Json file
score = 0;
nextQuestion = true;
jsonString = File.ReadAllText (Application.dataPath + "/quiz.json");
questionData = JsonMapper.ToObject (jsonString);
//StartCoroutine ("Json");
}
public void OnClick(){//get the question and answer from Json after clicking
if (nextQuestion) {
GameObject[] destroyAnswer = GameObject.FindGameObjectsWithTag ("Answer");
if (destroyAnswer != null) {
for (int x=0; x<destroyAnswer.Length; x++) {
DestroyImmediate (destroyAnswer [x]);
}
}
qq.GetComponentInChildren<Text> ().text = questionData ["data"] [questionNumber] ["question"].ToString ();
for (int i=0; i<questionData["data"][questionNumber]["answer"].Count; i++) {
GameObject answer = Instantiate (answerPrefab);
answer.GetComponentInChildren<Text> ().text = questionData ["data"] [questionNumber] ["answer"] [i].ToString ();
Transform answerC = GameObject.Find ("GameObject").GetComponent<Transform> ();
answer.transform.SetParent (answerC);
string x = i.ToString ();
if (i == 0) {
answer.name = "CorrectAnswer";
answer.GetComponent<Button> ().onClick.AddListener (() => Answer ("0"));
} else {
answer.name = "WrongAnswer" + x;
answer.GetComponent<Button> ().onClick.AddListener (() => Answer (x));
}
answer.transform.SetSiblingIndex (Random.Range (0, 3));
}
questionNumber++;
nextQuestion = false;
clickAns = true;
}
}
我的Json文件:
{
"data": [
{
"id": "1",
"question": "Cape Warthog and Dodo come from which country?",
"answer": [
"Africa",
"India",
"Philippines",
"Australia"
]
}, {
"id": "2",
"question": "Dama Gazelle live in what place?",
"answer": [
"Sahara dessert",
"Africa beach",
"Rain forest",
"South pole"
]
}, {
"id": "3",
"question": "Why did the Sabertooth Tiger extinct",
"answer": [
"overhunting",
"Habitat loss",
"Natural polution",
"Diseases"
]
}, {
"id": "4",
"question": "Wolly Mammoth resemblance to which animal?",
"answer": [
"Elephant",
"Tiger",
"Sheep",
"Giraffe"
]
}, {
"id": "5",
"question": "How big Tasmanian Tiger mouth would open?",
"answer": [
"120 degree angle",
"110 degree angle",
"100 degree angle",
"130 degree angle"
]
}, {
"id": "6",
"question": "Greak Auk could not fly because it has ..... ?",
"answer": [
"Tiny wing",
"Heavy body",
"Big head",
"Large feet"
]
}, {
"id": "7",
"question": "PyreneanIbex make their homes at where ? ",
"answer" : [
"Cliffs",
"Caves",
"Forest",
"Waterfall"
]
}, {
"id": "8",
"question": "What are endangered animals?",
"answer": [
"Animals that going to extinct",
"Animals that flys",
"Animals that are dangerous",
"Animals that have 4 legs"
]
}
]
}
2条答案
按热度按时间u5i3ibmn1#
我遇到了同样的问题。我正在练习Unity Live Session中的Quiz Game 2。您需要为Android编写不同的代码。我将我的data.json文件放在Assets文件夹内的StreamingAssets文件夹中。它在Unity Editor中运行良好,但问题是StreamingAssets文件夹在Android上不存在。
在Android上,文件包含在压缩的.jar文件中(基本上与标准zip压缩文件的格式相同)。这意味着如果您不使用Unity的WWW类来检索文件,您需要使用其他软件来查看.jar存档内部并获取文件。
下面是我的代码示例;从开始()调用***LoadGameData()***。
ee7vknir2#
首先将json文件放入Resources文件夹中。创建一个类并将json反序列化为该类。
你可以通过questionData List访问json数据。这里是一个获取随机问题并分配给TextMeshProUGUI文本的示例。