> ## 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.

# Title Data 快速入门

> 使用 GetTitleData 和 SetTitleData API 及 C# 代码示例，从游戏客户端和服务器读取和写入 PlayFab Title Data 键值对。

Title data 是一组纯文本键值对，用于在服务器上远程存储和管理游戏的配置数据。它使 title 范围的配置变量易于访问和组织，可以在客户端检索。

本快速入门介绍如何以编程方式创建和使用 title data。

这是一个重要主题，因为在服务器上远程存储游戏的配置数据（可以随时更改）是使用像 PlayFab 这样的服务的最基本原因之一。

## 获取 title data

### 从游戏客户端

使用 `PlayFabClientAPI` 中的 [GetTitleData](xref:titleid.playfabapi.com.client.title-widedatamanagement.gettitledata) 获取特定 title 的 KVP。以下代码示例显示所有 title data 的值。

```csharp theme={null}
public void ClientGetTitleData() {
    PlayFabClientAPI.GetTitleData(new GetTitleDataRequest(),
        result => {
            if(result.Data == null || !result.Data.ContainsKey("MonsterName")) Debug.Log("No MonsterName");
            else Debug.Log("MonsterName: "+result.Data["MonsterName"]);
        },
        error => {
            Debug.Log("Got error getting titleData:");
            Debug.Log(error.GenerateErrorReport());
        }
    );
}
```

### 从游戏服务器

使用 `PlayFabServerAPI` 中的 [GetTitleData](xref:titleid.playfabapi.com.server.title-widedatamanagement.gettitledata) 获取特定 Title 的 KVP。以下代码示例显示所有 Title Data 的值。

```csharp theme={null}
public void ServerGetTitleData() {
    PlayFabServerAPI.GetTitleData( new GetTitleDataRequest(),
        result => {
            if (result.Data == null || !result.Data.ContainsKey("MonsterName")) Debug.Log("No MonsterName");
            else Debug.Log("MonsterName: " + result.Data["MonsterName"]);
        },
        error => {
            Debug.Log("Got error getting titleData:");
            Debug.Log(error.GenerateErrorReport());
        });
}
```

## 设置 title data

title data 不太可能非常频繁地更改。在大多数情况下，你应该将 title data 用于在 title 生命周期中基本保持不变的静态数据。

你可以使用 Game Manager 或通过服务器 API 函数设置 title data。

### 使用 Game Manager 设置 title data

要使用 Game Manager 向 title 添加 Primary Title Data，请执行以下步骤。

1. 在 **Game Manager** 中打开 title。
2. 选择 **Content**，然后选择 **Title Data**。
3. 选择 **New Title Data**
4. 在新 title data 页面中，输入 **Key** 的值和 **Value** 的值。**Key** 和 **Value** 都以字符串形式存储。
5. 使用 **Save** 按钮保存新数据项。

<img src="https://mintcdn.com/microsoft-4404708b/3hg2JQs0m7qmDqay/images/playfab/live-service-management/game-configuration/titledata/tutorials/title-data-new-title-data.PNG?fit=max&auto=format&n=3hg2JQs0m7qmDqay&q=85&s=2aad92647840e49665b006d43d2b6128" alt="Screenshot shows new title data." width="2965" height="1053" data-path="images/playfab/live-service-management/game-configuration/titledata/tutorials/title-data-new-title-data.PNG" />

<img src="https://mintcdn.com/microsoft-4404708b/3hg2JQs0m7qmDqay/images/playfab/live-service-management/game-configuration/titledata/tutorials/title-data-add-primary-title-data.PNG?fit=max&auto=format&n=3hg2JQs0m7qmDqay&q=85&s=e4d36dac8aaf57c54f680f7bae73be7e" alt="Screenshot of Title data" width="3000" height="1001" data-path="images/playfab/live-service-management/game-configuration/titledata/tutorials/title-data-add-primary-title-data.PNG" />

### 通过在 C# 中调用服务器 API 设置 title data

[SetTitleData](xref:titleid.playfabapi.com.server.title-widedatamanagement.settitledata) API 是一个服务器 API，你必须从专用服务器调用。每次调用 `SetTitleData` 时只能设置一个 title data KVP。

```csharp theme={null}
public void SetTitleData() {
    PlayFabServerAPI.SetTitleData(
        new SetTitleDataRequest {
            Key = "MonsterName",
            Value = "Dorf"
        },
        result => Debug.Log("Set titleData successful"),
        error => {
            Debug.Log("Got error setting titleData:");
            Debug.Log(error.GenerateErrorReport());
        }
    );
}
```

### 使用 Game Manager 设置 title data 覆盖项

覆盖项也是一组 JSON 键值对，理想情况下应用于拥有 Primary Title Data 的键值对中所需的差异变化。差异变化可能包括与 Primary Title data 相比的键值对，如新的键值、现有键值的更改或键的删除（以 NULL 作为值的输入）。

要向主 title data 添加覆盖项，请执行以下步骤。

1. 在 **Game Manager** 中打开 title。
2. 选择 **Content**，然后选择 **Title Data**。
3. 选择 **New Override**。
4. 输入 Override Label，并输入 **Key** 的值和 **Value** 的值。Key 和 Value 都以字符串形式存储。
5. 使用 **Save** 按钮保存新的 Title Data Override。

<Note>
  目前，此键/值对覆盖集仅在使用 [Experiments](/services/playfab/live-service-management/game-configuration/experiments) 时生效。
</Note>

如果玩家属于包含 title data 覆盖项的实验变体，则会在服务器端自动应用覆盖项，并在客户端与 title data 一起返回。

<Note>
  Title Data Override 值可能需要长达一分钟才能刷新和持久化。
</Note>

<img src="https://mintcdn.com/microsoft-4404708b/3hg2JQs0m7qmDqay/images/playfab/live-service-management/game-configuration/titledata/tutorials/new-title-data-override.PNG?fit=max&auto=format&n=3hg2JQs0m7qmDqay&q=85&s=d0bf7381eb30bee016b63b5b8ae57eb0" alt="Screenshot shows new title data override." width="2955" height="1006" data-path="images/playfab/live-service-management/game-configuration/titledata/tutorials/new-title-data-override.PNG" />

<img src="https://mintcdn.com/microsoft-4404708b/3hg2JQs0m7qmDqay/images/playfab/live-service-management/game-configuration/titledata/tutorials/set-title-data-override.PNG?fit=max&auto=format&n=3hg2JQs0m7qmDqay&q=85&s=a4f1978c4749d06b4767a0cfaf64bbd5" alt="Screenshot shows how to set ne title data override." width="2999" height="974" data-path="images/playfab/live-service-management/game-configuration/titledata/tutorials/set-title-data-override.PNG" />

<img src="https://mintcdn.com/microsoft-4404708b/3hg2JQs0m7qmDqay/images/playfab/live-service-management/game-configuration/titledata/tutorials/title-data-landing.PNG?fit=max&auto=format&n=3hg2JQs0m7qmDqay&q=85&s=2655a7e1a34589e3c6f19590262d3426" alt="Screenshot shows title data and override." width="2969" height="1572" data-path="images/playfab/live-service-management/game-configuration/titledata/tutorials/title-data-landing.PNG" />

## 内部 title data

与 User Data 类似，title data 具有对客户端隐藏的内部存储。此数据也可以在 Game Manager 中设置，或通过服务器 API 设置。

### 通过在 C# 中调用服务器 API 获取内部 title data

```csharp theme={null}
public void GetTitleInternalData()
{
    PlayFabServerAPI.GetTitleInternalData( new GetTitleDataRequest(),
        result => {
            if (result.Data == null || !result.Data.ContainsKey("PlayFab")) Debug.Log("No PlayFab");
            else Debug.Log("PlayFab: " + result.Data["PlayFab"]);
        },
        error => {
            Debug.Log("Got error getting titleData:");
            Debug.Log(error.GenerateErrorReport());
        }
    );
}
```

### 通过在 C# 中调用服务器 API 设置内部 title data

```csharp theme={null}
public void SetTitleInternalData() {
    PlayFabServerAPI.SetTitleInternalData(
        new SetTitleDataRequest {
            Key = "PlayFab",
            Value = "{ \"Status\": \"Secretly Awesome\" }"
        },
        result => { Debug.Log("Set titleData successful"); },
        error =>
        {
            Debug.Log("Got error setting titleData:");
            Debug.Log(error.GenerateErrorReport());
        });
}
```

## 另请参阅

* [使用 Publisher Data](/services/playfab/live-service-management/game-configuration/titledata/using-publisher-data)
* [玩家数据快速入门](/services/playfab/player-progression/player-data/quickstart)
* [CloudScript 快速入门](/services/playfab/live-service-management/service-gateway/automation/cloudscript/quickstart)


## Related topics

- [玩家数据快速入门](/zh-CN/services/playfab/player-progression/player-data/quickstart.md)
- [使用 publisher data](/zh-CN/services/playfab/live-service-management/game-configuration/titledata/using-publisher-data.md)
- [快速入门](/zh-CN/services/playfab/economy-monetization/economy-v2/quickstart.md)
- [Title News 快速入门](/zh-CN/services/playfab/live-service-management/game-configuration/title-communications/news/quickstart.md)
- [计划任务快速入门](/zh-CN/services/playfab/data-analytics/acting-data/scheduled-tasks/quickstart.md)
