//return all track scores private static TrackScores[] Load() { if (System.IO.File.Exists(Application.persistentDataPath + fileName)) { string unparsedJson = System.IO.File.ReadAllText(Application.persistentDataPath + fileName); TrackScoresWrapper list = JsonUtility.FromJson <TrackScoresWrapper>(unparsedJson); return(list.objects); } else { File.Create(Application.persistentDataPath + fileName).Dispose(); return(null); } return(null); }
private static void Save(TrackScores track) { //Debug.Log(track.names[0]); TrackScores[] curList = Load(); if (curList == null || curList.Length == 0) { curList = new TrackScores[1]; curList[0] = new TrackScores(); curList[0].track = track.track; curList[0].times[0] = track.times[0]; curList[0].names[0] = track.names[0]; //Debug.Log(curList[0].track); //Debug.Log(curList[0].times[0]); //Debug.Log(curList[0].names[0]); } else { //Debug.Log("its trying to save here"); for (int i = 0; i < curList.Length; i++) { if (curList[i].track == track.track) { curList[i].times = track.times; curList[i].names = track.names; } } } //Debug.Log(curList[0].track); //Debug.Log(curList[0].times[0]); //Debug.Log(curList[0].names[0]); TrackScoresWrapper wrapper = new TrackScoresWrapper(); wrapper.objects = curList; string jsonString = JsonUtility.ToJson(wrapper); //Debug.Log("Almost there, " + jsonString); File.Create(Application.persistentDataPath + fileName).Dispose(); File.WriteAllText(Application.persistentDataPath + fileName, jsonString); }
//return one track score private static TrackScores Load(string trackName) { if (System.IO.File.Exists(Application.persistentDataPath + fileName)) { string unparsedJson = System.IO.File.ReadAllText(Application.persistentDataPath + fileName); TrackScoresWrapper list = JsonUtility.FromJson <TrackScoresWrapper>(unparsedJson); for (int i = 0; i < list.objects.Length; i++) { if (list.objects [i].track == trackName) { return(list.objects[i]); } } } else { File.Create(Application.persistentDataPath + fileName).Dispose(); return(null); } return(null); }