Skip to main content

Add friends management menu - Manage friends - (Unity module)

Last updated on December 13, 2024

What's on the menu

In this tutorial, you will learn how to prepare the user interface (UI) that you will use to display blocked players and the action buttons to unfriend, block, and unblock players. The UI menus are available in the Resources section and consists of the following files:

  • BlockedPlayersMenuCanvas_Starter: A UI menu to show the blocked player list.
    • CS file: /Assets/Resources/Modules/ManagingFriends/Scripts/UI/BlockedPlayersMenuHandler_Starter.cs
    • Prefab file: /Assets/Resources/Modules/ManagingFriends/Prefabs/BlockedPlayers/BlockedPlayersMenuCanvas_Starter.prefab
  • FriendDetailsMenuCanvas_Starter: A UI menu to display individual friend information.
    • CS file: /Assets/Resources/Modules/FriendsEssentials/Scripts/UI/FriendDetailsMenuHandler_Starter.cs
    • Prefab file: /Assets/Resources/Modules/FriendsEssentials/Prefabs/FriendDetails/FriendDetailsMenuCanvas_Starter.prefab
  • BlockedPlayerEntryComponent: A UI component to display the blocked friend information and an unblock button.
    • CS file: /Assets/Resources/Modules/ManagingFriends/Scripts/UI/BlockedPlayerEntryHandler.cs
    • Prefab file: /Assets/Resources/Modules/ManagingFriends/Prefabs/BlockedPlayers/BlockedPlayerEntryComponent.prefab

Blocked Player Canvas

Below is the preview of BlockedPlayersMenuCanvas_Starter.prefab. This prefab contains the following panels:

  • Default Panel: Displays default text messages when there are no blocked players.
  • Loading Panel: Displays loading animation when the game is fetching blocked players.
  • Loading Failed Panel: Displays a message when the game fails to fetch blocked players.
  • Result Content Panel: Displays the list of blocked players.

Blocked Player Canvas Unity Byte Wars manage friends

The BlockedPlayersMenuHandler_Starter.cs script has the following fields to store the UI references:

[Header("Blocked Players Component"), SerializeField] private GameObject playerEntryPrefab;
[Header("View Panels"), SerializeField] private RectTransform defaultPanel;
[SerializeField] private RectTransform loadingPanel;
[SerializeField] private RectTransform loadingFailedPanel;
[SerializeField] private RectTransform resultContentPanel;

Change the panel by setting the value of currentView attribute. For example:

currentView = BlockedFriendsView.Default;

It also has a function to clear the current displayed list:

private void ClearBlockedPlayers()
{
resultContentPanel.DestroyAllChildren();

blockedPlayers.Clear();
}

Friend Details Canvas

Below is the preview of the FriendDetailsMenuCanvas_Starter.prefab. This prefab displays friend information, such as display name, avatar, block button, and unfriend button.

Potential friend entry widget Unity Byte Wars manage friends

The FriendDetailsMenuHandler_Starter.cs sript has the following fields to store the UI references:

[Header("Friend Details"), SerializeField] private Image friendImage;
[SerializeField] private TMP_Text friendDisplayName;

[Header("Friend Components"), SerializeField] private Button blockButton;
[SerializeField] private Button unfriendButton;

public Image FriendImage => friendImage;
public TMP_Text FriendDisplayName => friendDisplayName;
public string UserId { get; set; } = string.Empty;

Blocked Players Entry Component

Below is the preview of the BlockedPlayerEntryComponent.prefab. This prefab displays friend information, such as display name, avatar, and send invite buttons.

Potential friend entry widget Unity Byte Wars manage friends

The FriendDetailsMenuHandler_Starter.cs script has the following public getters and property to allow other script manipulate what it shows:

public string UserId { get; set; } = string.Empty;

public Image FriendImage => friendImage;
public TMP_Text FriendName => friendName;
public Button UnblockButton => unblockButton;

Ready the UI

  1. Open the BlockedPlayersMenuHandler_Starter.cs file and find the following functions, which you will implement later on:

    • LoadBlockedPlayers(): You will implement this function to get the list of blocked players.

      private void LoadBlockedPlayers()
      {
      // TODO: Implement Load Blocked Players function here.
      BytewarsLogger.LogWarning("The LoadBlockedPlayers method is not implemented yet");
      }
    • OnLoadBlockedPlayersCompleted(): You will implement this function as the response to LoadBlockedPlayers().

      private void OnLoadBlockedPlayersCompleted(Result<BlockedList> result)
      {
      // TODO: Implement Load Blocked Players callback functions here.
      BytewarsLogger.LogWarning("The OnLoadBlockedPlayersCompleted method is not implemented yet");
      }
  2. Open FriendDetailsMenuHandler_Starter.cs and find the following functions:

    • BlockPlayer(): You will implement this function to enable blocking other players.

      private void BlockPlayer()
      {
      // TODO: Implement Block Player function here.
      BytewarsLogger.LogWarning("BlockPlayer is not yet implemented.");
      }
    • OnBlockPlayerComplete(): You will implement this as a response function to the BlockPlayer() function.

      private void OnBlockPlayerComplete(Result<BlockPlayerResponse> result)
      {
      // TODO: Implement OnBlockPlayerComplete function here.
      BytewarsLogger.LogWarning("OnBlockPlayerComplete is not yet implemented.");
      }
    • Unfriend(): You will implement this function to enable unfriending other players.

      private void Unfriend()
      {
      // TODO: Implement Unfriend function here.
      BytewarsLogger.LogWarning("Unfriend is not yet implemented.");
      }
    • OnUnfriendCompleted(): You will implement this as a response function to the Unfriend() function.

      private void OnUnfriendCompleted(Result result)
      {
      // TODO: Implement OnUnfriendCompleted function here.
      BytewarsLogger.LogWarning("OnUnfriendCompleted is not yet implemented.");
      }
  3. In Unity Editor, go to Assets/Resources/Modules/ManagingFriends. There, you will find an asset config called ManagingFriendsAssetConfig. Open it and enable Is Starter Active in the Inspector. This will activate the friends management Starter files.

    Activate Tutorial Module Asset Config starter mode Unity Byte Wars manage friends

  4. Play the game in the Editor. If the implementation is successful, you will be able to navigate to the friends management menus and see the associated logs in the Console.

    • For Social > Block Players, you will see the following log:

      [BlockedPlayersMenuHandler_Starter] [LoadBlockedPlayers] - The LoadBlockedPlayers method is not implemented yet
    • For Social > Friends > [click any friend entry] > Block, you will see the following log:

      [FriendDetailsMenuHandler_Starter] [Unfriend] - Unfriend is not yet implemented.
    • For Social > Friends > [click any friend entry] > Unfriend, you will be able to see the following log:

      [FriendDetailsMenuHandler_Starter] [BlockPlayer] - BlockPlayer is not yet implemented.

Resources