-
-
Save ElKoziRRo/2191b841c119a70106548cf14e689cb2 to your computer and use it in GitHub Desktop.
SaveGame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.IO; | |
| public class FileManager | |
| { | |
| /// <summary> | |
| /// Load File | |
| /// </summary> | |
| /// <typeparam name="T">Data Model Type</typeparam> | |
| /// <param name="filename">File Name</param> | |
| /// <returns>Instance</returns> | |
| public static T Load<T>(string filename) where T : new() | |
| { | |
| string filePath = Path.Combine(Application.persistentDataPath, filename); | |
| T output; | |
| if (File.Exists(filePath)) | |
| { | |
| string dataAsJson = File.ReadAllText(filePath); | |
| output = JsonUtility.FromJson<T>(dataAsJson); | |
| } | |
| else | |
| { | |
| output = new T(); | |
| } | |
| return output; | |
| } | |
| /// <summary> | |
| /// Save File | |
| /// </summary> | |
| /// <typeparam name="T">Model Type</typeparam> | |
| /// <param name="filename">File Name</param> | |
| /// <param name="content">Model Content</param> | |
| public static void Save<T>(string filename, T content) | |
| { | |
| string filePath = Path.Combine(Application.persistentDataPath, filename); | |
| string dataAsJson = JsonUtility.ToJson(content); | |
| File.WriteAllText(filePath, dataAsJson); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| public class SaveGame | |
| { | |
| //serialized | |
| public string PlayerName = "Player"; | |
| public int XP = 0; | |
| private static string _gameDataFileName = "data.json"; | |
| private static SaveGame _instance; | |
| public static SaveGame Instance | |
| { | |
| get | |
| { | |
| if (_instance == null) | |
| Load(); | |
| return _instance; | |
| } | |
| } | |
| public static void Save() | |
| { | |
| FileManager.Save(_gameDataFileName, _instance); | |
| } | |
| public static void Load() | |
| { | |
| _instance = FileManager.Load<SaveGame>(_gameDataFileName); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment