示例#1
0
        public void AddEntry(ScoreboardEntryData scoreboardEntryData)
        {
            ScoreboardSaveData  savedScores = GetSavedScores();
            ScoreboardEntryData hashedScoreboardEntryData = CreateIntegrityHash(scoreboardEntryData);

            Debug.Log("Adding new score entry...");

            bool scoreAdded = false;

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

            if (!scoreAdded && savedScores.highscores.Count < maxScoreboardEntries)
            {
                savedScores.highscores.Add(hashedScoreboardEntryData);
            }

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

            UpdateUI(savedScores);
            SaveScores(savedScores);

            Debug.Log("New entry added succesfully");
        }
示例#2
0
        private void Start()
        {
            ScoreboardSaveData savedScores = GetSavedScores();

            UpdateUI(savedScores);
            SaveScores(savedScores);
        }
示例#3
0
 private void SaveScores(ScoreboardSaveData scoreboardSaveData)
 {
     using (StreamWriter stream = new StreamWriter(SavePath))
     {
         string json = JsonUtility.ToJson(scoreboardSaveData, true);
         stream.Write(json);
     }
 }
示例#4
0
        private void UpdateUI(ScoreboardSaveData savedScores)
        {
            if (SceneManager.GetActiveScene().buildIndex > 0)
            {
                return;
            }
            foreach (Transform child in highscoreHolderTransform)
            {
                Destroy(child.gameObject);
            }

            foreach (ScoreboardEntryData highscore in savedScores.highscores)
            {
                var checkedHighscore = CheckHighScore(highscore);
                var child            = Instantiate(scoreboardEntryObject, highscoreHolderTransform);
                child.GetComponent <ScoreboardEntryUI>().Initialise(checkedHighscore);
            }

            SaveScores(savedScores);
        }