\$\begingroup\$
\$\endgroup\$
5
I'm making a game in Unity and I'm using json format to save. I have no problem recording it on my computer, but it does not record the animals I catch on my Android phone. How can I solve this? What is the reason?
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class KayitHayvanSlot : MonoBehaviour
{
public GameObject HayvanPaneliGO;
public GameObject Player;
HayvanPaneliKod HayvanPaneliKod;
public HayvanDatas hayvanDatas;
//------------------------
private void Awake()
{
HayvanYukle();
}
void Start()
{
HayvanPaneliKod = HayvanPaneliGO.GetComponent<HayvanPaneliKod>();
Debug.Log(Application.persistentDataPath + "/hayvanlarSave.json");
if (File.Exists(Application.persistentDataPath + "/hayvanlarSave.json"))
{
HayvanTakas2();
}
Debug.Log("Bu script " + gameObject.name + " objesine bağlıdır.");
}
private void HayvanYukle()
{
if (File.Exists(Application.persistentDataPath + "/hayvanlarSave.json"))
{
JsonYukle();
}
}
public void JsonKaydet()
{
string jsonString = JsonUtility.ToJson(hayvanDatas, true);
Debug.Log(jsonString);
File.WriteAllText(Application.persistentDataPath + "/hayvanlarSave.json", jsonString);
}
public void JsonYukle()
{
string jsonOku = File.ReadAllText(Application.persistentDataPath + "/hayvanlarSave.json");
hayvanDatas = JsonUtility.FromJson<HayvanDatas>(jsonOku);
}
//------------------------------------------------------
public void HayvanTakas()
{
for (int i = 0; HayvanPaneliKod.hayvanlar.Count > i; i++)
{
if (HayvanPaneliKod.hayvanlar[i] != null)
{
hayvanDatas.dataHayvans[i].hayvanismi = HayvanPaneliKod.hayvanlar[i].hayvanismi;
hayvanDatas.dataHayvans[i].hayvanbilgi = HayvanPaneliKod.hayvanlar[i].hayvanbilgi;
hayvanDatas.dataHayvans[i].hayvanid = HayvanPaneliKod.hayvanlar[i].hayvanid;
hayvanDatas.dataHayvans[i].hayvanatak = HayvanPaneliKod.hayvanlar[i].hayvanatak;
hayvanDatas.dataHayvans[i].hayvandefans = HayvanPaneliKod.hayvanlar[i].hayvandefans;
hayvanDatas.dataHayvans[i].hayvancan = HayvanPaneliKod.hayvanlar[i].hayvancan;
hayvanDatas.dataHayvans[i].hayvancandolu = HayvanPaneliKod.hayvanlar[i].hayvancandolu;
hayvanDatas.dataHayvans[i].kazanılanTecrübe = HayvanPaneliKod.hayvanlar[i].kazanılanTecrübe;
hayvanDatas.dataHayvans[i].hayvanlevel = HayvanPaneliKod.hayvanlar[i].hayvanlevel;
hayvanDatas.dataHayvans[i].hayvantipi = HayvanPaneliKod.hayvanlar[i].hayvantipi;
hayvanDatas.dataHayvans[i].hayvanYakalandiTF = HayvanPaneliKod.hayvanlar[i].hayvanYakalandiTF;
}
}
}
public void HayvanTakas2()
{
for (int i = 0; 70 > i; i++)
{
if (hayvanDatas.dataHayvans[i].hayvanid != 0)
{
Hayvan yenihayvan = new Hayvan(hayvanDatas.dataHayvans[i].hayvanismi, hayvanDatas.dataHayvans[i].hayvanbilgi,
hayvanDatas.dataHayvans[i].hayvanid, hayvanDatas.dataHayvans[i].hayvanatak,
hayvanDatas.dataHayvans[i].hayvandefans, hayvanDatas.dataHayvans[i].hayvancan,
hayvanDatas.dataHayvans[i].hayvancandolu, hayvanDatas.dataHayvans[i].kazanılanTecrübe,
hayvanDatas.dataHayvans[i].hayvanlevel, hayvanDatas.dataHayvans[i].hayvantipi,
hayvanDatas.dataHayvans[i].hayvanYakalandiTF);
HayvanPaneliKod.SeciliSlotaHayvanEkle(yenihayvan, i);
}
}
}
}
[System.Serializable]
public class HayvanData
{
public string hayvanismi;
public string hayvanbilgi;
public int hayvanid;
public float hayvanatak;
public float hayvandefans;
public float hayvancan;
public float hayvancandolu;
public int kazanılanTecrübe;
public int hayvanlevel;
public Hayvan.HayvanType hayvantipi;
public bool hayvanYakalandiTF;
public HayvanData()
{
}
public HayvanData(string hayvanismi, string hayvanbilgi, int hayvanid, float hayvanatak, float hayvandefans, float hayvancan,
float hayvancandolu, int kazanılanTecrübe, int hayvanlevel, Hayvan.HayvanType hayvantipi, bool hayvanYakalandiTF)
{
this.hayvanismi = hayvanismi;
this.hayvanbilgi = hayvanbilgi;
this.hayvanid = hayvanid;
this.hayvanatak = hayvanatak;
this.hayvandefans = hayvandefans;
this.hayvancan = hayvancan;
this.hayvancandolu = hayvancandolu;
this.kazanılanTecrübe = kazanılanTecrübe;
this.hayvanlevel = hayvanlevel;
this.hayvantipi = hayvantipi;
this.hayvanYakalandiTF = hayvanYakalandiTF;
}
}
[System.Serializable]
public class HayvanDatas
{
public List<HayvanData> dataHayvans = new List<HayvanData>();
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TopluKayit : MonoBehaviour
{
public GameObject TarlaEkimArayuzGO1;
public GameObject TarlaEkimArayuzGO2;
public GameObject TarlaEkimArayuzGO3;
public GameObject KayitEnvanterSlotGO;
public GameObject KaydetmePanelEHGO;
public GameObject BitkiToplaArayuzGO;
public GameObject EnvanterGO;
public List<GameObject> MaviTotemGOs;
public List<GameObject> SariTotemGOs;
public List<GameObject> FareTotemGOs;
public GameObject YarasaGO;
KayitYetenekSlot KayitYetenekSlot;
KayitYetenekRuhAtak KayitYetenekRuhAtak;
KayitYetenekLevel KayitYetenekLevel;
KayitKarakter KayitKarakter;
KayitHayvanSlot KayitHayvanSlot;
DevHayvanAdamKayit DevHayvanAdamKayit;
GorevKayit GorevKayit;
DogaKayit DogaKayit;
BitkiKayit BitkiKayit;
KayitEnvanterSlot KayitEnvanterSlot;
List<Totemler> MaviTotems;
List<Totemler> SariTotems;
List<Totemler> FareTotems;
Totemler YarasaTotem;
Envanter Envanter;
TarlaEkim TarlaEkimArayuz1;
TarlaEkim TarlaEkimArayuz2;
TarlaEkim TarlaEkimArayuz3;
private void Start()
{
KayitYetenekSlot = KayitEnvanterSlotGO.GetComponent<KayitYetenekSlot>();
KayitYetenekRuhAtak = KayitEnvanterSlotGO.GetComponent<KayitYetenekRuhAtak>();
KayitYetenekLevel = KayitEnvanterSlotGO.GetComponent<KayitYetenekLevel>();
KayitKarakter = KayitEnvanterSlotGO.GetComponent<KayitKarakter>();
KayitHayvanSlot = KayitEnvanterSlotGO.GetComponent<KayitHayvanSlot>();
DevHayvanAdamKayit = KayitEnvanterSlotGO.GetComponent<DevHayvanAdamKayit>();
GorevKayit = KayitEnvanterSlotGO.GetComponent<GorevKayit>();
DogaKayit = BitkiToplaArayuzGO.GetComponent<DogaKayit>();
BitkiKayit = KayitEnvanterSlotGO.GetComponent<BitkiKayit>();
KayitEnvanterSlot = KayitEnvanterSlotGO.GetComponent<KayitEnvanterSlot>();
Envanter = EnvanterGO.GetComponent<Envanter>();
for (int i = 0; MaviTotemGOs.Count > i; i++)
{
MaviTotems = new List<Totemler>();
}
for (int i = 0; SariTotemGOs.Count > i; i++)
{
SariTotems = new List<Totemler>();
}
for (int i = 0; FareTotemGOs.Count > i; i++)
{
FareTotems = new List<Totemler>();
}
YarasaTotem = new Totemler();
for (int i = 0; MaviTotemGOs.Count > i; i++)
{
MaviTotems.Add(MaviTotemGOs[i].GetComponent<Totemler>());
}
for (int i = 0; SariTotemGOs.Count > i; i++)
{
SariTotems.Add(SariTotemGOs[i].GetComponent<Totemler>());
}
for (int i = 0; FareTotemGOs.Count > i; i++)
{
FareTotems.Add(FareTotemGOs[i].GetComponent<Totemler>());
}
YarasaTotem = YarasaGO.GetComponent<Totemler>();
TarlaEkimArayuz1 = TarlaEkimArayuzGO1.GetComponent<TarlaEkim>();
TarlaEkimArayuz2 = TarlaEkimArayuzGO2.GetComponent<TarlaEkim>();
TarlaEkimArayuz3 = TarlaEkimArayuzGO3.GetComponent<TarlaEkim>();
}
public void DataTakaslar()
{
KaydetmePanelEHGO.SetActive(true);
KayitYetenekSlot.YetenekTakas();
KayitYetenekRuhAtak.Takas1();
KayitYetenekLevel.Takas1();
KayitKarakter.TepeHayvani();
//StartCoroutine(KayitKarakter.TotemKaydetme1Cor());
KayitKarakter.ItemTakas();
KayitKarakter.BagKurulanHayvanTakas1();
KayitKarakter.YetenekKisitlamaTakas1();
KayitKarakter.HangiSehirde();
KayitKarakter.HangiSehirde();
KayitKarakter.TepeHayvani();
KayitEnvanterSlot.ItemTakas();
GorevKayit.DataTakas();
GorevKayit.DataTakas3();
GorevKayit.DataTakas5();
GorevKayit.DataTakas7();
DogaKayit.TekBitkiZamanCor();
DogaKayit.BitkiSayisiOrtaCor();
DogaKayit.BitkiSayisiCokCor();
BitkiKayit.DataTakas();
KayitHayvanSlot.HayvanTakas();
DevHayvanAdamKayit.DataTakas();
for (int i = 0; MaviTotemGOs.Count > i; i++)
{
MaviTotems[i].ItemTakas();
}
for (int i = 0; SariTotemGOs.Count > i; i++)
{
SariTotems[i].ItemTakas();
}
for (int i = 0; FareTotemGOs.Count > i; i++)
{
FareTotems[i].ItemTakas();
}
YarasaTotem.ItemTakas();
Envanter.ItemTakas();
TarlaEkimArayuz1.BitkiSayisiCor();
TarlaEkimArayuz1.BitkiSayisi2Cor();
TarlaEkimArayuz1.TekBitkiZamanCor();
TarlaEkimArayuz2.BitkiSayisiCor();
TarlaEkimArayuz2.BitkiSayisi2Cor();
TarlaEkimArayuz2.TekBitkiZamanCor();
TarlaEkimArayuz3.BitkiSayisiCor();
TarlaEkimArayuz3.BitkiSayisi2Cor();
TarlaEkimArayuz3.TekBitkiZamanCor();
}
public void Kaydetmeler()
{
KayitEnvanterSlot.JsonKaydet();
KayitYetenekSlot.JsonKaydet();
KayitYetenekRuhAtak.JsonKaydet();
KayitYetenekLevel.JsonKaydet();
KayitKarakter.JsonKaydet();
GorevKayit.JsonKaydet();
DogaKayit.JsonKaydet();
BitkiKayit.JsonKaydet();
KayitHayvanSlot.JsonKaydet();
DevHayvanAdamKayit.JsonKaydet();
for (int i = 0; MaviTotemGOs.Count > i; i++)
{
MaviTotems[i].JsonKaydet();
}
for (int i = 0; SariTotemGOs.Count > i; i++)
{
SariTotems[i].JsonKaydet();
}
for (int i = 0; FareTotemGOs.Count > i; i++)
{
FareTotems[i].JsonKaydet();
}
YarasaTotem.JsonKaydet();
Envanter.JsonKaydet();
TarlaEkimArayuz1.JsonKaydet();
TarlaEkimArayuz2.JsonKaydet();
TarlaEkimArayuz3.JsonKaydet();
KaydetmeKapat();
}
public void KaydetmeKapat()
{
KaydetmePanelEHGO.SetActive(false);
}
}
Below is the working code I wrote.
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Collections;
public class KayitHayvanSlot : MonoBehaviour
{
public GameObject HayvanPaneliGO;
public GameObject Player;
HayvanPaneliKod HayvanPaneliKod;
public HayvanDatas hayvanDatas;
//------------------------
private void Awake()
{
HayvanYukle();
}
IEnumerator DataCekme()
{
yield return new WaitUntil(() => File.Exists(Path.Combine(Application.persistentDataPath, "hayvanlarSave.json")));
HayvanTakas2();
}
void Start()
{
HayvanPaneliKod = HayvanPaneliGO.GetComponent<HayvanPaneliKod>();
Debug.Log(Path.Combine(Application.persistentDataPath, "hayvanlarSave.json"));
StartCoroutine(DataCekme());
Debug.Log("Bu script " + gameObject.name + " objesine bağlıdır.");
}
private void HayvanYukle()
{
if (File.Exists(Path.Combine(Application.persistentDataPath, "hayvanlarSave.json")))
{
JsonYukle();
}
}
public void JsonKaydet()
{
string jsonString = JsonUtility.ToJson(hayvanDatas, true);
Debug.Log(jsonString);
File.WriteAllText(Path.Combine(Application.persistentDataPath, "hayvanlarSave.json"), jsonString);
}
public void JsonYukle()
{
string jsonOku = File.ReadAllText(Path.Combine(Application.persistentDataPath, "hayvanlarSave.json"));
hayvanDatas = JsonUtility.FromJson<HayvanDatas>(jsonOku);
}
//------------------------------------------------------
public void HayvanTakas()
{
for (int i = 0; HayvanPaneliKod.hayvanlar.Count > i; i++)
{
if (HayvanPaneliKod.hayvanlar[i] != null)
{
hayvanDatas.dataHayvans[i].hayvanismi = HayvanPaneliKod.hayvanlar[i].hayvanismi;
hayvanDatas.dataHayvans[i].hayvanbilgi = HayvanPaneliKod.hayvanlar[i].hayvanbilgi;
hayvanDatas.dataHayvans[i].hayvanid = HayvanPaneliKod.hayvanlar[i].hayvanid;
hayvanDatas.dataHayvans[i].hayvanatak = HayvanPaneliKod.hayvanlar[i].hayvanatak;
hayvanDatas.dataHayvans[i].hayvandefans = HayvanPaneliKod.hayvanlar[i].hayvandefans;
hayvanDatas.dataHayvans[i].hayvancan = HayvanPaneliKod.hayvanlar[i].hayvancan;
hayvanDatas.dataHayvans[i].hayvancandolu = HayvanPaneliKod.hayvanlar[i].hayvancandolu;
hayvanDatas.dataHayvans[i].kazanılanTecrübe = HayvanPaneliKod.hayvanlar[i].kazanılanTecrübe;
hayvanDatas.dataHayvans[i].hayvanlevel = HayvanPaneliKod.hayvanlar[i].hayvanlevel;
hayvanDatas.dataHayvans[i].hayvantipi = HayvanPaneliKod.hayvanlar[i].hayvantipi;
hayvanDatas.dataHayvans[i].hayvanYakalandiTF = HayvanPaneliKod.hayvanlar[i].hayvanYakalandiTF;
}
}
}
public void HayvanTakas2()
{
for (int i = 0; 70 > i; i++)
{
if (hayvanDatas.dataHayvans[i].hayvanid != 0)
{
Hayvan yenihayvan = new Hayvan(hayvanDatas.dataHayvans[i].hayvanismi, hayvanDatas.dataHayvans[i].hayvanbilgi,
hayvanDatas.dataHayvans[i].hayvanid, hayvanDatas.dataHayvans[i].hayvanatak,
hayvanDatas.dataHayvans[i].hayvandefans, hayvanDatas.dataHayvans[i].hayvancan,
hayvanDatas.dataHayvans[i].hayvancandolu, hayvanDatas.dataHayvans[i].kazanılanTecrübe,
hayvanDatas.dataHayvans[i].hayvanlevel, hayvanDatas.dataHayvans[i].hayvantipi,
hayvanDatas.dataHayvans[i].hayvanYakalandiTF);
HayvanPaneliKod.SeciliSlotaHayvanEkle(yenihayvan, i);
}
}
}
}
//-------------------------------------------------
[System.Serializable]
public class HayvanData
{
public string hayvanismi;
public string hayvanbilgi;
public int hayvanid;
public float hayvanatak;
public float hayvandefans;
public float hayvancan;
public float hayvancandolu;
public int kazanılanTecrübe;
public int hayvanlevel;
public Hayvan.HayvanType hayvantipi;
public bool hayvanYakalandiTF;
public HayvanData()
{
}
public HayvanData(string hayvanismi, string hayvanbilgi, int hayvanid, float hayvanatak, float hayvandefans, float hayvancan,
float hayvancandolu, int kazanılanTecrübe, int hayvanlevel, Hayvan.HayvanType hayvantipi, bool hayvanYakalandiTF)
{
this.hayvanismi = hayvanismi;
this.hayvanbilgi = hayvanbilgi;
this.hayvanid = hayvanid;
this.hayvanatak = hayvanatak;
this.hayvandefans = hayvandefans;
this.hayvancan = hayvancan;
this.hayvancandolu = hayvancandolu;
this.kazanılanTecrübe = kazanılanTecrübe;
this.hayvanlevel = hayvanlevel;
this.hayvantipi = hayvantipi;
this.hayvanYakalandiTF = hayvanYakalandiTF;
}
}
[System.Serializable]
public class HayvanDatas
{
public List<HayvanData> dataHayvans = new List<HayvanData>();
}'''
Volkan AKDAG
-
\$\begingroup\$ I think it looks like a permissions issue. But what confuses me is that most of my registration codes work, the code I made to catch animals does not work on mobile. \$\endgroup\$Volkan AKDAG– Volkan AKDAG06/26/2024 13:54:30Commented Jun 26, 2024 at 13:54
-
\$\begingroup\$ I wrote two code scripts, with one code script I get the values of the animals I catch and with the other I save them. There are also values I got from different scripts where I saved them. Here's how I do the recording. I press a key and get the values from DataTakaslar(). When I press the Yes button, Kaydetmeler() works and everything is saved. \$\endgroup\$Volkan AKDAG– Volkan AKDAG06/29/2024 02:02:26Commented Jun 29, 2024 at 2:02
-
\$\begingroup\$ I solved my problem, not only on Android but also on PC, it was not saving when compiled. So, I put a waiting condition on the data I pulled at start with a coroutine so that the data would not be pulled until the file was created, and the code I wrote worked. The code I sent you is a working version. Thank you very much for your answers so far. \$\endgroup\$Volkan AKDAG– Volkan AKDAG07/06/2024 09:32:22Commented Jul 6, 2024 at 9:32
-
\$\begingroup\$ Jave you tried saving on a pc build? If it foes not work there either, it is rather a code error and if it does work it's an android specific error. \$\endgroup\$CheckerT– CheckerT07/06/2024 17:39:15Commented Jul 6, 2024 at 17:39
-
\$\begingroup\$ @CheckerT It was working smoothly in the editor. I was compiling for Android, so I thought the fault was in Android, but when I compiled for PC, the saving part was also problematic. So, I edited the code and it worked. Thank you very much for your answer, I have already explained how I solved the problem. \$\endgroup\$Volkan AKDAG– Volkan AKDAG07/06/2024 21:35:53Commented Jul 6, 2024 at 21:35
1 Answer 1
\$\begingroup\$
\$\endgroup\$
1
This looks like an Android permissions issue. You need to ask the user for permission to write files. See Android.Permission.
Try this:
#if UNITY_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
{
Permission.RequestUserPermission(Permission.ExternalStorageWrite);
}
#endif
answered Jul 3, 2024 at 10:22
-
\$\begingroup\$ I'll look into Android.Permission \$\endgroup\$Volkan AKDAG– Volkan AKDAG07/03/2024 11:54:25Commented Jul 3, 2024 at 11:54
You must log in to answer this question.
lang-cs