2016-04-16 199 views
1

我正在使用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" 
    ] 
} 
] 
} 
+0

JSON文件的外观如何? QuestionData对象的结构是什么? – Egor

+0

我已编辑到我的问题。谢谢 – Kurogane

+0

你有没有在logcat中得到任何反馈? – Pooya

回答

0

我得到了同样的问题。我正在练习Unity Live Session的Quiz Game 2。 Android需要不同的代码。我把data.json文件放在StreamingAssets文件夹里面Assets文件夹中。它在Unity编辑器中工作正常,但问题在于Android上不存在StreamingAssets文件夹。

在Android上,这些文件包含在压缩的.jar文件中(与标准zip压缩文件的格式基本相同)。这意味着如果您不使用Unity的WWW类来检索文件,则需要使用其他软件来查看.jar​​存档文件并获取文件。

这里我的代码示例;从Start()调用LoadGameData()

private void LoadGameData() 
{ 
    string filePath = ""; 

    #if UNITY_EDITOR 
    filePath = Application.dataPath + "/StreamingAssets/data.json"; 
    if (File.Exists (filePath)) { 

     string dataAsJson = File.ReadAllText (filePath); 
     GameData loadedData = JsonUtility.FromJson<GameData> (dataAsJson); 

     allRoundData = loadedData.allRoundData; 
    } else { 
     Debug.LogError ("Can not load game data!"); 
    } 
    #elif UNITY_ANDROID 
    filePath = "jar:file://" + Application.dataPath + "!/assets/data.json"; 
    StartCoroutine(GetDataInAndroid(filePath)); 
    #endif 
} 

IEnumerator GetDataInAndroid(string url) 
{ 
    WWW www = new WWW(url); 
    yield return www; 
    if (www.text != null) { 

     string dataAsJson = www.text; 
     GameData loadedData = JsonUtility.FromJson<GameData> (dataAsJson); 

     allRoundData = loadedData.allRoundData; 
    } else { 
     Debug.LogError ("Can not load game data!"); 
    } 
} 
相关问题