Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Dynamic Save System for Unity

Overview

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.

Features

  • 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.

File Structure

/Assets/
  /DSS/
    DynamicSavingSystem
    

How to Use

1. Saving Data

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);

2. Loading Data

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}");
}

3. Saving Custom Objects

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}");

4. Saving Unity-Specific Data (Vector3, Color, etc.)

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();

5. Debug Mode

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.

File Location

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.

Future Improvements

  • Automatic Saving System: Secure saved data.
  • Listing Save Files: Handle outdated save files gracefully.

License

This project is open-source and can be modified as needed.

About

[Unity]Dynamic Saving system. Saves all kind of data.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages