Advertisement
ziuber18

saveloadq

Nov 23rd, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using System.IO;
  6.  
  7. public static class SaveLoad {
  8.  
  9.     public static SaveFile saveFile = new SaveFile();
  10.  
  11.     public static void GatherData()     //function that gathers variables data and sends them to SaveGame class
  12.     {
  13.         SaveFile.current.hiScoreSave = GameManagerScript.highScore;
  14.         SaveFile.current.totalGoldSave = GameManagerScript.totalGold;
  15.     }
  16.     public static void PopulateData()       //function that sends data from SaveGame back to game's classes
  17.     {
  18.  
  19.         GameManagerScript.highScore = SaveFile.current.hiScoreSave;
  20.         GameManagerScript.totalGold = SaveFile.current.totalGoldSave;
  21.     }
  22.  
  23.     public static void Save() {
  24.         GatherData();
  25.         saveFile = SaveFile.current;
  26.         BinaryFormatter bf = new BinaryFormatter();
  27.         FileStream fileStream = File.Create (Application.persistentDataPath + "/save.sav");
  28.         bf.Serialize(fileStream, saveFile);
  29.         fileStream.Close();
  30. //      Debug.Log(Application.persistentDataPath);      //uncomment this to see where the save file is stored
  31.     }  
  32.  
  33.     public static void Load() {
  34.         if(File.Exists(Application.persistentDataPath + "/save.sav")) {
  35.             BinaryFormatter bf = new BinaryFormatter();
  36.             FileStream fileStream = File.Open(Application.persistentDataPath + "/save.sav", FileMode.Open);
  37.             saveFile = (SaveFile)bf.Deserialize(fileStream);
  38.             fileStream.Close();
  39.             SaveFile.current = saveFile;
  40.         }
  41.         PopulateData();
  42.     }
  43.     public static void Delete()
  44.     {
  45.         if(File.Exists(Application.persistentDataPath + "/save.sav"))
  46.         {
  47.             File.Delete(Application.persistentDataPath + "/save.sav");
  48.         }
  49.     }
  50. }
  51.  
  52. [System.Serializable]
  53. public class SaveFile {            
  54.     // in this class we store data that will be saved to a file
  55.    
  56.     public static SaveFile current;
  57.  
  58.     public int hiScoreSave;
  59.     public int totalGoldSave;
  60.    
  61.     public SaveFile () {
  62.         hiScoreSave = 0;
  63.         totalGoldSave = 0;
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement