我的问题是没有在Android设备中加载/读取JSON文件,只能在Unity编辑器中。我在许多网站上尝试了很多解决方案,例如this,this和this,但没有任何结果。
所以任何人都可以帮助我。 这是我的代码:
private void LoadGameData()
{
// Path.Combine combines strings into a file path
// Application.StreamingAssets points to Assets/StreamingAssets in the Editor, and the StreamingAssets folder in a build
string filePath = Path.Combine(Application.streamingAssetsPath, "Data.json");
if (File.Exists(filePath))
{
// Read the json from the file into a string
string dataAsJson = File.ReadAllText(filePath);
// Pass the json to JsonUtility, and tell it to create a GameData object from it
GameData loadedData = JsonUtility.FromJson<GameData>(dataAsJson);
// Retrieve the allRoundData property of loadedData
Questions = loadedData.Questions ;
}
else
{
Debug.LogError("Cannot load game data!");
}
}
我尝试这些代码,但没有任何反应: 第一个代码:
void Start()
{
string filePath = "jar:file://" + Application.dataPath + "!/assets/SahAwKhataa.json";
StartCoroutine(GetDataInAndroid(filePath));
}
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);
Questions = loadedData.Questions ;
}
else
{
Debug.LogError("Can not load game data!");
}
}
第二个:
public void ReadJson()
{
string path = Application.streamingAssetsPath + "/Data.json";
#if UNITY_EDITOR
//string path = "/StreamingAssets/Data.json";
#endif
WWW www = new WWW(path);
while (!www.isDone) { }
string json = www.text;
GameData loadedData = JsonUtility.FromJson<GameData>(json);
Questions = loadedData.Questions;
}
GameData脚本代码:
[System.Serializable]
public class GameData
{
public GameQuestions[] Questions;
}
GameManager脚本代码:
public class GameManager : MonoBehaviour {
private GameQuestions[] Questions;
private static List<GameQuestions> LevelQuestions ;
private string gameDataFileName = "Data.json";
private GameQuestions CurrentQuest;
void Start()
{
TestText.text = "Text1";
StartCoroutine(LoadStreamingAsset(gameDataFileName));
TestText.text = "Text2";
if (LevelQuestions == null || LevelQuestions.Count == 0)
{
TestText.text = "Text3";
LevelQuestions = Questions.ToList<GameQuestions>();
}
TestText.text = "Text4";
int QuestNumber = Random.Range(0, LevelQuestions.Count);
CurrentQuest= LevelQuestions [QuestNumber ];
QuestText.text = CurrentQuest.Quest;
}
IEnumerator LoadStreamingAsset(string fileName)
{
string result;
string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, fileName);
if (filePath.Contains("://") || filePath.Contains(":///"))
{
WWW www = new WWW(filePath);
yield return www;
result = www.text;
}
else
{
result = File.ReadAllText(filePath);
}
GameData loadedData = JsonUtility.FromJson<GameData>(result);
Questions= loadedData.Questions;
TestText_0.text = loadedData.Questions[0].quest;
}
GameQuestions脚本代码:
[System.Serializable]
public class GameQuestions
{
public string quest;
public bool isCorrect;
}
Data.json:
{"Questions":[{"quest":"Question 1","isCorrect":true},{"quest":"Question 2","isCorrect":false},{"quest":"Question 3","isCorrect":true}]}