ManajemenInformatika_E31230007/Assets/Scripts/StoryManager/VN_SaveSystem.cs

81 lines
1.9 KiB
C#

using UnityEngine;
[System.Serializable]
public class SaveData
{
public int nodeID;
public int chapterPoints; // 🔥 WAJIB
}
public static class VN_SaveSystem
{
private static string GetKey(int slot)
{
return "save_" + slot;
}
// ================= SAVE =================
public static void Save(int nodeID, int chapterPoints, int slot = 1)
{
SaveData data = new SaveData();
data.nodeID = nodeID;
data.chapterPoints = chapterPoints;
string json = JsonUtility.ToJson(data);
PlayerPrefs.SetString(GetKey(slot), json);
PlayerPrefs.Save();
Debug.Log("Game Saved (Slot " + slot + ")");
}
// ================= LOAD =================
// ================= LOAD =================
public static SaveData Load(int slot = 1)
{
if (!PlayerPrefs.HasKey(GetKey(slot)))
{
Debug.Log("Belum ada save di slot " + slot);
return null;
}
string json = PlayerPrefs.GetString(GetKey(slot));
SaveData data = JsonUtility.FromJson<SaveData>(json);
return data;
}
// ================= CHECK SAVE =================
public static bool HasSave(int slot = 1)
{
return PlayerPrefs.HasKey(GetKey(slot));
}
// ================= DELETE SAVE =================
public static void DeleteSave(int slot = 1)
{
if (PlayerPrefs.HasKey(GetKey(slot)))
{
PlayerPrefs.DeleteKey(GetKey(slot));
Debug.Log("Save dihapus (Slot " + slot + ")");
}
}
// ================= DELETE ALL =================
public static void DeleteAllSave()
{
int maxSlot = 10;
for (int i = 1; i <= maxSlot; i++)
{
string key = "save_" + i;
if (PlayerPrefs.HasKey(key))
{
PlayerPrefs.DeleteKey(key);
}
}
PlayerPrefs.Save();
Debug.Log("SEMUA SAVE DIHAPUS (ALL SLOTS)");
}
}