Add friend requests menu - Add friends - (Unity module)
What's on The Menu
In this tutorial, you will learn how to prepare the user interface (UI) you will use to display and handle sent or received friend requests. The UI files are available in the Resources section and consist of the following:
FriendRequestsMenuCanvas_Starter
: A UI menu to display received friend requests.- CS file:
Assets/Resources/Modules/FriendsEssentials/Scripts/UI/FriendRequestsMenuHandler_Starter.cs
- Prefab file:
Assets/Resources/Modules/FriendsEssentials/Prefabs/FriendRequest/FriendRequestsMenuCanvas_Starter.prefab
- CS file:
SentFriendRequestsMenuCanvas_Starter
: A UI menu to display sent friend requests.- CS file:
Assets/Resources/Modules/FriendsEssentials/Scripts/UI/SentFriendRequestsMenuHandler_Starter.cs
- Prefab file:
Assets/Resources/Modules/FriendsEssentials/Prefabs/SentFriendRequest/SentFriendRequestsMenuCanvas_Starter.prefab
- CS file:
FriendRequestsEntryComponent
: A UI component to display the item of the received friend request information such as display name, avatar, and action buttons.- CS file:
Assets/Resources/Modules/FriendsEssentials/Scripts/UI/FriendRequestsEntryHandler.cs
- Prefab file:
Assets/Resources/Modules/FriendsEssentials/Prefabs/FriendRequest/FriendRequestsEntryComponent.prefab
- CS file:
SentFriendRequestEntryComponent
: A UI component to display the item of the sent friend request information such as display name, avatar, and action buttons.- CS file:
Assets/Resources/Modules/FriendsEssentials/Scripts/UI/SentFriendRequestsEntryHandler.cs
- Prefab file:
Assets/Resources/Modules/FriendsEssentials/Prefabs/SentFriendRequest/SentFriendRequestsEntryComponent.prefab
- CS file:
Take a look at the prefabs of these UI menus for more detailed information.
Friend Requests Menu Canvas
Below is the preview of the FriendRequestsMenuCanvas_Starter.prefab
. This prefab contains the following panels:
- Default Panel: Displays default text messages when there are no friend requests.
- Loading Panel: Displays loading animation when the game is fetching friend requests.
- Loading Failed Panel: Displays a message when the game fails to fetch friend requests.
- Result Content Panel: Displays the list of incoming friend requests.
The components used in this prefab are declared in the FriendRequestsMenuHandler_Starter.cs
file. Below are the declarations of the components used by this prefab.
[Header("Friend Request Components"), SerializeField] private GameObject friendEntryPrefab;
[Header("View Panels"), SerializeField] private RectTransform defaultPanel;
[SerializeField] private RectTransform loadingPanel;
[SerializeField] private RectTransform loadingFailedPanel;
[SerializeField] private RectTransform resultContentPanel;
To change the state of the panels, you can set the value of the CurrentView
property in the FriendRequestsMenuHandler_Starter.cs
file. Below is the code snippet that shows how to change the state of the panels.
CurrentView = FriendRequestsView.Loading;
Sent Friend Requests Menu Canvas
Below is the preview of the SentFriendRequestMenuCanvas_Starter.prefab
. This prefab contains the following panels:
- Default Panel: Displays default text messages when there are no sent friend requests.
- Loading Panel: Displays loading animation when the game is fetching sent friend requests.
- Loading Failed Panel: Displays a message when the game fails to fetch sent friend requests.
- Result Content Panel: Displays the list of outgoing friend requests.
The components used in this prefab are declared in the SentFriendRequestsMenuHandler_Starter.cs
file. This prefab uses the following declarations of components:
[Header("Friend Request Components"), SerializeField] private GameObject friendEntryPrefab;
[Header("View Panels"), SerializeField] private RectTransform defaultPanel;
[SerializeField] private RectTransform loadingPanel;
[SerializeField] private RectTransform loadingFailedPanel;
[SerializeField] private RectTransform resultContentPanel;
To change the state of the panels, set the value of the CurrentView
property in the SentFriendRequestsMenuHandler_Starter.cs
file. The following code snippet shows how to change the state of the panels:
CurrentView = SentFriendRequestsView.Loading;
Friend Requests Entry Component
Below is the preview of FriendRequestsEntryComponent.prefab
which is used to display the item of the incoming friend request information such as display name, avatar, and action buttons. Used by the Friend Requests Menu Canvas.
The components used in this prefab are declared in the SentFriendRequestsEntryHandler.cs
file. Below are the declarations of those components:
[SerializeField] private Image friendImage;
[SerializeField] private TMP_Text friendName;
[SerializeField] private Button rejectButton;
[SerializeField] private Button acceptButton;
To manipulate those components, the script have these public getters:
public Image FriendImage => friendImage;
public TMP_Text FriendName => friendName;
public Button RejectButton => rejectButton;
public Button AcceptButton => acceptButton;
It also has a User ID attribute to identify which entry represents which user.
public string UserId { get; set; } = string.Empty;
Sent Friend Requests Entry Component
Below is the preview of SentFriendRequestsEntryComponent.prefab
which is used to display the item of the outgoing friend request information such as display name, avatar, and action buttons.
This prefab can be found in the SentFriendRequestsEntryHandler.cs
file. The declarations of the components used in this prefab are as below:
[SerializeField] private Image friendImage;
[SerializeField] private TMP_Text friendName;
[SerializeField] private Button cancelButton;
To manipulate those components, the script have these public getters:
public Image FriendImage => friendImage;
public TMP_Text FriendName => friendName;
public Button CancelButton => cancelButton;
It also has a User ID attribute to identify which entry represents which user.
public string UserId { get; set; } = string.Empty;
Ready the UI
In this section, you will learn how to prepare the UI for both the Friend Requests and Sent Friend Requests menus.
Open the
FriendRequestsMenuHandler_Starter.cs
file. You will see a function calledLoadIncomingFriendRequests
. Later on, you will implement this function to query all incoming friend requests.private void LoadIncomingFriendRequests()
{
// TODO: Implement Load Incoming Friend Requests here.
BytewarsLogger.LogWarning("The LoadIncomingFriendRequests method is not implemented yet");
}In the
FriendRequestsMenuHandler_Starter.cs
file, find theOnEnable
function. This function is called when the UI is enabled.private void OnEnable()
{
// TODO: Define Module Wrappers and Load Incoming Friend Requests here.
}Open the
SentFriendRequestsMenuHandler_Starter.cs
file. You will see a function calledLoadOutgoingFriendRequests
. Later on, you will implement this function to query all outgoing friend requests.private void LoadOutgoingFriendRequests()
{
// TODO: Implement Load Outgoing Friend Requests here.
BytewarsLogger.LogWarning("The LoadOutgoingFriendRequests method is not implemented yet");
}In the
SentFriendRequestsMenuHandler_Starter.cs
file, find theOnEnable
function. This function is called when the UI is enabled.private void OnEnable()
{
// TODO: Define Module Wrappers and Load Outgoing Friend Requests here.
}In the Unity Editor, go to
Assets/Resources/Modules/FriendsEssentials
, open theFriendsEssentialsAssetConfig
asset file and make sure theIs Starter Active
is set to true.Play the game in the Editor, log in, and go to Social > Friends Requests. If your implementation up to this point is successful, you will be able to open the Friend Requests menu and receive the following logs:
[FriendRequestsMenuHandler_Starter.cs] [LoadIncomingFriendRequests] [Warning] [79] - The LoadIncomingFriendRequests method is not implemented yet
Still while playing the game in the editor, go to Social > Sent Friend Requests. If your implementation up to this point is successful, you will be able to open the Sent Friend Requests menu and receive the following logs:
[SentFriendRequestsMenuHandler_Starter.cs] [LoadOutgoingFriendRequests] [Warning] [79] - The LoadOutgoingFriendRequests method is not implemented yet
Resources
- The files used in this tutorial are available in the Unity Byte Wars GitHub repository:
- Assets/Resources/Modules/FriendsEssentials/Prefabs/FriendRequests/FriendRequestsMenuCanvas_Starter.prefab
- Assets/Resources/Modules/FriendsEssentials/Scripts/UI/FriendRequestsMenuHandler_Starter.cs
- Assets/Resources/Modules/FriendsEssentials/Prefabs/FriendRequests/FriendRequestsEntryComponent.prefab
- Assets/Resources/Modules/FriendsEssentials/Scripts/UI/FriendRequestsEntryHandler.cs
- Assets/Resources/Modules/FriendsEssentials/Prefabs/SentFriendRequests/SentFriendRequestsMenuCanvas_Starter.prefab
- Assets/Resources/Modules/FriendsEssentials/Scripts/UI/SentFriendRequestsMenuHandler_Starter.cs
- Assets/Resources/Modules/FriendsEssentials/Prefabs/SentFriendRequest/SentFriendRequestsEntryComponent.prefab
- Assets/Resources/Modules/FriendsEssentials/Scripts/UI/SentFriendRequestsEntryHandler.cs