When I build on Android my json files seems to not load because i dont see data. in unity editor play it works fine.
here is my code
public static JsonData LoadFile(string path)
{
var fileContents = File.ReadAllText(path);
var data = JsonMapper.ToObject(fileContents);
return data;
}
public void QuestionLoader()
{
// Load the Json file and store its contents in a 'JsonData' variable
var data = LoadFile(Application.dataPath + "/Resources/json/level1.json");
For save files you can use StreamReader and StreamWriter just make sure you use Application.persistentDataPath.
For more details how to do this you can reference tutorial I wrote about this a while back.
string parseThis = txtAsset.text; causes error - Object not set to instance…
Loading json as Text asset is not working. I read in places that text asset does not work for json. All Im trying to do is replace (Application.dataPath with a path that andriod phones can recognize and load my json file from. Is there a different path for andriod ?
Even this does not work - I got path from Unity Docs but not working. Im struggling with something thats working on PC but not on Android. Just a path difference I guess but its not easy for a beginner !
public static JsonData LoadFile(string path)
{
var www = new WWW(path);
return www.text;
//var fileContents = File.ReadAllText(path);
// var data = JsonMapper.ToObject(fileContents);
// return data;
}
public void QuestionLoader()
{
string path = "";
// Load the Json file and store its contents in a 'JsonData' variable
#if UNITY_ANDROID
path = "jar:file://" + Application.dataPath + "!/assets/Levels/level1.json";
#endif
#if UNITY_EDITOR
path = Application.dataPath + "/StreamingAssets/Levels/level1.json";
#endif
//
var data = LoadFile(path);
Oh sorry, Resources.Load doesn’t need the filename extension (.json) it can figure it out by itself.
Fixed/Edited the example on my post.
If you’d have just taken the time to check the Unity documentation / script-reference for Resources.Load maybe we could’ve avoided your angry private message.
It works fine.
The only problem you you might get with it is that if you have other text assets sharing the name but having different extension. e.g Level_1.json and Level_1.txt or Level_1.xml.