public void LoadGame() { if (File.Exists(Application.persistentDataPath + "/scoreData.dat")) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/scoreData.dat", FileMode.Open); ScoreDataContainer scoreDataContainer = (ScoreDataContainer)bf.Deserialize(file); foreach (ScoreData scoreData in scoreDataContainer.GetScoreDataList()) { Debug.Log("scoreData.score: " + scoreData.Score); Debug.Log("scoreData.combo: " + scoreData.MaxCombo); Debug.Log("scoreData.wpm: " + scoreData.Wpm); Debug.Log("scoreData.acc: " + scoreData.Accuracy); Debug.Log("scoreData.error: " + scoreData.Error); Debug.Log("------: " + scoreData.Error); } file.Close(); Debug.Log("Game data loaded!"); } else { Debug.LogError("There is no save data!"); } }
/* * This start method should be the main method used to swap out classes * This is intentional to be semi-modular with any other * classes that want to save their data */ //void Start() // { // } /* * Save this session to file * IF a record already exists, check to save into a list of record saves * if record saves is at max saves, delete oldest save and store into file. */ public void SaveGame() { BinaryFormatter bf = new BinaryFormatter(); // If file exists, get data from it if (File.Exists(Application.persistentDataPath + "/scoreData.dat")) { FileStream file = File.Open(Application.persistentDataPath + "/scoreData.dat", FileMode.Open); ScoreDataContainer scoreDataContainer = (ScoreDataContainer)bf.Deserialize(file); file.Close(); ScoreData data = new ScoreData(); data.Score = statManager.GetScore(); data.MaxCombo = statManager.GetMaxCombo(); data.Wpm = statManager.GetWpm(); data.Accuracy = statManager.GetAccuracy(); data.Error = statManager.GetIncorrectKeyStrokes(); scoreDataContainer.Add(data); FileStream newFile = File.Create(Application.persistentDataPath + "/scoreData.dat"); bf.Serialize(newFile, scoreDataContainer); newFile.Close(); } // If file DNE exists, create file and store data else { FileStream file = File.Create(Application.persistentDataPath + "/scoreData.dat"); ScoreData data = new ScoreData(); data.Score = statManager.GetScore(); data.MaxCombo = statManager.GetMaxCombo(); data.Wpm = statManager.GetWpm(); data.Accuracy = statManager.GetAccuracy(); data.Error = statManager.GetIncorrectKeyStrokes(); ScoreDataContainer scoreDataContainer = new ScoreDataContainer(); scoreDataContainer.Add(data); bf.Serialize(file, scoreDataContainer); file.Close(); } Debug.Log("Game data saved!"); }
private void Awake() { entryContainer = transform.Find("HighScoreEntryContainer"); entryTemplate = entryContainer.Find("HighScoreEntryTemplate"); entryTemplate.gameObject.SetActive(false); ScoreDataContainer scoreDataContainer = serializer.LoadSaveData(); if (scoreDataContainer == null) // null check { return; } //foreach (ScoreData scoreData in scoreDataContainer.GetScoreDataList()) //{ // Debug.Log("scoreData.score: " + scoreData.Score); // Debug.Log("scoreData.combo: " + scoreData.MaxCombo); // Debug.Log("scoreData.wpm: " + scoreData.Wpm); // Debug.Log("scoreData.acc: " + scoreData.Accuracy); // Debug.Log("scoreData.error: " + scoreData.Error); // Debug.Log("------"); //} List <ScoreData> scoreDataList = scoreDataContainer.GetScoreDataList(); float templateHeight = 45f; for (int i = scoreDataList.Count - 1, j = 0; i >= 0; i--) //for (int i = 0; i < scoreDataList.Count; i++) { ScoreData currScoreData = scoreDataList[i]; Debug.Log("currScoreData.Score.ToString(): " + currScoreData.Score.ToString()); Transform entryTransform = Instantiate(entryTemplate, entryContainer); RectTransform entryRectTransform = entryTransform.GetComponent <RectTransform>(); entryRectTransform.anchoredPosition = new Vector2(entryRectTransform.position.x, -templateHeight * j); j++; entryTransform.Find("ScoreText").GetComponent <Text>().text = currScoreData.Score.ToString(); entryTransform.Find("MaxComboText").GetComponent <Text>().text = currScoreData.MaxCombo.ToString(); entryTransform.Find("WPMText").GetComponent <Text>().text = currScoreData.Wpm.ToString("0.0"); entryTransform.Find("AccuracyText").GetComponent <Text>().text = currScoreData.Accuracy.ToString("0.00") + "%"; entryTransform.Find("ErrorsText").GetComponent <Text>().text = currScoreData.Error.ToString(); entryTransform.gameObject.SetActive(true); } }
public ScoreDataContainer LoadSaveData() { ScoreDataContainer scoreDataContainer = null; if (File.Exists(Application.persistentDataPath + "/scoreData.dat")) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/scoreData.dat", FileMode.Open); scoreDataContainer = (ScoreDataContainer)bf.Deserialize(file); file.Close(); Debug.Log("Game data loaded!"); } else { Debug.LogError("There is no save data!"); } return(scoreDataContainer); }