Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. using UnityEngine;
  2. using System.IO;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4.  
  5. public static class SaveSystem {
  6.  
  7. public static void CheckDirectory() {
  8. if(!Directory.Exists(Application.dataPath + "/Saves/")) {
  9. Directory.CreateDirectory (Application.dataPath + "/Saves/");
  10. }
  11. }
  12.  
  13. public static void SaveProgress (SaveGame save) {
  14. CheckDirectory ();
  15. BinaryFormatter formatter = new BinaryFormatter ();
  16. string path = Application.dataPath + "/Saves/player.save";
  17. FileStream stream = new FileStream (path, FileMode.Create);
  18. Progress data = new Progress (save);
  19. formatter.Serialize (stream, data);
  20. stream.Close ();
  21. }
  22.  
  23. public static void Delete (string pathPart) {
  24. string path = Application.dataPath + "/Saves/" + pathPart + ".save";
  25. File.Delete(path);
  26. }
  27.  
  28. public static Progress LoadProgress() {
  29. string path = Application.dataPath + "/Saves/player.save";
  30. if (File.Exists (path)) {
  31. BinaryFormatter formatter = new BinaryFormatter ();
  32. FileStream stream = new FileStream (path, FileMode.Open);
  33. Progress data = formatter.Deserialize (stream) as Progress;
  34. stream.Close ();
  35. return data;
  36. } else {
  37. Debug.LogError ("Save file for progress not found in " + path);
  38. return null;
  39. }
  40. }
  41.  
  42. public static void SaveSettings (Menu menu) {
  43. CheckDirectory ();
  44. BinaryFormatter formatter = new BinaryFormatter ();
  45. string path = Application.dataPath + "/Saves/settings.save";
  46. FileStream stream = new FileStream (path, FileMode.Create);
  47.  
  48. Settings data = new Settings (menu);
  49. formatter.Serialize (stream, data);
  50. stream.Close ();
  51. }
  52.  
  53. public static Settings LoadSettings () {
  54. string path = Application.dataPath + "/Saves/settings.save";
  55. if (File.Exists (path)) {
  56. BinaryFormatter formatter = new BinaryFormatter ();
  57. FileStream stream = new FileStream (path, FileMode.Open);
  58. Settings data = formatter.Deserialize (stream) as Settings;
  59. stream.Close ();
  60. return data;
  61. } else {
  62. Debug.LogError ("Save file for settings not found in " + path);
  63. return null;
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement