I have made my levels for my game in json format. I have a class to load the json file and convert the json to a Serializable object which I in turn use to create a GameObject. My levels are under /Assets/Resources/Levels/ with a file name like *Level01-01.json". I’ve tried using both Application.dataPath and Application.persistentDataPath.
When I execute this on pc it works fine. When I try it on Android it can’t find the file.
Should I put my json files under a different directory? Should I build my FilePath in another way? Is the technique for reading the file wrong?
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class LevelDataController : MonoBehaviour {
private string gameDataProjectFilePath = "/Resources/Levels/Level";
public Level level;
public string levelpacknr;
public string levelnr;
private string FilePath() {
return Application.dataPath + gameDataProjectFilePath + levelpacknr + "-" + levelnr + ".json";
}
public void LoadGameData() {
string filePath = FilePath();
if (File.Exists(filePath)) {
Debug.Log("file at " + filePath + " exists");
string dataAsJson = File.ReadAllText(filePath);
level = JsonUtility.FromJson<Level>(dataAsJson);
} else {
Debug.Log("file at " + filePath + " does not exist");
}
}
}