保存数据
用于将玩家游戏数据保存到云端的 API。
URL 确认
此 API 使用 service-storage-api.playnanoo.com 域名。
API 信息
- URL:
https://service-storage-api.playnanoo.com/storage/v20221001/save - Method:
PUT - 需要认证: 是
DeviceInfo 继承
此 API 的 Req 类继承自 DeviceInfo。DeviceInfo 的所有属性会自动包含。
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| storage_key | string | 必填 | 要保存的数据键 |
| storage_value | string | 必填 | 要保存的数据值(可为 JSON 字符串) |
| isPrivate | string | 必填 | 数据访问限制设置(on/off)- 设置为 on 时阻止外部玩家访问数据 |
响应数据
Res 类
| 字段 | 类型 | 说明 |
|---|---|---|
| Status | string | 处理状态 |
Unity C# 实现
BaseResponse 类
所有 API 响应的基类。
public class BaseResponse
{
public string ErrorCode;
public string Message;
public string WithdrawalKey;
public string BlockKey;
}
字段说明:
ErrorCode:错误代码Message:错误信息WithdrawalKey:账号处于注销等待状态时,用于恢复的密钥(仅注销等待中的账号提供)BlockKey:账号被封禁时提供的密钥(仅被封禁的账号提供)
保存数据类
using System;
using System.Collections;
using UnityEngine.Networking;
public class StorageSave
{
static string path = "https://service-storage-api.playnanoo.com/storage/v20221001/save";
[Serializable]
public class Req : DeviceInfo
{
public string storage_key; // 저장할 데이터의 키
public string storage_value; // 저장할 데이터의 값
public string isPrivate; // 데이터 접근 제한 설정 시 외부 플레이어의 데이터 접근을 차단
public IEnumerator Send(string storage_key, string storage_value, bool isPrivate, Action<Res> onSuccess, Action<BaseResponse> onError)
{
if (!string.IsNullOrEmpty(storage_key)) this.storage_key = storage_key;
if (!string.IsNullOrEmpty(storage_value)) this.storage_value = storage_value;
this.isPrivate = isPrivate ? "on" : "off";
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 SaveGameData()
{
StorageSave.Req req = new StorageSave.Req();
// 게임 데이터를 JSON으로 직렬화
var gameData = new
{
level = 10,
score = 1000,
items = new[] { "sword", "shield", "potion" }
};
string jsonData = JsonUtility.ToJson(gameData);
StartCoroutine(req.Send(
storage_key: "player_game_data",
storage_value: jsonData,
isPrivate: false, // 공개 데이터로 저장
onSuccess: res =>
{
Debug.Log("데이터 저장 성공");
},
onError: error =>
{
Debug.LogError($"데이터 저장 실패: [{error.ErrorCode}] {error.Message}");
}
));
}
数据格式
storage_value 是字符串类型,因此建议将复杂数据结构序列化为 JSON 后保存。
自动保存
建议在游戏中的重要时间点(升级、获得道具等)保存游戏数据。请检查网络状态,并在保存失败时实现重试逻辑。