Advertisement
Dieton

JsonManager

May 18th, 2023 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | Gaming | 0 0
  1. /*
  2. By: Nathan Rumsey
  3. Engineer 3d llc
  4. 2022
  5. */
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using System.IO;
  10.  
  11. public static class JsonManager
  12. {
  13.     public static string directory = "/JasonSave/";
  14.     public static string fileName = "saveFile.json";
  15.     //string dataType =
  16.  
  17.     private static string GetFullDataPath()
  18.     {
  19.         return Application.dataPath + directory + fileName;//dataPath persistentDataPath streamingAssetsPath
  20.     }
  21.  
  22.     public static bool SaveCheck()
  23.     {
  24.         string dir = GetFullDataPath();
  25.         //Debug.Log(File.Exists(dir));
  26.         //Debug.Log(Directory.Exists(dir));
  27.         if (File.Exists(dir))
  28.         {
  29.             //Debug.Log("file is there");
  30.             return true;
  31.         }
  32.         else
  33.         {
  34.             //Debug.Log("file not there");
  35.             return false;
  36.         }
  37.     }
  38.  
  39.     public static void Save(GameData sd)
  40.     {
  41.         //Debug.Log(sd);
  42.         string dir = Application.dataPath + directory;
  43.  
  44.         if (!Directory.Exists(dir))
  45.             Directory.CreateDirectory(dir);
  46.  
  47.         string json = JsonUtility.ToJson(sd, true);
  48.         File.WriteAllText(dir + fileName, json);
  49.         //Debug.Log("Save Action Finished");
  50.     }
  51.  
  52.     public static GameData Load()
  53.     {
  54.         string fullPath = GetFullDataPath();
  55.         GameData so;
  56.  
  57.         string json = File.ReadAllText(fullPath);
  58.         so = JsonUtility.FromJson<GameData>(json);
  59.         //Debug.Log("File Loaded");
  60.  
  61.         //return null;
  62.         return so;
  63.     }
  64.  
  65.  
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement