public static Savedata SaveProcess() { //I. Create a new savedata object. Savedata sav = new Savedata(); //II. Initialize this object by loading local file. sav.InitialSavedataObject(); sav.CreateNewSaveData(); //III. Write object to local file. sav.WriteSaveData(); //IV. Return. return(sav);//Maybe have some lifetime problem. }
private Savedata LoadAndBuild() { bool failureFlag = false; //Load local file if it exists. string _path = Application.persistentDataPath + "/savedata.rua"; if (File.Exists(_path)) { BinaryFormatter bf = new BinaryFormatter(); FileStream _file = File.Open(_path, FileMode.Open);// No handle for open failure. if (_file == null) { Debug.Log("Open files failed. The file has been removed at runtime."); // failureFlag = true;// Maybe can let control stream jump to create brunch. InitialSavedataObject(); } else { Savedata _tmpSav = (Savedata)bf.Deserialize(_file);// No handle for deserialize failure. if (_tmpSav == null) { _tmpSav.InitialSavedataObject(); } _file.Close(); _highScore = _tmpSav._highScore; _highMove = _tmpSav._highMove; _allTimeMove = _tmpSav._allTimeMove; } } //If not, create a new file else { InitialSavedataObject(); } return(this); }
private Savedata CreateNewSaveData() { bool failureFlag = false; //I. Building Savedata object. //Load local file if it exists. string _path = Application.persistentDataPath + "/savedata.rua"; if (File.Exists(_path)) { BinaryFormatter bf = new BinaryFormatter(); FileStream _file = File.Open(_path, FileMode.Open);// No handle for open failure. if (_file == null) { Debug.Log("Open files failed. The file has been removed at runtime."); // failureFlag = true;// Maybe can let control stream jump to create brunch. InitialSavedataObject(); } else { Savedata _tmpSav = (Savedata)bf.Deserialize(_file);// No handle for deserialize failure. if (_tmpSav == null) { _tmpSav.InitialSavedataObject(); } _file.Close(); _highScore = _tmpSav._highScore; _highMove = _tmpSav._highMove; _allTimeMove = _tmpSav._allTimeMove; //_highItems = _tmpSav._highItems;// Uncertain //_allTimeItems = _tmpSav._allTimeItems; } } //If not, create a new file else { InitialSavedataObject(); } //II. Update some values of savedata object. GameObject _gcon = GameObject.FindWithTag("GameController"); GameObject _player = GameObject.FindWithTag("Player"); Score[] _score = _gcon.GetComponents <Score>(); for (int i = 0; i < _score.Length; ++i)//To find the highscore, but not exactly. { if (_score[i].getScore() > _highScore) { _isHighScore = 1; _highScore = _score[i].getScore(); _highMove = _player.transform.position.x; Debug.Log("New record!"); } } _allTimeMove += _player.transform.position.x; if (failureFlag) { Debug.Log("Savedata object creation is failed."); } else { Debug.Log("Savedata creation is completed successfully."); } _isCreated = 1; return(this); }