- Home /
About Json Datapath.
Hello Everyone.
I have This function. But the path is working (of cource) in only editor. How can i edit it for both editor and Build. And how can i check if there is the save file in that folder. Thank u so much in advance.
[System.Serializable]
public class SaveData
{
public int Shooting = ShootingManager.ShootingCounter;
}
public void Save()
{
SaveData data = new SaveData();
string json = JsonUtility.ToJson(data);
File.WriteAllText(Application.dataPath + "/save.txt", json);
}
public void Load()
{
if (File.Exists(Application.dataPath + "/save.txt"))
{
string savestring = File.ReadAllText(Application.dataPath + "/save.txt");
SaveData data = JsonUtility.FromJson<SaveData>(savestring);
ShootingManager.ShootingCounter = data.Shooting;
}
}
Answer by Llama_w_2Ls · May 20, 2021 at 09:39 AM
Change Application.dataPath
to Application.persistentDataPath
. Application.persistentDataPath Documentation
A persistent data path is a path that can always be written and read to/from, on most platforms. Since the OS of Windows and Android are different, you can't use the same datapath on both platforms. They don't exist on both platforms.
However, a persistent data path is like a universal data path (in a sense), since it can always be located on any OS, by Unity. @Ercova
Thank You so much for the answer but it was my bad i forgot to refence an object to the inspector and i thought it is about the persistentdatapath is not working all the time.. I've changed it to Application.persistentDataPath But at this time; When i quit the game the save data will be deleted but when i restart the game data will be there? what could be the problem :S @Llama_w_2Ls
I don't understand your new issue.
When i quit the game the save data will be deleted
Does this mean that the data can't be seen in the editor? If the data is there when the game is loaded, then I don't see the problem.