Пример #1
0
        public void AddEntry(HighScoresEntryData highScoresEntryData)     //Adds new entry to list
        {
            HighScoresSaveData savedScores = GetSavedScores();

            bool scoreAdded = false;

            {
                for (int i = 0; i < savedScores.highscores.Count; i++)
                {
                    if (highScoresEntryData.entryScore > savedScores.highscores[i].entryScore)
                    {
                        savedScores.highscores.Insert(i, highScoresEntryData);
                        scoreAdded = true;
                        break;
                    }
                }

                if (!scoreAdded && savedScores.highscores.Count < maxHighScoresEntries)
                {
                    savedScores.highscores.Add(highScoresEntryData);
                }

                if (savedScores.highscores.Count > maxHighScoresEntries)
                {
                    savedScores.highscores.RemoveRange(maxHighScoresEntries, savedScores.highscores.Count - maxHighScoresEntries);
                }

                UpdateUI(savedScores);
                SaveScores(savedScores);
            }
        }
Пример #2
0
 private void SaveScores(HighScoresSaveData highscoresSaveData)     //Function to write data into save file
 {
     using (StreamWriter stream = new StreamWriter(SavePath))
     {
         string json = JsonUtility.ToJson(highscoresSaveData, true);
         stream.Write(json);
     }
 }
Пример #3
0
        private string SavePath => $"{Application.persistentDataPath}/highscores.json";     //Location of high score data

        private void Start()
        {
            HighScoresSaveData savedScores = GetSavedScores();

            UpdateUI(savedScores);

            SaveScores(savedScores);
        }
Пример #4
0
        private void UpdateUI(HighScoresSaveData savedScores)
        {
            foreach (Transform child in highscoresHolderTransform)    //After added entry, destroy old components and repopulate new components for the new list of scores
            {
                Destroy(child.gameObject);
            }

            foreach (HighScoresEntryData highScores in savedScores.highscores)    //Loop through all entries and create new components
            {
                Instantiate(highscoreEntryObject, highscoresHolderTransform).GetComponent <HighScoresEntryUI>().Initialise(highScores);
            }
        }