Advertisement
Guest User

Saving your Game's Data Safely and Easily

a guest
Apr 8th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. //----------------< Brought to you by FRITZ PAZ @PAZ GAMES >----------------
  2. //Facebook: http://www.facebook.com/pazgamesdev/
  3. //Twitter: http://www.twitter.com/paz_games/
  4. //Contact: contact@pazgames.com
  5. //Special thanks to Ben Tristem for the spectacular Unity course!
  6. //Much of what I learned came from there!
  7. //You can find the course at: http://www.udemy.com/unitycourse/
  8. //or at: http://www.completeunitydeveloper.com/
  9. //-------------------------< The Code Starts Here >-------------------------
  10.  
  11. //Do not forget to Include those! You may get errors without them!
  12. using UnityEngine;
  13. using System;
  14. using System.Runtime.Serialization.Formatters.Binary;
  15. using System.IO;
  16.  
  17. //The main class, that will be used at Runtime.
  18. public class DataSaving : MonoBehaviour
  19. {
  20.     //You can add any variables you want, like Bools, Arrays, Lists, Dictionaries, Time, Etc.
  21.     //Just keep the pattern used here! You can also add static classes, for like adding money, score, highscore, keeping achievements,
  22.     //unlocked objects, etc!
  23.  
  24.     public static DataSaving dataSaving; //Static Reference for the class
  25.     public int money; //Int representing the money in the game.
  26.     public int highscore; //Your highscore
  27.     public bool saveBool; //A bool you may want to save (PlayerPrefs doesn't save bools)
  28.     public string fileName = "/info.dat";//Name of the file which will be saved
  29.  
  30.     void OnEnable()//Whenever this object is enabled this will be called
  31.     {
  32.         LoadData();//Loads the Data when starting the game
  33.         DontDestroyOnLoad(this);//Keeps this instance active on all scenes
  34.     }
  35.  
  36.     void OnDisable()//Whenever this object is disabled, this will be called
  37.     {
  38.         SaveData();//Saves the Data when leaving the game
  39.     }
  40.  
  41.     public void LoadData()//Function responsible for Data Loading
  42.     {
  43.         if (File.Exists(Application.persistentDataPath + fileName))//This will only take place if the file exists, so we do not get errors
  44.         {
  45.             //System for the loading (Do not change unless you know what you are doing)
  46.             BinaryFormatter bf = new BinaryFormatter();
  47.             FileStream file = File.Open(Application.persistentDataPath + fileName, FileMode.Open);
  48.             InternalData intData = (InternalData)bf.Deserialize(file);
  49.             file.Close();
  50.             //^^
  51.  
  52.             //Passing the variables from the saved data to the game data
  53.             //You can add all variables to be loaded.
  54.             //Works like this:
  55.             //Data used in the game = Data saved last time.
  56.             highscore = intData.highscore;
  57.             money = intData.money;
  58.             saveBool = intData.saveBool;
  59.         }
  60.     }
  61.  
  62.     public void SaveData()//Function responsible for Data Saving
  63.     {
  64.         //System for the saving (Do not change unless you know what you are doing)
  65.         BinaryFormatter bf = new BinaryFormatter();
  66.         FileStream file = File.Create(Application.persistentDataPath + fileName);
  67.         InternalData intData = new InternalData();
  68.         //^^
  69.  
  70.         //Passing the variables from the saved data to the game data
  71.         //You can add all variables to be saved.
  72.         //Works like this:
  73.         //Data that will be save = Data in the game
  74.         intData.highscore = highscore;
  75.         intData.money = money;
  76.         intData.saveBool = saveBool;
  77.     }
  78. }
  79.  
  80. [Serializable]//Necessary for Serialization of the class
  81. public class InternalData
  82. {
  83.     public int money; //Int representing the money in the game.
  84.     public int highscore; //Your highscore
  85.     public bool saveBool; //A bool you may want to save (PlayerPrefs doesn't save bools)
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement