- Home /
Question by
PinzonGames · Apr 07, 2020 at 07:34 AM ·
android2ddatabasejson
Unity Android JSON Read / Write
I'm trying to make a simple json application. I have 2 buttons , save and add coins . It will save my Name ( input field ) and coin to json . Im working on it since last week but still can't fix it . It works well in PC , but when , build it to android , it do not works . Can someone help me please ?
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class DataManager : MonoBehaviour {
public PlayerData data;
private string file = "player.txt";
void Start() {
// GetFilePath(file);
// TextAsset filee = Resources.Load(file) as TextAsset;
// Debug.Log(filee);
}
public void Save() {
string json = JsonUtility.ToJson(data);
WriteToFile(file, json);
}
public void Load() {
data = new PlayerData();
string json = ReadFromFile(file);
JsonUtility.FromJsonOverwrite(json, data);
}
private void WriteToFile(string fileName, string json) {
string path = GetFilePath(fileName);
FileStream fileStream = new FileStream(path, FileMode.Create);
using(StreamWriter writer = new StreamWriter(fileStream)) {
writer.Write(json);
}
}
private string ReadFromFile(string fileName) {
string path = GetFilePath(fileName);
if (File.Exists(path))
{
using(StreamReader reader = new StreamReader(path)) {
Debug.Log(path);
string json = reader.ReadToEnd();
return json;
}
}
else {
string json = "File Not Found !";
Debug.Log("File not found !");
return json;
}
}
private string GetFilePath(string fileName) {
return Application.persistentDataPath + "/" + fileName;
}
}
here is my code . And my PlayerData :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerData {
public string name = "";
public int coins = 0;
}
please anyone help me . I waste many hours on this..
Comment
Your answer
Follow this Question
Related Questions
Project feasibility 1 Answer
File.WriteAllText() on Android (Json file) 2 Answers
Unity sqlite to android: no such table 0 Answers
Scroll Rect Content Disappearing (Video) 1 Answer
Json not recognized on Android 1 Answer