> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 如何设置只读玩家数据

> 通过服务器 API 写入只读 PlayFab 玩家数据,以便客户端能够读取权威的进度值,但无法对其进行修改。

若要设置只读 KVP,你必须从服务器进程调用服务器 API [UpdateUserReadOnlyData](xref:titleid.playfabapi.com.server.playerdatamanagement.updateuserreadonlydata) 方法。这是服务器可以修改但客户端只能读取的数据。

本主题中的 C# 示例是为 Unity SDK 编写的。Unity SDK 使用事件驱动模型来处理非同步任务。若要使用 C# SDK 运行示例代码,必须修改代码以使用 async Task 模型。需要修改的方法在签名中的方法名末尾附加了 Async。例如,Unity SDK 中的 SetObject 在 C# SDK 中变为 SetObjectAsync。有关更多信息,请参阅[使用 async 和 await 的异步编程](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/)。

## C# 代码示例

以下 **C#** 代码示例使用 PlayFab 服务器 API 创建(或更新,如果 KVP 已存在)键为 `Father`、值为 `Fred`,键为 `Mother`、值为 `Alice`,键为 `Sister`、值为 `Lucy`,以及键为 `Brother`、值为 `Doug` 的 KVP。

```csharp theme={null}
public void UpdateUserReadOnlyData() {
    PlayFabServerAPI.UpdateUserReadOnlyData(new UpdateUserDataRequest() {
        PlayFabId = "user PlayFabId here - obtained from any successful LoginResult",
        Data = new Dictionary<string, string>() {
            {"Father", "Fred"},
            {"Mother", "Alice"},
            {"Sister", "Lucy"},
            {"Brother", "Doug"}
        },
        Permission = UserDataPermission.Public
    },
    result => Debug.Log("Set read-only user data successful"),
    error => {
        Debug.Log("Got error updating read-only user data:");
        Debug.Log(error.GenerateErrorReport());
    });
}
```

## 另请参阅

[如何获取只读玩家数据](/services/playfab/player-progression/player-data/how-to-get-read-only-player-data)


## Related topics

- [如何获取只读玩家数据](/zh-CN/services/playfab/player-progression/player-data/how-to-get-read-only-player-data.md)
- [玩家数据快速入门](/zh-CN/services/playfab/player-progression/player-data/quickstart.md)
- [如何设置内部玩家数据](/zh-CN/services/playfab/player-progression/player-data/how-to-set-internal-player-data.md)
- [如何获取内部玩家数据](/zh-CN/services/playfab/player-progression/player-data/how-to-get-internal-player-data.md)
- [通过 CloudScript 修改只读或内部玩家数据](/zh-CN/services/playfab/player-progression/player-data/how-to-modify-read-only-internal-player-data.md)
