Advertisement
Guest User

SaveManagerGhetto

a guest
Apr 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using System.IO;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7.  
  8. public class SaveManager : MonoBehaviour {
  9.  
  10.     //when is this called, onquite
  11.     public static void SaveCurrency(CurrencyManager currencyManager)
  12.     {
  13.         BinaryFormatter binaryFormatter = new BinaryFormatter();
  14.         FileStream fileStream = new FileStream(Application.persistentDataPath + "/player.dat", FileMode.Create);
  15.  
  16.         PlayerData playerData = new PlayerData();
  17.         binaryFormatter.Serialize(fileStream, playerData);
  18.         fileStream.Close();
  19.     }
  20.  
  21.     /*
  22.     public static int LoadCurrency()
  23.     {
  24.         if (File.Exists(Application.persistentDataPath + "/player.dat"))
  25.         {
  26.             BinaryFormatter binaryFormatter = new BinaryFormatter();
  27.             FileStream fileStream = new FileStream(Application.persistentDataPath + "/player.dat", FileMode.Open);
  28.  
  29.             PlayerData playerData = binaryFormatter.Deserialize(fileStream) as PlayerData;
  30.             fileStream.Close();
  31.             return playerData.currency;
  32.         }
  33.         else
  34.         {
  35.             Debug.LogError("File doesn't exist.");
  36.             return new int();
  37.         }
  38.     }
  39.     */
  40.  
  41.     public static PlayerData LoadData()
  42.     {
  43.         if (File.Exists(Application.persistentDataPath + "/player.dat"))
  44.         {
  45.             BinaryFormatter binaryFormatter = new BinaryFormatter();
  46.             FileStream fileStream = new FileStream(Application.persistentDataPath + "/player.dat", FileMode.Open);
  47.  
  48.             PlayerData playerData = binaryFormatter.Deserialize(fileStream) as PlayerData;
  49.             fileStream.Close();
  50.             return playerData;
  51.         }
  52.         else
  53.         {
  54.             Debug.LogError("File doesn't exist.");
  55.             return new PlayerData();
  56.         }
  57.     }
  58.  
  59.     public static void SaveData()
  60.     {
  61.         //this is god's light, yw
  62.         BinaryFormatter binaryFormatter = new BinaryFormatter();
  63.         FileStream fStream = new FileStream(Application.persistentDataPath + "/player.dat", FileMode.Create);
  64.  
  65.         PlayerData pData = new PlayerData();
  66.         binaryFormatter.Serialize(fStream, pData);
  67.         fStream.Close();
  68.     }
  69.        
  70.     [System.Serializable]
  71.     // this is helpful, allows us an abstract view of what the player has - need to use this
  72.     // to our advantage
  73.     public class PlayerData
  74.     {
  75.         public int currency;
  76.         public int hullLvl;
  77.         //let me try sth to see what it saves
  78.  
  79.         public PlayerData()
  80.         {
  81.             //dont stress too much over it now, just try to get the logic
  82.             hullLvl = UpgradeManager.hullLvlIndex;
  83.             currency = CurrencyManager.Currency;
  84.         }
  85.        
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement