Storage API
Storage API
在设备本地存储中保存/查询数据。
参考: Storage 由 SDK 内部管理,当应用切换到后台或关闭时会自动同步数据。
异步 API
SetStorage(key, data, onSuccess, onFail)
将数据保存到本地存储。
参数:
| 参数 | 类型 | 必须 | 说明 |
|---|---|---|---|
key | string | ✅ | 要保存的键 |
data | string | ✅ | 要保存的数据 |
onSuccess | Action<SetStorageResult> | - | 成功回调 |
onFail | Action<string> | - | 失败回调 |
TudadaSDK.Instance.SetStorage("playerName", "张三",
onSuccess: (result) => Debug.Log("保存完成"),
onFail: (err) => Debug.LogError("保存失败: " + err)
);
GetStorage(key, onSuccess, onFail)
从本地存储中查询数据。
参数:
| 参数 | 类型 | 必须 | 说明 |
|---|---|---|---|
key | string | ✅ | 要查询的键 |
onSuccess | Action<GetStorageResult> | - | 成功回调 |
onFail | Action<string> | - | 失败回调 |
成功响应 (GetStorageResult):
| 字段 | 类型 | 说明 |
|---|---|---|
data | string | 查询到的数据 |
TudadaSDK.Instance.GetStorage("playerName",
onSuccess: (result) => Debug.Log("姓名: " + result.data),
onFail: (err) => Debug.LogError("查询失败: " + err)
);
RemoveStorage(key, onSuccess, onFail)
从本地存储中删除数据。
参数:
| 参数 | 类型 | 必须 | 说明 |
|---|---|---|---|
key | string | ✅ | 要删除的键 |
onSuccess | Action<RemoveStorageResult> | - | 成功回调 |
onFail | Action<string> | - | 失败回调 |
TudadaSDK.Instance.RemoveStorage("tempData",
onSuccess: (result) => Debug.Log("删除完成")
);
ClearStorage(onSuccess, onFail)
删除本地存储中的所有数据。
参数:
| 参数 | 类型 | 必须 | 说明 |
|---|---|---|---|
onSuccess | Action<ClearStorageResult> | - | 成功回调 |
onFail | Action<string> | - | 失败回调 |
TudadaSDK.Instance.ClearStorage(
onSuccess: (result) => Debug.Log("全部删除完成")
);
同步 API
SetStorageSync(key, data)
同步地将数据保存到本地存储。
参数:
| 参数 | 类型 | 必须 | 说明 |
|---|---|---|---|
key | string | ✅ | 要保存的键 |
data | string | ✅ | 要保存的数据 |
TudadaSDK.Instance.SetStorageSync("score", "1000");
GetStorageSync(key)
同步地从本地存储中查询数据。
参数:
| 参数 | 类型 | 必须 | 说明 |
|---|---|---|---|
key | string | ✅ | 要查询的键 |
返回值 (string):
返回查询到的数据字符串。
string score = TudadaSDK.Instance.GetStorageSync("score");
Debug.Log("分数: " + score);
RemoveStorageSync(key)
同步地从本地存储中删除数据。
参数:
| 参数 | 类型 | 必须 | 说明 |
|---|---|---|---|
key | string | ✅ | 要删除的键 |
TudadaSDK.Instance.RemoveStorageSync("tempData");
ClearStorageSync()
同步地删除本地存储中的所有数据。
TudadaSDK.Instance.ClearStorageSync();
GetStorageInfoSync()
同步地查询存储使用信息。
返回值 (StorageInfo):
| 字段 | 类型 | 说明 |
|---|---|---|
keys | string[] | 已保存的键列表 |
currentSize | int | 已用容量 (KB) |
limitSize | int | 容量限制 (KB) |
StorageInfo info = TudadaSDK.Instance.GetStorageInfoSync();
Debug.Log("已保存的键数: " + info.keys.Length);
Debug.Log("已用容量: " + info.currentSize + " KB");
Debug.Log("容量限制: " + info.limitSize + " KB");