Created
July 7, 2024 08:39
-
-
Save shohan4556/4292a5ff66241ae2909010683a6c96e3 to your computer and use it in GitHub Desktop.
This file contains 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using System.IO; | |
using System.Runtime.Serialization.Formatters.Binary; | |
namespace Ludo | |
{ | |
public static class PixelLabDataManager | |
{ | |
private static string SessionDataLocation = ""; | |
private static Dictionary<string, int> themeDatas = new Dictionary<string, int>(); | |
public static void Init() | |
{ | |
string _saveFilename = "mygame"; | |
SessionDataLocation = Application.persistentDataPath + "/" + _saveFilename + "_sessionData.sav"; | |
themeDatas = LoadGameData(); | |
} | |
public static void SetThemeData(string _key, int _data) | |
{ | |
int tempData = 0; | |
themeDatas = LoadGameData(); | |
if (!themeDatas.TryGetValue(_key, out tempData)) | |
{ | |
themeDatas.Add(_key, _data); | |
} | |
else | |
{ | |
themeDatas[_key] = _data; | |
} | |
SaveGamedata(); | |
} | |
private static void SaveGamedata() | |
{ | |
if (themeDatas.Count > 0) | |
{ | |
Debug.Log("saving GameData"); | |
try | |
{ | |
Stream sessionFile = File.Open(SessionDataLocation, FileMode.Create, FileAccess.Write); | |
BinaryFormatter bf = new BinaryFormatter(); | |
bf.Serialize(sessionFile, themeDatas); | |
sessionFile.Flush(); | |
sessionFile.Close(); | |
Debug.Log("Gamedata Saved"); | |
} | |
catch (IOException exception) | |
{ | |
Debug.Log(exception.Message); | |
throw; | |
} | |
} | |
} | |
private static Dictionary<string, int> LoadGameData() | |
{ | |
//Debug.Log("Trying to read previously saved game data"); | |
Dictionary<string, int> _gameDatas = new Dictionary<string, int>(); | |
if (File.Exists(SessionDataLocation)) | |
{ | |
Stream sessionFile = File.Open(SessionDataLocation, FileMode.Open, FileAccess.Read); | |
BinaryFormatter bf = new BinaryFormatter(); | |
_gameDatas = (Dictionary<string, int>) bf.Deserialize(sessionFile); | |
sessionFile.Close(); | |
// Debug.Log("Previously saved gamedata found and restored"); | |
return _gameDatas; | |
} | |
// Debug.Log("No previously saved game data found. It will be a fresh start"); | |
return _gameDatas; | |
} | |
public static int GetThemeData(string _key, int _defaulData = -1) | |
{ | |
int data = 0; | |
themeDatas = LoadGameData(); | |
if (themeDatas.TryGetValue(_key, out data)) | |
{ | |
return data; | |
} | |
else | |
{ | |
return _defaulData; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment