135 lines
3.6 KiB
C#
135 lines
3.6 KiB
C#
|
|
using UnityEngine;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
|
|||
|
|
public static class SaveSystem
|
|||
|
|
{
|
|||
|
|
// ============================================================
|
|||
|
|
// BASIC TYPES
|
|||
|
|
// ============================================================
|
|||
|
|
|
|||
|
|
public static void SaveInt(string key, int value)
|
|||
|
|
{
|
|||
|
|
PlayerPrefs.SetInt(key, value);
|
|||
|
|
PlayerPrefs.Save();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static int LoadInt(string key, int defaultValue = 0)
|
|||
|
|
{
|
|||
|
|
return PlayerPrefs.GetInt(key, defaultValue);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void SaveFloat(string key, float value)
|
|||
|
|
{
|
|||
|
|
PlayerPrefs.SetFloat(key, value);
|
|||
|
|
PlayerPrefs.Save();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static float LoadFloat(string key, float defaultValue = 0f)
|
|||
|
|
{
|
|||
|
|
return PlayerPrefs.GetFloat(key, defaultValue);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void SaveBool(string key, bool value)
|
|||
|
|
{
|
|||
|
|
PlayerPrefs.SetInt(key, value ? 1 : 0);
|
|||
|
|
PlayerPrefs.Save();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static bool LoadBool(string key, bool defaultValue = false)
|
|||
|
|
{
|
|||
|
|
return PlayerPrefs.GetInt(key, defaultValue ? 1 : 0) == 1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void SaveString(string key, string value)
|
|||
|
|
{
|
|||
|
|
PlayerPrefs.SetString(key, value);
|
|||
|
|
PlayerPrefs.Save();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static string LoadString(string key, string defaultValue = "")
|
|||
|
|
{
|
|||
|
|
return PlayerPrefs.GetString(key, defaultValue);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ============================================================
|
|||
|
|
// JSON LIST / ARRAY SAVE
|
|||
|
|
// ============================================================
|
|||
|
|
|
|||
|
|
public static void SaveList<T>(string key, List<T> list)
|
|||
|
|
{
|
|||
|
|
string json = JsonUtility.ToJson(new Wrapper<T>(list));
|
|||
|
|
PlayerPrefs.SetString(key, json);
|
|||
|
|
PlayerPrefs.Save();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static List<T> LoadList<T>(string key)
|
|||
|
|
{
|
|||
|
|
string json = PlayerPrefs.GetString(key, "");
|
|||
|
|
|
|||
|
|
if (string.IsNullOrEmpty(json))
|
|||
|
|
return new List<T>();
|
|||
|
|
|
|||
|
|
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
|
|||
|
|
return wrapper.items ?? new List<T>();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[System.Serializable]
|
|||
|
|
private class Wrapper<T>
|
|||
|
|
{
|
|||
|
|
public List<T> items;
|
|||
|
|
|
|||
|
|
public Wrapper(List<T> list)
|
|||
|
|
{
|
|||
|
|
items = list;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ============================================================
|
|||
|
|
// DELETE SYSTEM
|
|||
|
|
// ============================================================
|
|||
|
|
|
|||
|
|
public static void DeleteKey(string key)
|
|||
|
|
{
|
|||
|
|
PlayerPrefs.DeleteKey(key);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void DeleteAll()
|
|||
|
|
{
|
|||
|
|
PlayerPrefs.DeleteAll();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ============================================================
|
|||
|
|
// SAFETY BACKUP
|
|||
|
|
// ============================================================
|
|||
|
|
|
|||
|
|
public static void Backup(string key)
|
|||
|
|
{
|
|||
|
|
if (!PlayerPrefs.HasKey(key)) return;
|
|||
|
|
|
|||
|
|
string val = PlayerPrefs.GetString(key, "");
|
|||
|
|
PlayerPrefs.SetString(key + "_backup", val);
|
|||
|
|
PlayerPrefs.Save();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void RestoreBackup(string key)
|
|||
|
|
{
|
|||
|
|
if (!PlayerPrefs.HasKey(key + "_backup")) return;
|
|||
|
|
|
|||
|
|
string val = PlayerPrefs.GetString(key + "_backup", "");
|
|||
|
|
PlayerPrefs.SetString(key, val);
|
|||
|
|
PlayerPrefs.Save();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ============================================================
|
|||
|
|
// MANUAL SAVE (Performans İyileştirmesi)
|
|||
|
|
// ============================================================
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Tüm PlayerPrefs değişikliklerini tek seferde diske kaydeder.
|
|||
|
|
/// Çoklu set işlemlerinden sonra çağırılması önerilir.
|
|||
|
|
/// </summary>
|
|||
|
|
public static void Save()
|
|||
|
|
{
|
|||
|
|
PlayerPrefs.Save();
|
|||
|
|
}
|
|||
|
|
}
|