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

# フレンド リスト

> Unity で PlayFab フレンド リストを構築するためのクイックスタート チュートリアル。フレンドの追加、削除、表示、および Steam、Facebook、XBOX のフレンドのリンクを扱います。

フレンド リストは、プレイヤー同士の交流機能を向上させる優れた機能です。扱いやすく、リーダーボードをユーザーにとってより魅力的なものにできます。

## 前提条件

### SDK: Unity

* タイトル ID が `PlayFabSharedSettings` オブジェクトに設定されている。
* プロジェクトからユーザーが正常にログインできる。
* タイトルに少なくとも 2 人の登録済みユーザーがいる。

## フレンドについて

タイトル内のどのプレイヤーも、タイトル内の他のプレイヤーとフレンドになれます。特に注意すべき点として、PlayFab のフレンド関係は一方向です。

**Albert** が **Bob** をフレンドとして追加した場合、**Bob** による承認プロセスは*ありません*。実際、**Bob** はそのことに気づいていない場合もあります。

フレンド関係を*相互*にするには、**Bob** が別途 **Albert** を追加する必要があります。相互関係のルールを持たせたい場合は、必要に応じてカスタム ゲーム サーバーまたは CloudScript ロジックでこれらの条件を強制することはタイトル側の責任となります。

プレイヤーが Steam、Facebook、XBOX Live のアカウントをリンクしている場合、それらのフレンドもあなたのタイトルをプレイしていれば、プラットフォーム固有のフレンドを表示することもできます。

## フレンドを作成する

サンプル コードでは、アプリの UI の代わりとして `DisplayFriends()` と `DisplayError(string error)` 関数を使用します。これらをエディターに貼り付ければ、追加の作業なしで動作させることができます。もしくは、呼び出しをご自身のコードに置き換えてください。

```csharp theme={null}
void DisplayFriends(List<FriendInfo> friendsCache) { friendsCache.ForEach(f => Debug.Log(f.FriendPlayFabId)); }
void DisplayPlayFabError(PlayFabError error) { Debug.Log(error.GenerateErrorReport()); }
void DisplayError(string error) { Debug.LogError(error); }
```

1. プレイヤーがログインすると、フレンド用の UI にアクセスできます。この機能には通常、少なくともフレンドの追加、削除、表示が含まれます。
2. プレイヤーの現在のフレンド リストを取得するには、[GetFriendsList](xref:titleid.playfabapi.com.client.friendlistmanagement.getfriendslist) API 呼び出しを使用します。

```csharp theme={null}
List<FriendInfo> _friends = null;

void GetFriends() {
    PlayFabClientAPI.GetFriendsList(new GetFriendsListRequest {
        IncludeSteamFriends = false,
        IncludeFacebookFriends = false,
        XboxToken = null
    }, result => {
        _friends = result.Friends;
        DisplayFriends(_friends); // triggers your UI
    }, DisplayPlayFabError);
}
```

[GetFriendsList](xref:titleid.playfabapi.com.client.friendlistmanagement.getfriendslist) の結果には、[FriendInfo](xref:titleid.playfabapi.com.client.friendlistmanagement.getfriendslist#friendinfo) オブジェクトのリストである friends パラメーターが含まれます。

3. プレイヤーのフレンド リストにフレンドを追加するには、[AddFriend](xref:titleid.playfabapi.com.client.friendlistmanagement.addfriend) API 呼び出しを使用します。

```csharp theme={null}
enum FriendIdType { PlayFabId, Username, Email, DisplayName };

void AddFriend(FriendIdType idType, string friendId) {
    var request = new AddFriendRequest();
    switch (idType) {
        case FriendIdType.PlayFabId:
            request.FriendPlayFabId = friendId;
            break;
        case FriendIdType.Username:
            request.FriendUsername = friendId;
            break;
        case FriendIdType.Email:
            request.FriendEmail = friendId;
            break;
        case FriendIdType.DisplayName:
            request.FriendTitleDisplayName = friendId;
            break;
    }
    // Execute request and update friends when we are done
    PlayFabClientAPI.AddFriend(request, result => {
        Debug.Log("Friend added successfully!");
    }, DisplayPlayFabError);
}
```

4. プレイヤーをフレンド リストから削除するには、[RemoveFriend](xref:titleid.playfabapi.com.client.friendlistmanagement.removefriend) API 呼び出しを使用します。

```csharp theme={null}
// unlike AddFriend, RemoveFriend only takes a PlayFab ID
// you can get this from the FriendInfo object under FriendPlayFabId
void RemoveFriend(FriendInfo friendInfo) {
    PlayFabClientAPI.RemoveFriend(new RemoveFriendRequest {
        FriendPlayFabId = friendInfo.FriendPlayFabId
    }, result => {
        _friends.Remove(friendInfo);
    }, DisplayPlayFabError);
}
```

## さらに進んだ使い方

フレンドに対しては、追加、削除、表示以外にも行えることがあります。

### フレンドにタグを付ける

[GetFriendsList](xref:titleid.playfabapi.com.client.friendlistmanagement.getfriendslist) から取得した [FriendInfo](xref:titleid.playfabapi.com.client.friendlistmanagement.getfriendslist#friendinfo) オブジェクトには、フレンドのタグのリストが含まれます。リストを更新する際は、このリストに追加または削除を行い、API 呼び出しに含めます (以下参照)。

```csharp theme={null}
// this REPLACES the list of tags on the server
// for updates, make sure this includes the original tag list
void SetFriendTags(FriendInfo friend, List<string> newTags)
{
    // update the tags with the edited list
    PlayFabClientAPI.SetFriendTags(new SetFriendTagsRequest
    {
        FriendPlayFabId = friend.FriendPlayFabId,
        Tags = newTags
    }, tagresult => {
        // Make sure to save new tags locally. That way you do not have to hard-update friendlist
        friend.Tags = newTags;
    }, DisplayPlayFabError);
}
```

タグは、マッチメイキングに情報を与える (たとえば、プレイヤーは高難易度で **2tuff** とタグ付けされたフレンドとプレイしたくない)、フレンド グループを実装する、あるいは単に関係に関連付ける必要のあるメタデータを保存するためなどに使用できます。

重要な注意点として、PlayFab は現在これらのタグを一切インデックス化しません。[GetFriendsList](xref:titleid.playfabapi.com.client.friendlistmanagement.getfriendslist) はタグに基づくフィルタリングはできないため、フィルタリングはローカルで行う必要があります。

このシステムによって生じる可能性のあるパフォーマンスへの影響を考慮する際には、この点を念頭に置いてください。


## Related topics

- [XBOX の要件](/ja-jp/services/playfab/multiplayer/networking/xbox-requirements.md)
- [PlayFab Party invitations and the security model](/ja-jp/services/playfab/multiplayer/networking/concepts-invitations-security-model.md)
- [XR-070 フレンド リスト](/ja-jp/publishing/certification/xr/xr-070.md)
- [フレンド](/ja-jp/services/playfab/community/associations/friends/index.md)
- [ピープル システム (フレンド リスト)](/ja-jp/services/xbox-services/community/people-system/live-people-system-nav.md)
