private void SaveScores(ScoreboardSaveData scoreboardSaveData) { //Set the savepath for the score data savePath = $"{Application.persistentDataPath}/highscores.dat"; //Write the stream data to the stream path /*using (StreamWriter stream = new StreamWriter(savePath)) * { * //Write the save data to json * string json = JsonUtility.ToJson(scoreboardSaveData, true); * * //Write the data * stream.Write(json); * }*/ FileStream file; if (File.Exists(savePath)) { file = File.OpenWrite(savePath); } else { file = File.Create(savePath); } BinaryFormatter binaryFormatter = new BinaryFormatter(); binaryFormatter.Serialize(file, scoreboardSaveData); file.Close(); }
private void UpdateUI(ScoreboardSaveData savedScores) { if (savedScores.highScores.Count == 0) { //Disable the highscores holder scoreHolder.gameObject.SetActive(false); return; } //If the holder game object is disabled if (scoreHolder.gameObject.activeSelf == false) { //Activate the game object scoreHolder.gameObject.SetActive(true); } //Loop through all child objects in scoreHoler foreach (Transform child in scoreHolder) { //Destroy the child object Destroy(child.gameObject); } //Loop through all the entries in savedScores foreach (ScoreEntryData highScore in savedScores.highScores) { //Spawn the entry UI and initalize the entry Instantiate(scoreEntryObject, scoreHolder).GetComponent <ScoreEntryUI>().Initialize(highScore); } }
//check scores for adding, if more than max then keep the highest public void AddEntry(ScoreboardEntryData scoreboardEntryData) { ScoreboardSaveData SavedScores = GetSavedScore(); bool scoreAdded = false; //find the higher score and save to scoreboard for (int i = 0; i < SavedScores.Scores.Count; i++) { if (scoreboardEntryData.InputScore > SavedScores.Scores[i].InputScore) { SavedScores.Scores.Insert(i, scoreboardEntryData); scoreAdded = true; break; } } //check max, if not max then add more if (!scoreAdded && SavedScores.Scores.Count < MaxScoreboard) { SavedScores.Scores.Add(scoreboardEntryData); } //check for more than max if (SavedScores.Scores.Count > MaxScoreboard) { SavedScores.Scores.RemoveRange(MaxScoreboard, SavedScores.Scores.Count - MaxScoreboard); } UpdateUI(SavedScores); SaveScores(SavedScores); }
public void ListScores() { //Get the saved scores ScoreboardSaveData savedScores = GetSavedScores(); //Display the UI UpdateUI(savedScores); }
//also messing around with Json private void SaveScores(ScoreboardSaveData scoreboardSaveData) { using (StreamWriter stream = new StreamWriter(path)) { string json = JsonUtility.ToJson(scoreboardSaveData, true); stream.Write(json); } }
private void SaveScores(ScoreboardSaveData saveData) { using (var stream = new StreamWriter(SavePath)) { var jsonData = JsonUtility.ToJson(saveData, true); stream.Write(jsonData); } }
private void Start() { //Set the savepath for the score data savePath = $"{Application.persistentDataPath}/highscores.dat"; //Get the saved scores ScoreboardSaveData savedScores = GetSavedScores(); //Save the scores immediately SaveScores(savedScores); }
//update score slot (adding and removing) private void UpdateUI(ScoreboardSaveData SavedScores) { foreach (Transform child in TempScore) { Destroy(child.gameObject); } foreach (ScoreboardEntryData score in SavedScores.Scores) { Instantiate(ScoreboardEntryObject, TempScore).GetComponent <ScoreboardEntryUI>().init(score); } }
private void UpdateUI(ScoreboardSaveData saveData) { foreach (Transform child in highscoreHolder) { Destroy(child.gameObject); } foreach (var data in saveData.highscores) { Instantiate(entryObject, highscoreHolder).GetComponent <ScoreboardEntryUI>().Initialize(data); } }
void Start() { //for adding input test.InputName = ""; test.InputScore = 0; showEnterNameUI(); ScoreboardSaveData SavedScores = GetSavedScore(); UpdateUI(SavedScores); SaveScores(SavedScores); }
public void DeleteFile() { if (File.Exists(savePath)) { //Delete the save path File.Delete(savePath); //Get the saved scores ScoreboardSaveData savedScores = GetSavedScores(); //Save the scores immediately SaveScores(savedScores); //Update the score UI UpdateUI(savedScores); } }
public void AddEntry(ScoreEntryData entryData) { //Get the saved scores ScoreboardSaveData savedScores = GetSavedScores(); bool scoreAdded = false; //Loop through all saved scores for (int i = 0; i < savedScores.highScores.Count; i++) { //Check if the entry score is greater than this score if (entryData.entryScore >= savedScores.highScores[i].entryScore) { //Add the score to that point in the list savedScores.highScores.Insert(i, entryData); //Score has now been added scoreAdded = true; //Break out of the loop break; } } //Check if the score has not been added and the saved scores is lower than the max scores if (!scoreAdded && savedScores.highScores.Count < maxEntries) { //Add the entry data as a new item on the list savedScores.highScores.Add(entryData); } //If the number of saved scores is higher than the max entries if (savedScores.highScores.Count > maxEntries) { //Remove a range of entries until the number is equal to the max entries savedScores.highScores.RemoveRange(maxEntries, savedScores.highScores.Count - maxEntries); } //Save the scores SaveScores(savedScores); //Update the UI UpdateUI(savedScores, entryData); }
private void UpdateUI(ScoreboardSaveData savedScores, ScoreEntryData newData) { if (savedScores.highScores.Count == 0) { //Disable the highscores holder scoreHolder.gameObject.SetActive(false); return; } //If the holder game object is disabled if (scoreHolder.gameObject.activeSelf == false) { //Activate the game object scoreHolder.gameObject.SetActive(true); } //Loop through all child objects in scoreHoler foreach (Transform child in scoreHolder) { //Destroy the child object Destroy(child.gameObject); } //Loop through all the entries in savedScores foreach (ScoreEntryData highScore in savedScores.highScores) { //Spawn the entry UI ScoreEntryUI scoreListing = Instantiate(scoreEntryObject, scoreHolder).GetComponent <ScoreEntryUI>(); //Initalise the UI scoreListing.Initialize(highScore); //check if the new data matches the current highscore if (newData.Equals(highScore)) { scoreListing.HighlightUI(); } } }
private ScoreboardSaveData GetSavedScores() { //Set the savepath for the score data savePath = $"{Application.persistentDataPath}/highscores.dat"; //If the file exists if (!File.Exists(savePath)) { //Create the savepath file File.Create(savePath).Dispose(); //return new instance return(new ScoreboardSaveData()); } //Otherwise read the stream data /*using (StreamReader stream = new StreamReader(savePath)) * { * //Read the whole data * string json = stream.ReadToEnd(); * * //Return the converted JSON data * return JsonUtility.FromJson<ScoreboardSaveData>(json); * }*/ FileStream file; file = File.OpenRead(savePath); BinaryFormatter binaryFormatter = new BinaryFormatter(); ScoreboardSaveData savedScores = (ScoreboardSaveData)binaryFormatter.Deserialize(file); file.Close(); return(savedScores); }