跳到主要内容

Storage API

Storage API

在设备本地存储中保存/查询数据。

参考: Storage 由 SDK 内部管理,当应用切换到后台或关闭时会自动同步数据。

异步 API

SetStorage(key, data, onSuccess, onFail)

将数据保存到本地存储。

参数:

参数类型必须说明
keystring要保存的键
datastring要保存的数据
onSuccessAction<SetStorageResult>-成功回调
onFailAction<string>-失败回调
TudadaSDK.Instance.SetStorage("playerName", "张三",
onSuccess: (result) => Debug.Log("保存完成"),
onFail: (err) => Debug.LogError("保存失败: " + err)
);

GetStorage(key, onSuccess, onFail)

从本地存储中查询数据。

参数:

参数类型必须说明
keystring要查询的键
onSuccessAction<GetStorageResult>-成功回调
onFailAction<string>-失败回调

成功响应 (GetStorageResult):

字段类型说明
datastring查询到的数据
TudadaSDK.Instance.GetStorage("playerName",
onSuccess: (result) => Debug.Log("姓名: " + result.data),
onFail: (err) => Debug.LogError("查询失败: " + err)
);

RemoveStorage(key, onSuccess, onFail)

从本地存储中删除数据。

参数:

参数类型必须说明
keystring要删除的键
onSuccessAction<RemoveStorageResult>-成功回调
onFailAction<string>-失败回调
TudadaSDK.Instance.RemoveStorage("tempData",
onSuccess: (result) => Debug.Log("删除完成")
);

ClearStorage(onSuccess, onFail)

删除本地存储中的所有数据。

参数:

参数类型必须说明
onSuccessAction<ClearStorageResult>-成功回调
onFailAction<string>-失败回调
TudadaSDK.Instance.ClearStorage(
onSuccess: (result) => Debug.Log("全部删除完成")
);

同步 API

SetStorageSync(key, data)

同步地将数据保存到本地存储。

参数:

参数类型必须说明
keystring要保存的键
datastring要保存的数据
TudadaSDK.Instance.SetStorageSync("score", "1000");

GetStorageSync(key)

同步地从本地存储中查询数据。

参数:

参数类型必须说明
keystring要查询的键

返回值 (string):

返回查询到的数据字符串。

string score = TudadaSDK.Instance.GetStorageSync("score");
Debug.Log("分数: " + score);

RemoveStorageSync(key)

同步地从本地存储中删除数据。

参数:

参数类型必须说明
keystring要删除的键
TudadaSDK.Instance.RemoveStorageSync("tempData");

ClearStorageSync()

同步地删除本地存储中的所有数据。

TudadaSDK.Instance.ClearStorageSync();

GetStorageInfoSync()

同步地查询存储使用信息。

返回值 (StorageInfo):

字段类型说明
keysstring[]已保存的键列表
currentSizeint已用容量 (KB)
limitSizeint容量限制 (KB)
StorageInfo info = TudadaSDK.Instance.GetStorageInfoSync();
Debug.Log("已保存的键数: " + info.keys.Length);
Debug.Log("已用容量: " + info.currentSize + " KB");
Debug.Log("容量限制: " + info.limitSize + " KB");