Advertisement
evelynshilosky

SaveManager - Part 23

Nov 3rd, 2023
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.87 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using UnityEngine;
  7.  
  8. public class SaveManager : MonoBehaviour
  9. {
  10.     public static SaveManager Instance { get; set; }
  11.     private void Awake()
  12.     {
  13.         if (Instance != null && Instance != this)
  14.         {
  15.             Destroy(gameObject);
  16.         }
  17.         else
  18.         {
  19.             Instance = this;
  20.         }
  21.  
  22.         DontDestroyOnLoad(gameObject);
  23.  
  24.     }
  25.  
  26.     public bool isSavingToJson;
  27.  
  28.     #region || ---------- General Section ---------- ||
  29.  
  30.  
  31.     public void SaveGame()
  32.     {
  33.         AllGameData data = new AllGameData();
  34.  
  35.         data.playerData = GetPlayerData();
  36.  
  37.         SaveAllGameData(data);
  38.     }
  39.  
  40.     private PlayerData GetPlayerData()
  41.     {
  42.         float[] playerStats = new float[3];
  43.         playerStats[0] = PlayerState.Instance.currentHealth;
  44.         playerStats[1] = PlayerState.Instance.currentCalories ;
  45.         playerStats[2] = PlayerState.Instance.currentHydrationPercent;
  46.  
  47.         float[] playerPosAndRot = new float[6];
  48.         playerPosAndRot[0] = PlayerState.Instance.playerBody.transform.position.x;
  49.         playerPosAndRot[1] = PlayerState.Instance.playerBody.transform.position.y;
  50.         playerPosAndRot[2] = PlayerState.Instance.playerBody.transform.position.z;
  51.  
  52.         playerPosAndRot[3] = PlayerState.Instance.playerBody.transform.rotation.x;
  53.         playerPosAndRot[4] = PlayerState.Instance.playerBody.transform.rotation.y;
  54.         playerPosAndRot[5] = PlayerState.Instance.playerBody.transform.rotation.z;
  55.  
  56.         return new PlayerData(playerStats, playerPosAndRot);
  57.  
  58.  
  59.     }
  60.  
  61.     public void SaveAllGameData(AllGameData gameData)
  62.     {
  63.         if (isSavingToJson)
  64.         {
  65.             // SaveGameDataToJsonFile(gameData);
  66.         }
  67.         else
  68.         {
  69.             SaveGameDataToBinaryFile(gameData);
  70.         }
  71.     }
  72.    
  73.     #endregion
  74.  
  75.     #region || ---------- To Binary Section ---------- ||
  76.  
  77.     public void SaveGameDataToBinaryFile(AllGameData gameData)
  78.     {
  79.         BinaryFormatter formatter = new BinaryFormatter();
  80.  
  81.         string path = Application.persistentDataPath + "/save_game.bin";
  82.         FileStream stream = new FileStream(path, FileMode.Create);
  83.  
  84.         formatter.Serialize(stream, gameData);
  85.         stream.Close();
  86.  
  87.         print("Data saved to" + Application.persistentDataPath + "/save_game.bin");
  88.  
  89.     }
  90.  
  91.     public AllGameData LoadGameDataFromBinaryFile()
  92.     {
  93.         string path = Application.persistentDataPath + "/save_game.bin";
  94.  
  95.         if (File.Exists(path))
  96.         {
  97.             BinaryFormatter formatter = new BinaryFormatter();
  98.             FileStream stream = new FileStream(path, FileMode.Open);
  99.  
  100.             AllGameData data = formatter.Deserialize(stream) as AllGameData;
  101.             stream.Close();
  102.  
  103.             return data;
  104.         }
  105.         else
  106.         {
  107.             return null;
  108.         }
  109.     }
  110.  
  111.  
  112.  
  113.  
  114.     #endregion
  115.  
  116.  
  117.     #region || ---------- Settings Section ---------- ||
  118.  
  119.     #region || ---------- Volume Settings ---------- ||
  120.     [System.Serializable]
  121.     public class VolumeSettings
  122.     {
  123.         public float music;
  124.         public float effects;
  125.         public float master;
  126.     }
  127.  
  128.     public void SaveVolumeSettings(float _music, float _effects, float _master)
  129.     {
  130.         VolumeSettings volumeSettings = new VolumeSettings()
  131.         {
  132.             music = _music,
  133.             effects= _effects,
  134.             master = _master
  135.         };
  136.  
  137.         PlayerPrefs.SetString("Volume", JsonUtility.ToJson(volumeSettings));
  138.         PlayerPrefs.Save();
  139.  
  140.         print("Saved to Player Pref");
  141.     }
  142.  
  143.     public VolumeSettings LoadVolumeSettings()
  144.     {
  145.         return JsonUtility.FromJson<VolumeSettings>(PlayerPrefs.GetString("Volume"));
  146.     }
  147.     #endregion
  148.  
  149.  
  150.  
  151.     #endregion
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement