跳转到主要内容

批量保存数据

用于一次性保存多个游戏数据的 API。

URL 确认

此 API 使用 service-storage-api.playnanoo.com 域名。

API 信息

  • URL: https://service-storage-api.playnanoo.com/storage/v20221001/save/multi
  • Method: PUT
  • 需要认证: 是
DeviceInfo 继承

此 API 的 Req 类继承自 DeviceInfo。DeviceInfo 的所有属性会自动包含。

请求参数

参数类型必填说明
storage_itemsstring必填要保存的数据列表(JSON 字符串)

StorageMultiSaveParam 结构(转换为 JSON 传递)

字段类型必填说明
ItemsStorageItems[]必填要保存的数据数组

StorageItems 结构

字段类型必填说明
StorageKeystring必填要保存的数据键
StorageValuestring必填要保存的数据值
IsPrivatebool必填是否私有

响应数据

Res 类

字段类型说明
Statusstring处理状态

Unity C# 实现

BaseResponse 类

所有 API 响应的基类。

public class BaseResponse
{
public string ErrorCode;
public string Message;
public string WithdrawalKey;
public string BlockKey;
}

字段说明:

  • ErrorCode:错误代码
  • Message:错误信息
  • WithdrawalKey:账号处于注销等待状态时,用于恢复的密钥(仅注销等待中的账号提供)
  • BlockKey:账号被封禁时提供的密钥(仅被封禁的账号提供)

StorageMultiSaveParam 类

[Serializable]
public class StorageMultiSaveParam
{
public List<StorageItems> Items = new List<StorageItems>();
}

[Serializable]
public class StorageItems
{
public string StorageKey;
public string StorageValue;
public bool IsPrivate;
}

批量保存数据类

using System;
using System.Collections;
using UnityEngine.Networking;

public class StorageMultiSave
{
static string path = "https://service-storage-api.playnanoo.com/storage/v20221001/save/multi";

[Serializable]
public class Req : DeviceInfo
{
public string storage_items; // StorageMultiSaveParam을 JSON으로 변환하여 전달

public IEnumerator Send(string storage_items, Action<Res> onSuccess, Action<BaseResponse> onError)
{
if (!string.IsNullOrEmpty(storage_items)) this.storage_items = storage_items;

yield return HttpClient.Send<Req, Res>(
UnityWebRequest.kHttpVerbPUT,
path,
requireToken: true,
body: this,
onSuccess: onSuccess,
onError: onError
);
}
}

[Serializable]
public class Res : BaseResponse
{
public string Status;
}
}

使用示例

public void SaveMultipleData()
{
StorageMultiSave.Req req = new StorageMultiSave.Req();

// 여러 데이터를 한 번에 저장
StorageMultiSaveParam param = new StorageMultiSaveParam();

param.Items.Add(new StorageItems
{
StorageKey = "player_level",
StorageValue = "10",
IsPrivate = true
});

param.Items.Add(new StorageItems
{
StorageKey = "player_gold",
StorageValue = "5000",
IsPrivate = false
});

param.Items.Add(new StorageItems
{
StorageKey = "player_inventory",
StorageValue = "[sword, armor]",
IsPrivate = false
});

string json = JsonUtility.ToJson(param);

StartCoroutine(req.Send(
storage_items: json,
onSuccess: res =>
{
Debug.Log("다중 데이터 저장 성공");
},
onError: error =>
{
Debug.LogError($"다중 데이터 저장 실패: [{error.ErrorCode}] {error.Message}");
}
));
}
高效保存

需要同时保存多个数据时,使用此 API 可以减少网络请求次数,更加高效。

事务

所有数据以原子方式保存。如果其中任何一个失败,则全部失败。