'Json file was not found in Android
I am developing a game for mobile and can't create a normal save system because when playing on the phone the error is logged: Could not find the file. I use the persistent data path as it was recommended in many answers and this method works well in Editor and Windows but not in Android. Is there something I can do about it? Additional info: I am using IL2CPP as backend. Heard that it might create problem with serialization. Full code:
using System;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class JsonFileWriter : MonoBehaviour
{
public string path;
public JsonData data;
string jsonDataString;
void Awake(){
path = Path.Combine(Application.persistentDataPath, "savegames", "data.json");
FileInfo file = new FileInfo(path);
if(!Directory.Exists(path)){
file.Directory.Create();
}
CheckFileExistance(path);
DeserializeData();
SerializeData();
}
public void SerializeData()
{
CheckFileExistance(path);
jsonDataString = JsonUtility.ToJson(data, true);
File.WriteAllText(path, jsonDataString);
Debug.Log("JSon data "+ jsonDataString);
}
public void DeserializeData()
{
string loadedJsonDataString = File.ReadAllText(path);
data = JsonUtility.FromJson<JsonData>(loadedJsonDataString);
Debug.Log("health: " + data.healthBoostAmt.ToString() + "secondChar: " + data.secondCharacterUnlocked.ToString());
}
public void WriteData(int hp,int money,int score,int damage, bool secondChar,int moneyAmt,int charSelected){
data = new JsonData(hp,money,score,damage,secondChar,moneyAmt,charSelected);
SerializeData();
}
#region Getters and seters
public int GetHealthBoosterAmt(){
DeserializeData();
return data.healthBoostAmt;
}
public int GetMoneyBoosterAmt(){
DeserializeData();
return data.moneyBoostAmt;
}
public int GetScoreBoostAmt(){
DeserializeData();
return data.scoreBoostAmt;
}
public int GetDamageBoostAmt(){
DeserializeData();
return data.damageBoostAmt;
}
public bool GetSecondCharBought(){
DeserializeData();
return data.secondCharacterUnlocked;
}
public int GetMoneyAmt(){
DeserializeData();
return data.moneyAmt;
}
public int GetSelectedCharID(){
DeserializeData();
return data.charSelected;
}
public string GetJsonDataString(){
return jsonDataString;
}
public void SetJsonDataString(string newValue){
//int hp,int money,int score,int damage, bool secondChar
data = JsonUtility.FromJson<JsonData>(newValue);
WriteData(data.healthBoostAmt,data.moneyBoostAmt,data.scoreBoostAmt,data.damageBoostAmt,data.secondCharacterUnlocked,data.moneyAmt,data.charSelected);
}
#endregion
void CheckFileExistance(string filePath){
if (!File.Exists(filePath)){
File.Create(filePath).Close();
}
}
}
[Serializable]
public class JsonData
{
public int healthBoostAmt;
public int moneyBoostAmt;
public int scoreBoostAmt;
public int damageBoostAmt;
public bool secondCharacterUnlocked;
public int moneyAmt;
public int charSelected;
public JsonData (int healthBoostAmt,int moneyBoostAmt,int scoreBoostAmt,int damageBoostAmt,bool secondCharacterUnlocked,int moneyAmt,int charSelected)
{
this.healthBoostAmt = healthBoostAmt;
this.moneyBoostAmt = moneyBoostAmt;
this.scoreBoostAmt = scoreBoostAmt;
this.damageBoostAmt = damageBoostAmt;
this.secondCharacterUnlocked = secondCharacterUnlocked;
this.moneyAmt = moneyAmt;
this.charSelected = charSelected;
}
public JsonData (){}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|