// Use this for initialization void Start() { LvlJson lvlJson = null; if (Level_Number != 0) { lvlJson = JsonManagerScore.ReadLvlJSON(Level_Number); } Level_Info.text = "LEVEL " + Level_Number; if (lvlJson != null) { this.GetComponent <StarDisplay>().DisplayStars(lvlJson.Stars); Score_Info.text = "Score: " + lvlJson.Score; //Calculate next star if (lvlJson.Stars != 0) { if (lvlJson.Stars == 5) { NextStar_Info.text = "Level completed!"; } else { NextStar_Info.text = "\nNext in: " + lvlJson.LimitStarts[lvlJson.Stars]; Instantiate(ModelStar, positionModelStart); } } } }
public static bool StoreLvlJSON(LvlJson aux) { //READ JSON bool inside = false; bool new_record = false; List <LvlJson> lvls = null; try { lvls = JsonManagerScore.ReadLvlJSON(); //SEARCH LVL foreach (LvlJson lvl in lvls) { //UPDATE IF IS CREATED if (lvl.Lvl == aux.Lvl) { if (lvl.Score < aux.Score) // IF NEW RECORD { lvl.Score = aux.Score; if (lvl.Stars < aux.Stars) //IF NEW STAR OBTAINED { lvl.Stars = aux.Stars; lvl.LimitStarts = aux.LimitStarts; } new_record = true; } inside = true; } } } catch { } //CREATE IF IS NEW if (!inside) { if (lvls == null) { lvls = new List <LvlJson>(); } lvls.Add(aux); new_record = true; } //WRITE FILE string path = Path.Combine(Application.persistentDataPath, "score.json"); string json = JsonConvert.SerializeObject(lvls.ToArray()); using (TextWriter writer = File.CreateText(path)) { writer.Write(json); } return(new_record); }
public static List <LvlJson> ReadLvlJSON() { List <LvlJson> lvlList = new List <LvlJson>(); //READ FILE string path = Path.Combine(Application.persistentDataPath, "score.json"); string jsonFile = null; using (TextReader reader = File.OpenText(path)) { jsonFile = reader.ReadToEnd(); } //Debug.Log(jsonFile); SEE CONTENT OF JSON FILE //PARSE JSON FILE var objects = JArray.Parse(jsonFile); // parse as array foreach (JObject root in objects) { //Debug.Log(root.ToString()); LvlJson lvl = JsonConvert.DeserializeObject <LvlJson>(root.ToString()); lvlList.Add(lvl); } return(lvlList); }