public void saveFile(saveData data) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Create(Application.persistentDataPath + data.ToString() + ".dat"); print("This was saved to " + Application.persistentDataPath + data.ToString() + ".dat"); /* * This area was to test the save data, what now should happen is the data is already in existance, and this just saves it. * */ //data.scores[0] = 1.1111f; /*data.level.Add( new Level()); * //data.level [0].scene = currentScene; * data.level [0].worldNumber = 420000; * data.number = 25.35f; * data.scores = new float[numberofLevels]; * data.scores [currentScene.buildIndex] = score; */ int index; if (currentScene.name == "puffinNestattempt") { index = previousLevelIndex; } else { index = currentLevelIndex; } if (score > data.level [index].levelHighScore) { data.level [index].levelHighScore = score; } if (currentPuffletsHatched > data.level [index].puffletsHatched) { data.level [index].puffletsHatched = currentPuffletsHatched; } /* * here I create an instance of class and give it the data to store * saveData data = new saveData(); * data.health = health; * data.experience = experience; * * */ /* if (data.level [currentLevelIndex].levelHighScore < score) * { * data.level [currentLevelIndex].levelHighScore = score; * }*/ bf.Serialize(file, data); file.Close(); }
public saveData loadFile(saveData data) { if (File.Exists(Application.persistentDataPath + data.ToString() + ".dat")) { //try{ BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + data.ToString() + ".dat", FileMode.Open); //persistant data path that Unity makes// playerInfo is specific file //filemode data = (saveData)bf.Deserialize(file); //cast this to the object we need. It is stored as a generic object file.Close(); print(data.name + "'s file was loaded"); print("Save file located at" + Application.persistentDataPath + data.ToString() + ".dat"); return(data); /*print(data.number); * print (data.scores [currentScene.buildIndex]); * scoreText.text = data.scores[currentScene.buildIndex] + " Eels collected!"; * print(data.level[0].worldNumber); * //print(data.level [0].scene.name);*/ /* * this is where you give your script the data that was just loaded * health = data.health; * experience = data.experience; */ //} /*catch(ArgumentOutOfRangeException r) * { * * * }*/ } else { print("File Doesn't exist, so it can't be loaded"); return(data); } }