Skip to main content

Put it all together - Manage friends - (Unity module)

Last updated on June 10, 2024

Connect the UI to block and unblock player

  1. Open FriendDetailsMenuHandler_Starter.cs and add field following code:

    private ManagingFriendsWrapper_Starter _managingFriendsWrapper;
  2. Add the following code to Start():

    _managingFriendsWrapper = TutorialModuleManager.Instance.GetModuleClass<ManagingFriendsWrapper_Starter>();
  3. Update the OnUnfriendClicked() by with the following code:

    _managingFriendsWrapper.Unfriend(UserID, OnUnfriendCompleted);
  4. Create OnUnfriendCompleted that be trigger after the unfriend process completes.

    private void OnUnfriendCompleted(Result result)
    {
    if (!result.IsError)
    {
    Debug.Log($"successfully unfriend {UserID}");
    }
    else
    {
    Debug.LogWarning($"Error OnUnfriendCompleted");
    }
    }
  5. To block a player, update OnBlockeClicked() by adding the following code:

    _managingFriendsWrapper.BlockPlayer(UserID, OnBlockPlayerComplete);
  6. Create a callback function called OnBlockPlayerComplete.

    private void OnBlockPlayerComplete(Result<BlockPlayerResponse> result)
    {
    if (!result.IsError)
    {
    Debug.Log($"successfully blocked {UserID}");
    }
    else
    {
    Debug.LogWarning($"Error OnBlockPlayerComplete");
    }
    }

Connect the UI to display blocked players and to unblock players

  1. Open BlockedPlayersMenuHandler_Starter.cs and add the following code:

    private ManagingFriendsWrapper_Starter _managingFriendsWrapper;
  2. Update Awake() by adding the following code:

    _managingFriendsWrapper = TutorialModuleManager.Instance.GetModuleClass<ManagingFriendsWrapper_Starter>();
  3. Update GetBlockedPlayers with the following code:

    private void GetBlockedPlayers()
    {
    CurrentView = BlockedFriendsView.Default;
    _managingFriendsWrapper.GetBlockedPlayers(OnLoadBlockedPlayersCompleted);
    }
  4. Create a callback function to update the UI and data once the friend request loading is finished.

    private void OnLoadBlockedPlayersCompleted(Result<BlockedList> result)
    {
    if (!result.IsError)
    {
    CurrentView = BlockedFriendsView.LoadingSuccess;
    if (result.Value.data.Length > 0)
    {
    UserInfo(result.Value);
    }
    else
    {
    CurrentView = BlockedFriendsView.Default;
    }
    }
    else
    {
    CurrentView = BlockedFriendsView.LoadingFailed;
    }
    }
  5. Update the OnUnblockFriend, this function serves as callback when we press the unblock button.

    private void OnUnblockFriend(string userId)
    {
    _managingFriendsWrapper.UnblockPlayer(userId, result => OnUnblockedCompleted(userId, result));
    }

Resources