我正在使用Json/LitJson文件来读取数据库问题和答案到我的测验中。Android设备不读取JSON文件,但Unity能够读取它。为什么?
它在Unity上运行时没有任何错误,但是当我将它构建到APK中后,它在Android设备上无法正常运行,在启动它之后,测验并未出现。我正在使用MI3 android手机,然后我在BlueStacks上也尝试过它。它不适用于这两个平台(以及他们都是Android)。
我不是很确定这是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"
]
}
]
}
JSON文件的外观如何? QuestionData对象的结构是什么? – Egor
我已编辑到我的问题。谢谢 – Kurogane
你有没有在logcat中得到任何反馈? – Pooya