Location for files in order to be able to load them on Android
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");
}
}
}
Answer by Hellium · Jan 18, 2019 at 07:49 PM
JSON files can be handled as TextAsset
in Unity, making things muuuch easier. The only thing you have to do is to move the JSON file from the Resources
folder to another folder under Assets
such as DataFiles
for instance.
public class LevelDataController : MonoBehaviour
{
// Drag & drop the file located in `Assets/DataFiles/Levels`
// into the following field of the inspector
[SerializeField]
private TextAsset gameData = null;
public Level level;
public string levelpacknr;
public string levelnr;
public void LoadGameData()
{
string filePath = FilePath();
if ( gameData != null )
{
string dataAsJson = gameData.text ;
level = JsonUtility.FromJson<Level>(dataAsJson);
} else
{
Debug.LogError("You forgot to drag & drop the JSON file into the inspector");
}
}
}
I have every level in a different file. Should I join them in one json file or van I have an array of TextAsset's? Your solution does seem easier not having to worry about which path to use.
Thanks for the reply already, gonna try this out when I get access to my pc :)
I believe having an array of TextAsset
is the way to go !
I worked it out like you suggested with TextAsset + drag and drop. Now I can use it on my pc and on Android. Thank you so much!
Answer by xxmariofer · Jan 18, 2019 at 04:17 PM
you need to use WWW for accessing content in android since is inside jar, also see documentation for deciding if you should use persistentdatapath or streaming assets. check https://answers.unity.com/questions/1231211/how-to-get-resources-filepath-on-android-device.html
And can I keep my json files under Assets/Resources/Levels?
I cant test it now and to be honest, i have only used WWW for retrieving info from inside the jar but the info was downloaded at runtime so i am not sure if you have to place it at one exact folder at build, i just downloaded a zip in a random folder inside persistentdatapath directory and could access it.
Thanks for the reply, I'll test it next time I have access to my pc :)
Your answer
Follow this Question
Related Questions
Cannot write data into JSON file on Android's external memory 1 Answer
can you read json file on android ? I have a thesis and it's not going well. 1 Answer
File.ReadAllBytes() returns byte[0] 0 Answers
Help Writing and reading files on android. 1 Answer
Ressource.Load returns null on "build" but work on "build and run" 2 Answers