云存档
按键存储每位玩家的 JSON 文档,并带有修订号,两台设备永远不会悄无声息地互相覆盖。
代码注释和 API 名称保留英文,以便与 SDK 保持一致。
保存#
将任何可序列化对象(Unity)或 JSON 值保存在某个键下。每次保存都会获得下一个修订号。
[System.Serializable]
public class Progress { public int level; public int coins; }
var saved = await Majacloud.CloudSave.SaveAsync("progress", new Progress { level = 3, coins = 120 });
if (saved.Ok) Debug.Log("Saved revision " + saved.Value.Revision);var saved := await Majacloud.cloud_save.save("progress", {"level": 3, "coins": 120})
if saved.ok:
print("Saved revision ", saved.value.revision)const saved = await mc.cloudSave.save('progress', { level: 3, coins: 120 });
if (saved.ok) console.log('Saved revision ' + saved.value.revision);curl -X PUT https://majacloud.majanite.com/v1/cloud/progress -H "X-Project-ID: $PROJECT_ID" -H "X-Client-Key: $CLIENT_KEY" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d '{"data": {"level": 3, "coins": 120}}'加载#
加载从未保存过的键会返回 not_found:请将其视为首次启动。
var loaded = await Majacloud.CloudSave.LoadAsync<Progress>("progress");
if (loaded.Ok) progress = loaded.Value.Data;
else if (loaded.IsNotFound) progress = new Progress(); // first launchvar loaded := await Majacloud.cloud_save.load_save("progress")
if loaded.ok:
progress = loaded.value.data
elif loaded.is_not_found():
progress = {} # first launchconst loaded = await mc.cloudSave.load('progress');
if (loaded.ok) progress = loaded.value.data;
else if (loaded.error.code === 'not_found') progress = {}; // first launchcurl https://majacloud.majanite.com/v1/cloud/progress -H "X-Project-ID: $PROJECT_ID" -H "X-Client-Key: $CLIENT_KEY" \
-H "Authorization: Bearer $TOKEN"修订号和冲突#
将加载时得到的修订号作为预期修订号传入。如果其他设备在此期间保存过,保存会失败并返回 revision_mismatch 和 details.current_revision;请重新加载、合并后再试。预期修订号为 0 表示仅创建。
// Only overwrite the revision we loaded; another device may have saved since
var safe = await Majacloud.CloudSave.SaveAsync("progress", progress, expectedRevision: loaded.Value.Revision);
if (!safe.Ok && safe.Error.Code == MajacloudErrorCode.RevisionMismatch)
{
// Load again, merge, and save with the new revision
}# Only overwrite the revision we loaded; another device may have saved since
var safe := await Majacloud.cloud_save.save("progress", progress, loaded.value.revision)
if safe.error == "revision_mismatch":
pass # load again (details.current_revision), merge, save// Only overwrite the revision we loaded; another device may have saved since
const safe = await mc.cloudSave.save('progress', progress, { expectedRevision: loaded.value.revision });
if (!safe.ok && safe.error.code === 'revision_mismatch') {
// load again (safe.error.details.current_revision), merge, save
}curl -X PUT https://majacloud.majanite.com/v1/cloud/progress -H "X-Project-ID: $PROJECT_ID" -H "X-Client-Key: $CLIENT_KEY" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d '{"data": {"level": 4}, "revision": 3}'
# 409 revision_mismatch → details.current_revision列出和删除#
列出玩家的键及其修订号和大小,并删除不再需要的键:
var keys = await Majacloud.CloudSave.ListAsync();
foreach (var entry in keys.Value) Debug.Log(entry.Key + " r" + entry.Revision + " " + entry.SizeBytes + " B");
await Majacloud.CloudSave.DeleteAsync("old_slot");var keys := await Majacloud.cloud_save.list_saves()
for entry in keys.value.keys:
print(entry.key, " r", entry.revision, " ", entry.size_bytes, " B")
await Majacloud.cloud_save.delete_save("old_slot")const keys = await mc.cloudSave.list();
for (const entry of keys.value.keys) console.log(entry.key, entry.revision, entry.size_bytes);
await mc.cloudSave.delete('old_slot');curl https://majacloud.majanite.com/v1/cloud -H "X-Project-ID: $PROJECT_ID" -H "X-Client-Key: $CLIENT_KEY" \
-H "Authorization: Bearer $TOKEN"
curl -X DELETE https://majacloud.majanite.com/v1/cloud/old_slot -H "X-Project-ID: $PROJECT_ID" -H "X-Client-Key: $CLIENT_KEY" \
-H "Authorization: Bearer $TOKEN"限制#
- 每个文档最多 256 KB,每位玩家最多 100 个键。
- 键可以包含字母、数字、点、短横线和下划线(最多 80 个字符)。
- 每个账号的总存储空间取决于套餐(参见限制);超出后保存会失败并返回
plan_limit,不会删除任何数据。