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

# 遊戲資料快速入門

> 使用 GetTitleData 和 SetTitleData API 從遊戲用戶端和伺服器讀取和寫入 PlayFab 遊戲資料索引鍵/值組，並提供 C# 程式碼範例。

遊戲資料是一組純文字索引鍵/值組，用於在伺服器上遠端儲存和管理遊戲的設定資料。它可讓遊戲層級的設定變數保持可存取且井然有序，並可在用戶端擷取。

本快速入門說明如何以程式設計方式建立和使用遊戲資料。

這是一個重要的主題，因為將遊戲的設定資料遠端儲存在伺服器上 (可以隨時變更)，是使用 PlayFab 這類服務最基本的原因之一。

## 取得遊戲資料

### 從遊戲用戶端

使用 `PlayFabClientAPI` 中的 [GetTitleData](xref:titleid.playfabapi.com.client.title-widedatamanagement.gettitledata) 取得特定遊戲的 KVP。下列程式碼範例會顯示所有遊戲資料的值。

```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) 取得特定遊戲的 KVP。下列程式碼範例會顯示所有遊戲資料的值。

```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());
        });
}
```

## 設定遊戲資料

遊戲資料不太可能非常頻繁地變更。在大多數情況下，您應該將遊戲資料用於在遊戲生命週期中大部分時間都不會變更的靜態資料。

您可以使用 Game Manager 或伺服器 API 函式設定遊戲資料。

### 使用 Game Manager 設定遊戲資料

若要使用 Game Manager 將主要遊戲資料新增至遊戲，請執行下列步驟。

1. 在 **Game Manager** 中開啟遊戲。
2. 選取 **Content**，然後選取 **Title Data**。
3. 選取 **New Title Data**
4. 在新遊戲資料頁面中，輸入 **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="螢幕擷取畫面顯示新的遊戲資料。" 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="遊戲資料的螢幕擷取畫面" width="3000" height="1001" data-path="images/playfab/live-service-management/game-configuration/titledata/tutorials/title-data-add-primary-title-data.PNG" />

### 在 C# 中呼叫伺服器 API 設定遊戲資料

[SetTitleData](xref:titleid.playfabapi.com.server.title-widedatamanagement.settitledata) API 是您必須從專用伺服器呼叫的伺服器 API。每次呼叫 `SetTitleData` 只能設定一個遊戲資料 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 設定遊戲資料覆寫

覆寫也是一組 JSON 索引鍵/值組，理想上應用於包含主要遊戲資料之索引鍵/值組所需的差異變更。與主要遊戲資料相比，差異變更可以包括新的索引鍵/值、現有索引鍵值的變更，或索引鍵的刪除 (以 NULL 作為值的輸入)。

若要將覆寫新增至主要遊戲資料，請執行下列步驟。

1. 在 **Game Manager** 中開啟遊戲。
2. 選取 **Content**，然後選取 **Title Data**。
3. 選取 **New Override**。
4. 輸入覆寫標籤，並輸入 **Key** 的值和 **Value** 的值。Key 和 Value 都會儲存為字串。
5. 使用 **Save** 按鈕儲存新的遊戲資料覆寫。

<Note>
  此覆寫的索引鍵/值組集合目前只有在使用[實驗](/zh-TW/services/playfab/live-service-management/game-configuration/experiments)時才會生效。
</Note>

如果玩家屬於包含遊戲資料覆寫的實驗變體，覆寫會在伺服器端自動套用，並在用戶端與遊戲資料一起傳回。

<Note>
  遊戲資料覆寫值最多可能需要一分鐘才能重新整理並保存。
</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="螢幕擷取畫面顯示新的遊戲資料覆寫。" 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="螢幕擷取畫面顯示如何設定新的遊戲資料覆寫。" 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="螢幕擷取畫面顯示遊戲資料和覆寫。" width="2969" height="1572" data-path="images/playfab/live-service-management/game-configuration/titledata/tutorials/title-data-landing.PNG" />

## 內部遊戲資料

與使用者資料類似，遊戲資料也有對用戶端隱藏的內部儲存。此資料也可以在 Game Manager 中或透過伺服器 API 設定。

### 在 C# 中呼叫伺服器 API 取得內部遊戲資料

```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 設定內部遊戲資料

```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());
        });
}
```

## 另請參閱

* [使用發行者資料](/zh-TW/services/playfab/live-service-management/game-configuration/titledata/using-publisher-data)
* [玩家資料快速入門](/zh-TW/services/playfab/player-progression/player-data/quickstart)
* [CloudScript 快速入門](/zh-TW/services/playfab/live-service-management/service-gateway/automation/cloudscript/quickstart)


## Related topics

- [遊戲資料](/zh-TW/services/playfab/live-service-management/game-configuration/titledata/index.md)
- [玩家資料快速入門](/zh-TW/services/playfab/player-progression/player-data/quickstart.md)
- [使用發行者資料](/zh-TW/services/playfab/live-service-management/game-configuration/titledata/using-publisher-data.md)
- [統計資料快速入門](/zh-TW/services/playfab/player-progression/statistics/quickstart-statistics.md)
- [遊戲新聞快速入門](/zh-TW/services/playfab/live-service-management/game-configuration/title-communications/news/quickstart.md)
