My problem is that the JSON file is not loaded/read in android device, only in unity editor. I try many solutions from many sites, like this, this, and this, but nothing happen.
So anyone can help me. This is my code:
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!");
}
}
I try these codes, but nothing happens: First code:
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!");
}
}
The second:
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 script code:
[System.Serializable]
public class GameData
{
public GameQuestions[] Questions;
}
GameManager script code:
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 script code:
[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}]}