Created
August 9, 2014 16:04
-
-
Save zerosalife/6f6e811b27761d287ea5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System; | |
using System.Runtime.Serialization.Formatters.Binary; | |
using System.IO; | |
public class GameController: MonoBehaviour { | |
public static GameController control; | |
public int experiencePoints; | |
public int playerLevel; | |
public int score; | |
// Add new persistent variables here. | |
void Awake() { | |
if(control == null) { | |
DontDestroyOnLoad(gameObject); | |
control = this; | |
} else if(control != this) { | |
// There can be only one! | |
Destroy(gameObject); | |
} | |
} | |
public void Save() { | |
BinaryFormatter bf = new BinaryFormatter(); | |
FileStream file = File.Open(Application.persistentDataPath + "/gameInfo.dat", | |
FileMode.Open); | |
PlayerData data = new PlayerData(); | |
// Add new persistent variables to be saved here. | |
data.experiencePoints = experiencePoints; | |
data.playerLevel = playerLevel; | |
data.score = score; | |
bf.Serialize(file, data); | |
file.Close(); | |
} | |
public void Load() { | |
if(File.Exists(Application.persistentDataPath + "/gameInfo.dat")) { | |
BinaryFormatter bf = new BinaryFormatter(); | |
FileStream file = File.Open(Application.persistentDataPath + "/gameInfo.dat", | |
FileMode.Open); | |
PlayerData data = (PlayerData)bf.Deserialize(file); | |
file.Close(); | |
// Add new persistent variables to be loaded here. | |
experiencePoints = data.experiencePoints; | |
playerLevel = data.playerLevel; | |
score = data.score; | |
} | |
} | |
[Serializable] | |
class PlayerData { | |
// TODO: see about making gets and sets. | |
// TODO: automate the generation of this data structure. See: | |
// http://forums.devx.com/showthread.php?170650-How-to-dynamically-add-property-to | |
// Add new variables for loading and saving here. | |
public int experiencePoints; | |
public int playerLevel; | |
public int score; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment