Exemplo n.º 1
0
    //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);
    }
Exemplo n.º 2
0
 //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);
     }
 }
Exemplo n.º 3
0
    //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);
        }
    }
Exemplo n.º 4
0
    //Loads highscores from file and updates UI
    private void OnEnable()
    {
        HighscoreSaveData savedScores = HighScoreWriter.GetSavedScores();

        UpdateUI(savedScores);
    }