Android设备不读取JSON文件,但Unity能够读取它。为什么?

时间:2016-04-16 07:50:39

标签: android json unity5

我正在使用Json / LitJson文件来阅读数据库问题并回答我的测验。

它在Unity上没有任何错误,但是在我将它构建到APK之后,它在Android设备上运行不正常,我启动后测验没有出现。我使用MI3安卓手机,然后我也在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"
    ]
}
]
}

0 个答案:

没有答案