コード例 #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);
    }
コード例 #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);
     }
 }
コード例 #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);
        }
    }
コード例 #4
0
    //Loads highscores from file and updates UI
    private void OnEnable()
    {
        HighscoreSaveData savedScores = HighScoreWriter.GetSavedScores();

        UpdateUI(savedScores);
    }