Unity Save Systems: Roll Your Own Vs. Asset Store Plugins

Evaluating Needs and Goals for Save System

When starting a new Unity project, one key decision is determining what type of save system is needed to persist game state. This depends heavily on the scope and complexity of your game mechanics that need saving and restoring. For basic games like 2D platformers, saving basic player state like position, health, and items may be sufficient. However, for large open-world RPGs, you may need to design a robust system that can serialize complex object graphs with thousands of runtime-created items, NPCs, and other dynamically changing state.

Other factors to consider are whether you need to encrypt save data to prevent cheating or hacking of save files, as well as whether you need to support saving data across multiple platforms like PC, mobile, and gaming consoles. Planning these requirements upfront will inform your decision when choosing between writing a fully custom save system versus using an off-the-shelf solution from the Unity Asset Store.

Writing a Custom Save System

Serializing Data to XML, JSON, Binary

If creating your own save system in Unity, you have several serialization formats to choose from when persisting game state to files or other permanent storage on the device. Some common options are:

  • XML: Human-readable but produces verbose files
  • JSON: Lightweight and easy to parse
  • Binary: Small file size but not human-readable

For example, serializing data to XML would look like:

[SerializeField]
private Vector3 playerPosition;

private void SaveToXML() {
  XMLEncoder encoder = new XMLEncoder(stream);
  encoder.Serialize(playerPosition);
  encoder.Close();
}

Encrypting Saved Data

For save games that store sensitive data like player inventory or progress that could be exploited if accessed directly, you may want to encrypt the data before writing to disk and decrypt when restoring state.

A simple approach is to use cryptographic libraries to securely hash save data:

private string EncryptData(string data) {
  using(MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) {
    byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(data));
    return Convert.ToBase64String(keys); 
  }
}

Managing Multiple Save Slots

Most games require maintaining separate save files so player can keep multiple persistent states. This requires uniquely naming and managing access to these files.

For example:

private void SaveToSlot(int slot) {
  
  string fileName = "save_" + slot + ".dat"; 
  
  using(FileStream stream = File.Open(fileName, FileMode.Create)) {
    // Serialize data
  }
}  

Asset Store Alternatives

Top Plugins for Save Systems

Rather than building a save system completely from scratch, developers can save significant time by utilizing pre-made solutions from the Unity Asset Store. Some of the most popular options include:

  • Easy Save – Robust all-in-one solution for serializing, encrypting, and managing save data.
  • Odin Serializer – Very fast serializer that integrates well with Unity’s serialization.
  • Save Game Free – Solid basic save system handling multiple slots and common data types.

Benefits of Pre-Built Solutions

Using a ready-made save system asset provides a number of advantages over writing everything from scratch:

  • Handles tedious tasks like serialization and encryption for you.
  • Built-in support for multiple platforms.
  • Mature solutions that have been battle-tested across many games.
  • Active maintenance and support from developers.

Downsides to Consider

However, there are some potential downsides with asset store plugins to keep in mind:

  • Learning curve if you need heavy customization
  • Overkill for simple save requirements
  • Potential runtime performance overhead

Choosing the Right System

Custom vs Asset Store Based on Project Needs

So when should you roll your own save system versus buying one off the shelf? Here are some guidelines:

  • Use custom if you have very simple serialized state and don’t need encryption or multiplatform support.
  • Leverage existing assets if you need advanced serialization of complex data and encryption.
  • Write custom adapter/wrapper code if you like parts of a plugin but not the entire system.

Hybrid Approach

A balanced approach is to use an asset store plugin for robust underlying serialization/encryption, but customize the exposed API for interacting with save data to meet your game’s unique needs.

For example:

// Wrapper for accessing Easy Save backend
public class MySaveSystem {

  private EasySave _backend;

  public void SavePlayerData(Player player) {
    _backend.SerializeAndEncrypt(player); 
  }

}

This allows combining the strengths of both custom and pre-built code.

Leave a Reply

Your email address will not be published. Required fields are marked *