Newer
Older
HardPoint-Project-Abertay-University-Unity3D / Assets / Scripts / Systems / SaveSystem / SaveManager.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. using System;
  6. public class SaveManager : MonoBehaviour
  7. {
  8. string DirPath = Application.dataPath + "/Saves";
  9. public void CreateNewSave(string toSave, string fileName)
  10. {
  11. string filePath = Application.dataPath + "/Saves/" + fileName+".json";
  12. if (!Directory.Exists(DirPath))
  13. {
  14. Directory.CreateDirectory(Application.dataPath + "/Saves");
  15. }
  16. File.WriteAllText(filePath, toSave);
  17. }
  18. public bool HasSave(string fileName)
  19. {
  20. return File.Exists(DirPath + "/" + fileName + ".json");
  21. }
  22. public SaveStruct ReadSave(string fileName)
  23. {
  24. if (Directory.Exists(DirPath))
  25. {
  26. if (File.Exists(DirPath + "/" + fileName + ".json"))
  27. {
  28. string pathToSave = DirPath + "/" + fileName + ".json";
  29. StreamReader streamReader = new StreamReader(pathToSave);
  30. string temp = streamReader.ReadToEnd();
  31. SaveStruct json = JsonUtility.FromJson<SaveStruct>(temp);
  32. return json;
  33. }
  34. }
  35. Debug.LogError("Read Save Failed!");
  36. return new SaveStruct();
  37. }
  38. }
  39. [Serializable]
  40. public struct SaveStruct
  41. {
  42. public int currency;
  43. public List<ClassStats> classStats;
  44. public SaveStruct(int currency, List<ClassStats> classStats)
  45. {
  46. this.currency = currency;
  47. this.classStats = classStats;
  48. }
  49. }