ESave
ESave
ESave
  • ESave Documentation
  • Pro Comparison
  • 🕹️Getting Started
    • Installation
    • Example Scenes
    • Components
      • Save File Setup
      • Save Storage
  • 📽️Videos
    • Tutorial
  • 📄Scripting
    • Saving & Loading
      • Saving
      • Loading
      • Custom Serializable Objects
      • Example Script
    • Runtime Save Creation
      • Creating a Save File
      • Infinite Saves
        • Infinite Save Scripting
    • Runtime Save Deletion
    • Understanding Background Save & Load
    • Using the Save Storage
  • 🛠️Support
    • Getting Help
  • 📚Changelogs
    • Latest Releases
  • ⭐Rate Me?
Powered by GitBook
On this page
  • Unencrypted Save
  • Encrypted Save
  • 1. Store the AES Key and IV
  • 2. Create Setup Data
  • 3. Create the Save File
  • Background Save File
  1. Scripting
  2. Runtime Save Creation

Creating a Save File

PreviousRuntime Save CreationNextInfinite Saves

Last updated 1 month ago

Skip this process with .

We know that you can create a save file using the component. Alternatively, you can create a save file through code.

Unencrypted Save

A save file needs save file setup data to be created. The code below creates save file setup data.

SaveFileSetupData saveFileSetupData = new SaveFileSetupData()
{
    fileName = "Save File",
    saveLocation = SaveLocation.PersistentDataPath,
    filePath = "Example/Path", // The path AFTER the persistent data path (can be left empty)
    fileType = FileType.Json,
    encryptionMethod = EncryptionMethod.None,
    addToStorage = true
};

SaveFile saveFile = new SaveFile(saveFileSetupData);

However, this code will not create an encrypted save file.

Encrypted Save

The only supported encryption method at the time was AES. To create an AES-encrypted save file, we would need an AES key and IV. These are used for the AES algorithm. Both the AES key and IV are just a string of random alphanumeric characters. It is recommended that they be at least 16 characters in length.

1. Store the AES Key and IV

The key and IV of a save file must not change, as the same key and IV are required during both encryption and decryption. So, we should create constants that we will use every time.

The key must be 16, 24, or 32 characters in length. The IV must be 16 characters in length.

private const string aesKey = "randomkey1234567";
private const string aesIV = "randomiv12345678";

Optional Alternative

If you'd rather not write a random string, you have the option of using key and IV generation methods—however, these methods generate a new key and IV each time.

string key = ESaveEncryption.GenerateKey().ToBase64String();
string iv = ESaveEncryption.GenerateIV().ToBase64String();

You can come up with your own solution for reusing them. One way would be to debug the string in the console, copy it, and then paste it in a string variable.

2. Create Setup Data

This will be similar to the previous one, except we will change the encryption method to AES and provide the key and IV.

SaveFileSetupData saveFileSetupData = new SaveFileSetupData()
{
    fileName = "Save File",
    saveLocation = SaveLocation.PersistentDataPath,
    filePath = "Example/Path",
    fileType = FileType.Json,
    encryptionMethod = EncryptionMethod.AES,
    aesKey = aesKey,
    aesIV = aesIV,
    addToStorage = true
};

3. Create the Save File

After using the code below, you have successfully created an encrypted save file.

SaveFile saveFile = new SaveFile(saveFileSetupData);

Background Save File

To create a save file that will save and load data in the background, set the backgroundTask value to true.

SaveFileSetupData saveFileSetupData = new SaveFileSetupData()
{
    fileName = "Save File",
    saveLocation = SaveLocation.PersistentDataPath,
    filePath = "Example/Path",
    fileType = FileType.Json,
    encryptionMethod = EncryptionMethod.None,
    addToStorage = true,
    backgroundTask = true
};

SaveFile saveFile = new SaveFile(saveFileSetupData);

You can set this value at any time during runtime, and the save file will use a background or main thread accordingly.

saveFile.backgroundTask = false;
📄
ESave Pro
Save File Setup