Помогите с созданием json
Добавлено: 05 май 2020, 13:00
Добрый день, я начинающий разработчик игр и столкнулся с проблемой. При создании файла json на пк все отображается и создается.Но после компиляции апк и запуске на андроиде не создается файл. Я предполагаю что что-то связано с директорией path = Path.Combine(Application.persistentDataPath + "Save.json");
Вот код создания json
Вот код создания json
Синтаксис:
Используется csharp
using UnityEngine;
using System.IO;
using System;
public class SaveGame : MonoBehaviour
{
public GameObject settings;
public string path;
private SaveData sv = new SaveData();
private void Start()
{
settings = Resources.Load("Money") as GameObject;
path = Path.Combine(Application.persistentDataPath + "Save.json");
if (File.Exists(path))
{
sv = JsonUtility.FromJson<SaveData>(File.ReadAllText(path));
}
}
private void Update()
{
sv.Money_ammount = settings.GetComponent<Money>().Money_ammount;
sv.RadiotionScore = settings.GetComponent<Resource>().RadiotionScore;
sv.WaterScore = settings.GetComponent<Resource>().WaterScore;
sv.EatScore = settings.GetComponent<Resource>().EatScore;
sv.MedicineScore = settings.GetComponent<Resource>().MedicineScore;
sv.Weigth = settings.GetComponent<Add_drop>().Weigth;
sv.DamageScore = settings.GetComponent<Damage>().DamageScore;
sv.Location_now = settings.GetComponent<map>().Location_now.text;
}
private void OnApplicationQuit()
{
File.WriteAllText(path, JsonUtility.ToJson(sv));
settings.GetComponent<Money>().Money_ammount = sv.Money_ammount;
settings.GetComponent<Resource>().RadiotionScore = sv.RadiotionScore;
settings.GetComponent<Resource>().WaterScore = sv.WaterScore;
settings.GetComponent<Resource>().EatScore = sv.EatScore;
settings.GetComponent<Resource>().MedicineScore = sv.MedicineScore;
settings.GetComponent<Add_drop>().Weigth = sv.Weigth;
settings.GetComponent<Damage>().DamageScore = sv.DamageScore;
settings.GetComponent<map>().Location_now.text = sv.Location_now;
}
}
[Serializable]
public class SaveData
{
public int Money_ammount;
public float RadiotionScore;
public float WaterScore;
public float EatScore;
public float MedicineScore;
public float Weigth;
public int DamageScore;
public string Location_now;
}
using System.IO;
using System;
public class SaveGame : MonoBehaviour
{
public GameObject settings;
public string path;
private SaveData sv = new SaveData();
private void Start()
{
settings = Resources.Load("Money") as GameObject;
path = Path.Combine(Application.persistentDataPath + "Save.json");
if (File.Exists(path))
{
sv = JsonUtility.FromJson<SaveData>(File.ReadAllText(path));
}
}
private void Update()
{
sv.Money_ammount = settings.GetComponent<Money>().Money_ammount;
sv.RadiotionScore = settings.GetComponent<Resource>().RadiotionScore;
sv.WaterScore = settings.GetComponent<Resource>().WaterScore;
sv.EatScore = settings.GetComponent<Resource>().EatScore;
sv.MedicineScore = settings.GetComponent<Resource>().MedicineScore;
sv.Weigth = settings.GetComponent<Add_drop>().Weigth;
sv.DamageScore = settings.GetComponent<Damage>().DamageScore;
sv.Location_now = settings.GetComponent<map>().Location_now.text;
}
private void OnApplicationQuit()
{
File.WriteAllText(path, JsonUtility.ToJson(sv));
settings.GetComponent<Money>().Money_ammount = sv.Money_ammount;
settings.GetComponent<Resource>().RadiotionScore = sv.RadiotionScore;
settings.GetComponent<Resource>().WaterScore = sv.WaterScore;
settings.GetComponent<Resource>().EatScore = sv.EatScore;
settings.GetComponent<Resource>().MedicineScore = sv.MedicineScore;
settings.GetComponent<Add_drop>().Weigth = sv.Weigth;
settings.GetComponent<Damage>().DamageScore = sv.DamageScore;
settings.GetComponent<map>().Location_now.text = sv.Location_now;
}
}
[Serializable]
public class SaveData
{
public int Money_ammount;
public float RadiotionScore;
public float WaterScore;
public float EatScore;
public float MedicineScore;
public float Weigth;
public int DamageScore;
public string Location_now;
}