9. Cloud save
How saves are stored
Each player has a set of keys, each holding a string value. To save an object, turn it into text with JsonUtility.ToJson and back with JsonUtility.FromJson.
| Limit | Value |
|---|---|
| One key's value | 64 KB |
| One player's saves in total | 1 MB |
| Keys in one write | 20 |
Two devices, one save
Every key has a version, which goes up with each write. When you write, you can say which version you expect to replace:
expectedVersion |
The write happens |
|---|---|
-1 (the default) |
Always: overwrite |
0 |
Only if the key doesn't exist yet |
n |
Only if the key is still at version n, i.e. nobody wrote since you read it |
What happens on a mismatch:
- The write is refused with
Error=Conflict, and nothing is written. conflictsholds what is stored now.
That's how a phone and a tablet avoid silently overwriting each other: read, change, write with the version you read, and on a conflict merge or ask the player.
Script 9.1: Load and save progress
using System;
using System.Threading.Tasks;
using MultiConnect.App;
using UnityEngine;
[Serializable]
public class Progress
{
public int level = 1;
public int stars;
}
public static class ProgressSave
{
private const string Key = "progress";
private static int _loadedVersion;
public static async Task<Progress> Load()
{
var data = await McCloudSave.GetAsync(new[] { Key });
var item = data.Ok ? data.Value?.Get(Key) : null;
_loadedVersion = item?.version ?? 0;
return item == null ? new Progress() : JsonUtility.FromJson<Progress>(item.value);
}
public static async Task<bool> Save(Progress progress)
{
var result = await McCloudSave.SetAsync(Key, JsonUtility.ToJson(progress), _loadedVersion);
if (result.Ok && result.Value != null && result.Value.written.Count > 0)
{
_loadedVersion = result.Value.written[0].version;
return true;
}
if (result.Error == McAppError.Conflict)
Debug.Log("Saved from another device since this one loaded: load again and merge.");
else
Debug.Log("Not saved: " + result.Message);
return false;
}
}
Other calls:
McCloudSave.SetManyAsyncwrites up to 20 keys at once, all or nothing.McCloudSave.DeleteAsync(keys)removes keys.GetAsync()with no keys returns every key, plususedBytesandmaxBytes.
Server-only keys
Which keys they are: keys starting with the app's Server-only save prefix (default server., set under Sign-in and server access).
Who can write them:
- The client can read them but not write them.
- Only your server writes them, with
McAppServer.SetPlayerDataAsync(playerId, key, value).
Use them for anything a player must not edit on their device, such as an unlocked tier or a match history.
The server also has GetPlayerDataAsync, SetPlayerDataManyAsync and DeletePlayerDataAsync, for any player and any key.
You can view and edit any player's keys on their page under Players.

