メインコンテンツまでスキップ

SDK を使用してフレンドを管理する - フレンドを管理する - (Unity モジュール)

Last updated on February 4, 2026

注釈:本資料はAI技術を用いて翻訳されています。

ラッパーを開く

このチュートリアルでは、AccelByte Gaming Services (AGS) Game SDK を使用してフレンドを管理する方法を学びます。Byte Wars プロジェクトには、ManagingFriendsWrapper.cs という SDK ラッパーがあります。このラッパーには、フレンド管理に関連する機能が含まれています。このチュートリアルでは、そのラッパーのスターター版を使用して、お好みの機能を実装できます。

スターターパックの内容

このチュートリアルに従うには、ManagingFriendsWrapper_Starter というフレンド管理ラッパークラスのスターター版を見つけてください。

  • C# ファイル: Assets/Resources/Modules/Social/ManagingFriends/Scripts/ManagingFriendsWrapper_Starter.cs

また、ヘルパー定数変数と構造体を含む ManagingFriendsModels モデルクラスもあり、以下のファイルで定義されています。

  • C# ファイル: Assets/Resources/Modules/Social/ManagingFriends/Scripts/ManagingFriendsModels.cs

ManagingFriendsWrapper_Starter クラスにはいくつかの機能があります。

  • ユーザーのフレンドを取得する AGS Game SDK クラスは Lobby です。このコード行は ManagingFriendsWrapper_Starter.cs にあります。

    private static ApiClient ApiClient => AccelByteSDK.GetClientRegistry().GetApi();
    private static Lobby lobby;
  • Awake() 関数には、ApiClient.GetLobby() 関数に割り当てられた lobby の事前定義変数がすでに含まれています。

    void Awake()
    {
    lobby = ApiClient.GetLobby();
    }

ManagingFriendsModels クラスにはいくつかの機能があります。

  • OnPlayerBlockedOnPlayerUnfriended というデリゲートイベント。

    public static Action<string> OnPlayerBlocked = delegate { };
    public static Action<string> OnPlayerUnfriended = delegate { };

ブロックされたプレイヤーの取得を実装する

このセクションでは、ブロックされたプレイヤーのリストを取得する機能を実装します。この関数は、ユーザーによってブロックされたプレイヤーのリストを返します。

  1. ManagingFriendsWrapper_Starter.cs を開き、friendsEssentialsWrapper という FriendEssentialWrapper_Starter の変数定義を作成します。この参照は、キャッシュされたユーザー ID を更新して、他のスクリプトが適切に機能するようにするために必要です。

    private FriendsEssentialsWrapper_Starter friendsEssentialsWrapper;
  2. Start() 関数を更新して friendsEssentialsWrapper を初期化します。既存の実装を以下のコードに置き換えてください。

    private void Start()
    {
    friendsEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<FriendsEssentialsWrapper_Starter>();
    }
  3. ブロックされたプレイヤーのリストを取得するために、GetBlockedPlayers() という新しい関数を作成します。

    public void GetBlockedPlayers(ResultCallback<BlockedList> resultCallback)
    {
    lobby.GetListOfBlockedUser(result =>
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error to load blocked users, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    }
    else
    {
    BytewarsLogger.Log($"Success to load blocked users, total blocked users {result.Value.data.Length}");

    IEnumerable<string> blockedUserIds = result.Value.data.Select(user => user.blockedUserId);
    friendsEssentialsWrapper.CachedFriendUserIds.Add(blockedUserIds.ToArray());
    }

    resultCallback?.Invoke(result);
    });
    }

プレイヤーのブロックを実装する

このセクションでは、プレイヤーのブロック機能を実装します。この関数は、ユーザー ID によってプレイヤーをブロックします。この関数には FriendsEssentialsWrapper_Starter への参照が必要です。まだ行っていない場合は、ブロックされたプレイヤーの取得を実装するのステップ 1 - 2 を参照してください。

  1. ManagingFriendsWrapper_Starter.cs を開き、ユーザー ID によってプレイヤーをブロックするために BlockPlayer() という新しい関数を作成します。

    public void BlockPlayer(string userId, ResultCallback<BlockPlayerResponse> resultCallback)
    {
    lobby.BlockPlayer(userId, result =>
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error sending block player request, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    }
    else
    {
    BytewarsLogger.Log($"Successfully blocked player with user Id: {userId}");
    }

    resultCallback?.Invoke(result);
    });
    }

プレイヤーのブロック解除を実装する

このセクションでは、ブロック解除機能を実装します。この関数は、ユーザー ID によってプレイヤーのブロックを解除します。この関数には FriendsEssentialsWrapper_Starter への参照が必要です。まだ行っていない場合は、ブロックされたプレイヤーの取得を実装するのステップ 1 - 2 を参照してください。

  1. ManagingFriendsWrapper_Starter.cs を開き、ユーザー ID によってプレイヤーのブロックを解除するために UnblockPlayer() という新しい関数を作成します。

    public void UnblockPlayer(string userId, ResultCallback<UnblockPlayerResponse> resultCallback)
    {
    lobby.UnblockPlayer(userId, result =>
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error unblock a friend, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    }
    else
    {
    BytewarsLogger.Log($"Successfully unblocked player with user Id: {userId}");

    friendsEssentialsWrapper.CachedFriendUserIds.Remove(userId);
    }

    resultCallback?.Invoke(result);
    });
    }

フレンド解除を実装する

このセクションでは、フレンド解除機能を実装します。この関数は、プレイヤーのフレンドリストからフレンドを削除します。この関数には FriendsEssentialsWrapper_Starter への参照が必要です。まだ行っていない場合は、ブロックされたプレイヤーの取得を実装するのステップ 1 - 2 を参照してください。

  1. ManagingFriendsWrapper_Starter.cs を開き、ユーザー ID によってプレイヤーをフレンド解除するために Unfriend() という新しい関数を作成します。

    public void Unfriend(string userId, ResultCallback resultCallback)
    {
    lobby.Unfriend(userId, result =>
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error sending unfriend request, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    }
    else
    {
    BytewarsLogger.Log($"Successfully unfriended player with user Id: {userId}");

    friendsEssentialsWrapper.CachedFriendUserIds.Remove(userId);
    }

    resultCallback?.Invoke(result);
    });
    }

ブロックされたプレイヤーとフレンド解除をリッスンする

プレイヤーがブロックまたはブロック解除されると、OnPlayerBlocked イベントがトリガーされ、プレイヤーがフレンド解除されると、他のプレイヤーで OnUnfriend イベントがトリガーされます。ゲーム内でこれらのイベントをリッスンして、UI やデータを更新できます。

  1. ManagingFriendsWrapper_Starter.cs ファイルを開き、OnPlayerBlocked イベントをリッスンするために OnPlayerBlockedNotif() という関数を追加します。

    private static void OnPlayerBlockedNotif(Result<PlayerBlockedNotif> result)
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error retrieving player blocked notif, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");

    return;
    }

    BytewarsLogger.Log($"Player with user Id: {result.Value.userId} has been blocked");

    ManagingFriendsModels.OnPlayerBlocked?.Invoke(result.Value.userId);
    }
  2. OnUnfriend イベントをリッスンするために OnPlayerUnfriendNotif() という関数を作成します。

    private void OnPlayerUnfriendNotif(Result<Friend> result)
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error receiving unfriend notification, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    return;
    }

    BytewarsLogger.Log($"Unfriend from {result.Value.friendId}");

    ManagingFriendsModels.OnPlayerUnfriended?.Invoke(result.Value.friendId);
    }
  3. 次に、これらの関数を AGS Game SDK の実際のイベントにバインドする必要があります。以下のコードで Awake() 関数を更新します。

    private void Awake()
    {
    lobby = ApiClient.GetLobby();

    lobby.PlayerBlockedNotif += OnPlayerBlockedNotif;
    lobby.OnUnfriend += OnPlayerUnfriendNotif;
    }
  4. クリーンな破棄を確実にするために、これらの関数をアンバインドするように OnDestroy() を更新します。既存の実装を以下のコードに置き換えてください。

    private void OnDestroy()
    {
    lobby.PlayerBlockedNotif -= OnPlayerBlockedNotif;
    lobby.OnUnfriend -= OnPlayerUnfriendNotif;
    }

リソース