Advertisement
KTVX94

SaveManager

Sep 25th, 2020
1,844
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using System.IO;
  6.  
  7. public class SaveManager : MonoBehaviour
  8. {
  9.     public static SaveManager instance;
  10.     SaveFile svFile;
  11.  
  12.     private void Awake()
  13.     {
  14.         if (instance == null)
  15.             instance = this;
  16.     }
  17.  
  18.     private void OnApplicationQuit()
  19.     {
  20.         ClearTemp();
  21.     }
  22.  
  23.     public void SetGMData(GameManager mgr)
  24.     {
  25.         svFile.gmDat = new GameData(mgr);
  26.     }
  27.  
  28.     public void SetPlayerData(Player plyr)
  29.     {
  30.         svFile.plrDat = new PlayerData(plyr);
  31.     }
  32.  
  33.     public GameData GetGMData()
  34.     {
  35.         if (!svFile.written)
  36.         {
  37.             Debug.Log("Save File had no Game info");
  38.             return null;
  39.         }
  40.         else return svFile.gmDat;
  41.     }
  42.  
  43.     public PlayerData GetPlayerData()
  44.     {
  45.         if (!svFile.written)
  46.         {
  47.             Debug.Log("Save File had no Player info");
  48.             return null;
  49.         }
  50.         else return svFile.plrDat;
  51.     }
  52.  
  53.     public void Save(bool level)
  54.     {
  55.         /*
  56.         Debug.Log("Save was called");
  57.         svFile.written = true;
  58.  
  59.         string folder = Application.dataPath + "/SaveData";
  60.         string path = folder;
  61.         if (level) path += "/Gamestate.temp";
  62.         else path += "/ Save.sav";
  63.         BinaryFormatter bf = new BinaryFormatter();
  64.         if(!Directory.Exists(folder)) Directory.CreateDirectory(folder);
  65.         FileStream file = File.Create(path);
  66.         bf.Serialize(file, svFile);
  67.         file.Close();
  68.         file.Dispose();
  69.         */
  70.     }
  71.  
  72.     public void Load(bool level)
  73.     {
  74.         /*
  75.         Debug.Log("Tried to load");
  76.         string folder = Application.dataPath + "/SaveData";
  77.         string path = folder;
  78.         if (level) path += "/Gamestate.temp";
  79.         else path += "/Save.sav";
  80.         BinaryFormatter bf = new BinaryFormatter();
  81.         if (File.Exists(path))
  82.         {
  83.             FileStream file = File.Open(path, FileMode.Open);
  84.             SaveFile data = (SaveFile)bf.Deserialize(file);
  85.             file.Close();
  86.             file.Dispose();
  87.  
  88.             if(data != null)
  89.                 svFile = data;
  90.         }
  91.  
  92.         if (svFile == null)
  93.         {
  94.             svFile = new SaveFile();
  95.             svFile.written = false;
  96.         }
  97.  
  98.         if (level)
  99.         {
  100.             if (File.Exists(path + ".meta")) File.Delete(path + ".meta");
  101.             File.Delete(path);
  102.         }
  103.         */
  104.  
  105.         svFile = new SaveFile();
  106.     }
  107.  
  108.     public void ClearTemp()
  109.     {
  110.         //string path = Application.dataPath + "/SaveData/Gamestate.temp";
  111.         //if (File.Exists(path + ".meta")) File.Delete(path + ".meta");
  112.         //if (File.Exists(path)) File.Delete(path);
  113.     }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement