//Adds a highscoreEntry to the existing list public static void AddEntry(HighscoreEntry highscoreEntry) { HighscoreSaveData savedScores = GetSavedScores(); bool scoreAdded = false; for (int i = 0; i < savedScores.highscoreList.Count; i++) { if (highscoreEntry.entryScore > savedScores.highscoreList[i].entryScore) { savedScores.highscoreList.Insert(i, highscoreEntry); scoreAdded = true; break; } } if (!scoreAdded && savedScores.highscoreList.Count < maxEntryLimit) { savedScores.highscoreList.Add(highscoreEntry); } if (savedScores.highscoreList.Count > maxEntryLimit) { savedScores.highscoreList.RemoveRange(maxEntryLimit, savedScores.highscoreList.Count - maxEntryLimit); } SaveScores(savedScores); }
//Saves JSON to file private static void SaveScores(HighscoreSaveData highscoreSaveData) { using (StreamWriter stream = new StreamWriter(SavePath)) { string json = JsonUtility.ToJson(highscoreSaveData, true); stream.Write(json); } }
//Updates UI, creating new entrys if needed private void UpdateUI(HighscoreSaveData savedScores) { foreach (Transform child in highscoresContainerTransform) { Destroy(child.gameObject); } foreach (HighscoreEntry highscore in savedScores.highscoreList) { Instantiate(highscoreEntryTemplate, highscoresContainerTransform).GetComponent <HighscoreUI>().Initalise(highscore); } }
//Loads highscores from file and updates UI private void OnEnable() { HighscoreSaveData savedScores = HighScoreWriter.GetSavedScores(); UpdateUI(savedScores); }