public bool SaveHighscore(HighscoreRecord hr) { if (hr.name == "") { hr.name = "-"; } int pos = 0; int min = Math.Min(AllScores.Count, topScoresCount); for (pos = 0; pos < min; pos++) { if (HighscoreRecordCompare(AllScores[pos], hr) > 0) { break; } } if (pos == topScoresCount) { return(true); } for (int i = pos; i < min; i++) { HighscoreRecord t = AllScores[i]; AllScores[i] = hr; hr = t; PlayerPrefs.SetString(highscoreNamePreKey + i, AllScores[i].name); PlayerPrefs.SetInt(highscoreScorePreKey + i, AllScores[i].score); } if (AllScores.Count >= topScoresCount) { PlayerPrefs.Save(); return(true); } AllScores.Add(hr); PlayerPrefs.SetString(highscoreNamePreKey + (AllScores.Count - 1), hr.name); PlayerPrefs.SetInt(highscoreScorePreKey + (AllScores.Count - 1), hr.score); AllScores.Sort(HighscoreRecordCompare); PlayerPrefs.SetInt(highscoreCountKey, AllScores.Count); PlayerPrefs.Save(); return(true); }
//saves highscore if name does not already exist //return true when successful public bool NewHighscore(string name, int score) { var hr = new HighscoreRecord(name, score); return(SaveHighscore(hr)); }
private static int HighscoreRecordCompare(HighscoreRecord hr1, HighscoreRecord hr2) { return(hr2.score - hr1.score); }