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.IO;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using System;
- /*
- * SaveGame class. Replaces Unity's PlayerPrefs for saving game data.
- *
- * Uses Application.persistentDataPath for cross-platform compatibility.
- *
- * Can be used to replace PlayerPrefs directly, eg:
- * PlayerPrefs.SetInt("someInt", 1) -> SaveGame.SetInt("someInt", 1)
- *
- * © Arto Koistinen / Dicework Games 2012. Free for use.
- *
- */
- public class SaveGame {
- private static SaveGame _instance;
- private Hashtable saveData;
- private static SaveGame instance {
- get {
- if(_instance == null)
- new SaveGame();
- return _instance;
- }
- }
- private SaveGame() {
- _instance = this;
- saveData = new Hashtable();
- }
- public static void DeleteKey(string key) {
- instance.saveData.Remove(key);
- }
- public static void SetString(string key, string value) {
- if(instance.saveData[key] == null)
- instance.saveData.Add(key, value);
- else instance.saveData[key] = value;
- }
- public static void DeleteAll() {
- instance.saveData = new Hashtable();
- }
- public static void SetInt(string key, int value) {
- if(instance.saveData[key] == null)
- instance.saveData.Add(key, value);
- else instance.saveData[key] = value;
- }
- /// <summary>
- /// Loads the saveData hashtable from a file called "default.sav".
- /// </summary>
- public static void Load() {
- instance.saveData = (Hashtable) LoadFromFile("default.sav");
- if(instance.saveData == null)
- instance.saveData = new Hashtable();
- }
- /// <summary>
- /// Saves the saveData hashtable to a file called "default.sav".
- /// </summary>
- public static void Save() {
- SaveToFile(instance.saveData, "default.sav");
- }
- // if !DEBUG is just to prevent MonoDevelop from giving errors on the default value
- #if !DEBUG
- public static string GetString(string key, string defaultValue = "") {
- if(instance.saveData[key] == null)
- return defaultValue;
- else return (string) instance.saveData[key];
- }
- public static int GetInt(string key, int defaultValue = 0) {
- if(instance.saveData[key] == null)
- return defaultValue;
- else return (int) instance.saveData[key];
- }
- #endif
- // <summary>
- // Returns the save path. This is currently identical to Application.persistentDataPath.
- // </summary>
- // <returns>
- // A <see cref="System.String"/>
- // </returns>
- public static string GetPath() {
- string path = Application.persistentDataPath;
- // Debug.Log(path);
- return path;
- }
- // <summary>
- // Returns true if file exists in the save directory.
- // </summary>
- // <param name="filename">
- // A <see cref="System.String"/>
- // </param>
- // <returns>
- // A <see cref="System.Boolean"/>
- // </returns>
- public static bool Exists(string filename) {
- return File.Exists(GetPath() + "/" + filename);
- }
- /*
- *
- * Utility methods for SerializableDictionary. Uncomment if needed.
- *
- private static SerializableDictionary<string, SerializableDictionary<string,string>> ToSerializable( SerializableDictionary<string, SerializableDictionary<string, string>> source) {
- SerializableDictionary<string, SerializableDictionary<string, string>> serializable = new SerializableDictionary<string, SerializableDictionary<string, string>>();
- foreach(string key in source.Keys) {
- serializable[key] = ToSerializable(source[key]);
- }
- return serializable;
- }
- private static SerializableDictionary<string, LevelData> ToSerializable( SerializableDictionary<string, LevelData> source) {
- SerializableDictionary<string, LevelData> serializable = new SerializableDictionary<string, LevelData>();
- foreach(string key in source.Keys) {
- serializable[key] = source[key];
- }
- return serializable;
- }
- private static SerializableDictionary<string, string> ToSerializable( SerializableDictionary<string, string> source) {
- SerializableDictionary<string, string> serializable = new SerializableDictionary<string, string>();
- foreach(string key in source.Keys) {
- serializable[key] = source[key];
- }
- return serializable;
- } */
- // <summary>
- // Saves a System.Object to a file. Can be used to save any (serializable) classes. Uses binary formatter to serialize the file.
- // </summary>
- // <param name="data">
- // A <see cref="System.Object"/>
- // </param>
- // <param name="filename">
- // A <see cref="System.String"/>
- // </param>
- public static void SaveToFile(System.Object data, string filename) {
- string path = GetPath();
- DirectoryInfo saveDirectory = new DirectoryInfo(path);
- if(!saveDirectory.Exists) {
- Debug.Log("Creating save directory");
- saveDirectory.Create();
- }
- Debug.Log("Saving data to: " + saveDirectory.FullName + "/" + filename);
- Stream stream = new FileStream(saveDirectory.FullName + "/" + filename, FileMode.Create, FileAccess.Write, FileShare.None);
- BinaryFormatter formatter = new BinaryFormatter();
- formatter.Serialize(stream, data);
- stream.Close();
- }
- // <summary>
- // Loads a System.Object from a file. Uses binary formatter to deserialize.
- // </summary>
- // <param name="filename">
- // A <see cref="System.String"/>
- // </param>
- // <returns>
- // A <see cref="System.Object"/>
- // </returns>
- public static System.Object LoadFromFile(string filename) {
- Debug.Log("Load From file " + filename);
- BinaryFormatter formatter = new BinaryFormatter();
- string path = GetPath();
- DirectoryInfo saveDirectory = new DirectoryInfo(path);
- if(!saveDirectory.Exists) {
- return null;
- }
- //FileInfo saveFile = saveDirectory.GetFile(filename);
- FileInfo[] files = saveDirectory.GetFiles("*.sav");
- string saveFile = null;
- foreach(FileInfo file in files) {
- if(file.Name == filename) {
- saveFile = file.FullName;
- break;
- }
- }
- if(saveFile == null) {
- Debug.LogWarning("Save file not found.");
- return null;
- }
- Stream stream = new FileStream(saveFile, FileMode.Open, FileAccess.Read, FileShare.None);
- if(stream == null) {
- return null;
- }
- if(stream.Length > 0) {
- System.Object data = formatter.Deserialize(stream);
- Debug.Log("Load successful!");
- return data;
- } else {
- Debug.LogWarning("Stream.Length = 0!");
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement