Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //----------------< Brought to you by FRITZ PAZ @PAZ GAMES >----------------
- //Facebook: http://www.facebook.com/pazgamesdev/
- //Twitter: http://www.twitter.com/paz_games/
- //Contact: contact@pazgames.com
- //Special thanks to Ben Tristem for the spectacular Unity course!
- //Much of what I learned came from there!
- //You can find the course at: http://www.udemy.com/unitycourse/
- //or at: http://www.completeunitydeveloper.com/
- //-------------------------< The Code Starts Here >-------------------------
- //Do not forget to Include those! You may get errors without them!
- using UnityEngine;
- using System;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.IO;
- //The main class, that will be used at Runtime.
- public class DataSaving : MonoBehaviour
- {
- //You can add any variables you want, like Bools, Arrays, Lists, Dictionaries, Time, Etc.
- //Just keep the pattern used here! You can also add static classes, for like adding money, score, highscore, keeping achievements,
- //unlocked objects, etc!
- public static DataSaving dataSaving; //Static Reference for the class
- public int money; //Int representing the money in the game.
- public int highscore; //Your highscore
- public bool saveBool; //A bool you may want to save (PlayerPrefs doesn't save bools)
- public string fileName = "/info.dat";//Name of the file which will be saved
- void OnEnable()//Whenever this object is enabled this will be called
- {
- LoadData();//Loads the Data when starting the game
- DontDestroyOnLoad(this);//Keeps this instance active on all scenes
- }
- void OnDisable()//Whenever this object is disabled, this will be called
- {
- SaveData();//Saves the Data when leaving the game
- }
- public void LoadData()//Function responsible for Data Loading
- {
- if (File.Exists(Application.persistentDataPath + fileName))//This will only take place if the file exists, so we do not get errors
- {
- //System for the loading (Do not change unless you know what you are doing)
- BinaryFormatter bf = new BinaryFormatter();
- FileStream file = File.Open(Application.persistentDataPath + fileName, FileMode.Open);
- InternalData intData = (InternalData)bf.Deserialize(file);
- file.Close();
- //^^
- //Passing the variables from the saved data to the game data
- //You can add all variables to be loaded.
- //Works like this:
- //Data used in the game = Data saved last time.
- highscore = intData.highscore;
- money = intData.money;
- saveBool = intData.saveBool;
- }
- }
- public void SaveData()//Function responsible for Data Saving
- {
- //System for the saving (Do not change unless you know what you are doing)
- BinaryFormatter bf = new BinaryFormatter();
- FileStream file = File.Create(Application.persistentDataPath + fileName);
- InternalData intData = new InternalData();
- //^^
- //Passing the variables from the saved data to the game data
- //You can add all variables to be saved.
- //Works like this:
- //Data that will be save = Data in the game
- intData.highscore = highscore;
- intData.money = money;
- intData.saveBool = saveBool;
- }
- }
- [Serializable]//Necessary for Serialization of the class
- public class InternalData
- {
- public int money; //Int representing the money in the game.
- public int highscore; //Your highscore
- public bool saveBool; //A bool you may want to save (PlayerPrefs doesn't save bools)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement