i have a problem with unity and Json. Well i need to save a name and score from a game, i think Json it’s a great option but i never used them.
I understand the basics, how to read one JsonObject but the problem i don’t know how to creat a new object.
later i try to create an array of object in Json and i dont know hoy read them.
this is my idea.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class Score : MonoBehaviour {
string path;
string jsonString;
// Use this for initialization
void Start () {
path = Application.dataPath + "/ScoreRecords.json";
jsonString = File.ReadAllText (path);
ListaRecords listaRecords = JsonUtility.FromJson<ListaRecords> (jsonString);
print(listaRecords);
foreach(Record record in listaRecords){
Debug.Log ("nombre: " + record.name + "score: " + record.score);
}
}
// Update is called once per frame
void Update () {
}
}
[System.Serializable]
public class Record{
public string name;
public int score;
}
[System.Serializable]
public class ListaRecords{
public List<Record> Records;
}
and this is my Json
{
"Records": [
{"nombre":"daniel", "puntos":100},
{"nombre":"Adrian", "puntos":200}
]
}