This dynamic save system allows programmers to dynamically select which data they want to save. The system uses binary serialization to store data efficiently and supports multiple data types using a dictionary-based approach.
- Flexible Data Storage: Programmers can choose what to save.
- Supports Custom Objects: Save custom classes if marked
[Serializable]. - Type Saving: Saves Data Type when saving.
- Support for Unity Data Types: Convert Unity-specific types (like
Vector3) into serializable structures.
/Assets/
/DSS/
DynamicSavingSystem
To store data dynamically, use the Set method:
SaveData saveData = new SaveData();
saveData.Set("PlayerHealth", 100);
saveData.Set("Coins", 50);
saveData.Set("PlayerName", "Hero");
SaveSystem.Save(saveData);Retrieve stored data dynamically with the Get method:
SaveData loadedData = SaveSystem.Load();
int health = loadedData.Get<int>("PlayerHealth", 0);
int coins = loadedData.Get<int>("Coins", 0);
string playerName = loadedData.Get<string>("PlayerName", "Default");
Debug.Log($"Loaded: Health = {health}, Coins = {coins}, Name = {playerName}");OR
To ensure and in case of can not find data type, use GetSavedType
SaveData loadedData = SaveSystem.Load();
Type playerStatsType = loadedData.GetSavedType("PlayerStats");
Debug.Log($"Saved type: {playerStatsType}");
if (playerStatsType == typeof(PlayerStats))
{
PlayerStats stats = loadedData.Get<PlayerStats>("PlayerStats", new PlayerStats());
Debug.Log($"Level: {stats.level}, XP: {stats.experience}");
}If you want to store a custom class, ensure it is marked as [Serializable]:
[Serializable]
public class PlayerStats
{
public int level;
public float experience;
}Save a custom object:
saveData.Set("PlayerStats", new PlayerStats { level = 5, experience = 120.5f });
SaveSystem.Save(saveData);Load the custom object:
PlayerStats stats = loadedData.Get<PlayerStats>("PlayerStats", new PlayerStats());
Debug.Log($"Level: {stats.level}, XP: {stats.experience}");Since Unity's Vector3 is not serializable, convert it to a serializable struct:
[Serializable]
public struct SerializableVector3
{
public float x, y, z;
public SerializableVector3(Vector3 v) { x = v.x; y = v.y; z = v.z; }
public Vector3 ToVector3() => new Vector3(x, y, z);
}Save and load Unity data:
saveData.Set("PlayerPosition", new SerializableVector3(player.transform.position));
SerializableVector3 pos = loadedData.Get<SerializableVector3>("PlayerPosition", new SerializableVector3());
Vector3 playerPosition = pos.ToVector3();In every saving and loading, Debug.Log will be called to ensure the process is being held. In desire, by putting DebugMode in to false, debug log will no longer be there.
Save files are stored in:
Application.persistentDataPath + "/saveData.dat"
This ensures data is saved in a location appropriate for different platforms. Editing SaveFileName, allows to change save file path.
- Automatic Saving System: Secure saved data.
- Listing Save Files: Handle outdated save files gracefully.
This project is open-source and can be modified as needed.