RiftMayhem/Assets/Scripts/SaveData/GameStatePersistenceManager.cs
Pedro Gomes 9c136817b3 Updates to rogue-lite mechanics (WIP)
- visual update on riftraids
- ability unlock system through ability tomes
- (possible) bugfix sprite issue
- player reputation level
2025-01-18 23:14:12 +00:00

79 lines
2.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameStatePersistenceManager : MonoBehaviour
{
#region Singleton
private static GameStatePersistenceManager _instance;
// Public reference to the singleton instance
public static GameStatePersistenceManager Instance
{
get
{
// If the instance doesn't exist, try to find it in the scene
if (_instance == null)
{
_instance = FindObjectOfType<GameStatePersistenceManager>();
// If it's still null, create a new GameObject and add the component
if (_instance == null)
{
GameObject singletonObject = new GameObject(typeof(GameStatePersistenceManager).Name);
_instance = singletonObject.AddComponent<GameStatePersistenceManager>();
}
}
return _instance;
}
}
#endregion
protected void Awake()
{
// Ensure there's only one instance
if (_instance != null && _instance != this)
{
Destroy(gameObject);
return;
}
// If this is the first instance, set it as the singleton
_instance = this;
DontDestroyOnLoad(gameObject);
}
public void SaveData(string key, object value)
{
string jsonData = JsonUtility.ToJson(value);
Debug.Log($"Saving: {key} = {jsonData}");
PlayerPrefs.SetString(key, jsonData);
}
public bool HasDataForKey(string key)
{
return PlayerPrefs.HasKey(key);
}
public T LoadData<T>(string key)
{
string jsonData = PlayerPrefs.GetString(key);
Debug.Log($"Loading: {key} = {jsonData}");
return JsonUtility.FromJson<T>(jsonData);
}
public void SaveProgressDataCheckPoint(string key, int value)
{
PlayerPrefs.SetInt(key, value);
}
public int LoadProgressDataCheckPoint(string key)
{
return PlayerPrefs.GetInt(key, 0);
}
}