Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.IO;
- public static class SaveLoad {
- public static SaveFile saveFile = new SaveFile();
- public static void GatherData() //function that gathers variables data and sends them to SaveGame class
- {
- SaveFile.current.hiScoreSave = GameManagerScript.highScore;
- SaveFile.current.totalGoldSave = GameManagerScript.totalGold;
- }
- public static void PopulateData() //function that sends data from SaveGame back to game's classes
- {
- GameManagerScript.highScore = SaveFile.current.hiScoreSave;
- GameManagerScript.totalGold = SaveFile.current.totalGoldSave;
- }
- public static void Save() {
- GatherData();
- saveFile = SaveFile.current;
- BinaryFormatter bf = new BinaryFormatter();
- FileStream fileStream = File.Create (Application.persistentDataPath + "/save.sav");
- bf.Serialize(fileStream, saveFile);
- fileStream.Close();
- // Debug.Log(Application.persistentDataPath); //uncomment this to see where the save file is stored
- }
- public static void Load() {
- if(File.Exists(Application.persistentDataPath + "/save.sav")) {
- BinaryFormatter bf = new BinaryFormatter();
- FileStream fileStream = File.Open(Application.persistentDataPath + "/save.sav", FileMode.Open);
- saveFile = (SaveFile)bf.Deserialize(fileStream);
- fileStream.Close();
- SaveFile.current = saveFile;
- }
- PopulateData();
- }
- public static void Delete()
- {
- if(File.Exists(Application.persistentDataPath + "/save.sav"))
- {
- File.Delete(Application.persistentDataPath + "/save.sav");
- }
- }
- }
- [System.Serializable]
- public class SaveFile {
- // in this class we store data that will be saved to a file
- public static SaveFile current;
- public int hiScoreSave;
- public int totalGoldSave;
- public SaveFile () {
- hiScoreSave = 0;
- totalGoldSave = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement