Put it all together - Search for players - (Unity module)
Connect the UI to search player and send friend request
Open
FindFriendsMenuHandler_Starter.cs
and add the following code:private FriendEssentialsWrapper_Starter _friendEssentialsWrapper;
To initiate the
_friendEssentialsWrapper
functionality at the start, add the following code withinStart()
:_friendEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<FriendEssentialsWrapper_Starter>();
The start function will be update as below:
void Start()
{
//Predefined 8a
_panels = new List<RectTransform>()
{
defaultPanel,
loadingPanel,
loadingFailedPanel,
loadingSuccessPanel,
};
backButton.onClick.AddListener(OnBackButtonClicked);
friendSearchBar.onEndEdit.AddListener(FindFriend);
_friendEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<FriendEssentialsWrapper_Starter>();
}In the
FindFriend
function, implement the function from the wrapper by adding this highlighted code:private void FindFriend(string displayName)
{
if (string.IsNullOrEmpty(friendSearchBar.text) || string.IsNullOrEmpty(friendSearchBar.text))
{
return;
}
ClearSearchPanel();
_friendEssentialsWrapper.GetUserByDisplayName(displayName, OnUsersRetrieved);
}Create the callback function called
OnUsersRetrieved
:private void OnUsersRetrieved(Result<PagedPublicUsersInfo> result)
{
if (result.Value.data.Length > 0)
{
//Generate user list along with status and send invitation button
GenerateFriendResult(result.Value.data.Length, result.Value);
}
}Define a function to send a friend invitation by adding the following code:
private void SendFriendInvitation(string userId)
{
_friendEssentialsWrapper.SendFriendRequest(userId, result => OnSendRequestComplete(result, userId));
}Create a callback to handle the data and user interface (UI) changes for every friend invitation sent.
private void OnSendRequestComplete(Result result, string userId)
{
_usersResult.TryGetValue(userId, out var tempFiendHandler);
if (!result.IsError)
{
if (tempFiendHandler != null)
{
tempFiendHandler.GetComponent<FriendResultPanelHandler>().sendInviteButton.interactable = false;
tempFiendHandler.GetComponent<FriendResultPanelHandler>().sendInviteButton.GetComponentInChildren<TMP_Text>().text = "Request Sent";
}
}
}To get the avatar from a player, create a function called
RetrievedUserAvatar
.private void RetrievedUserAvatar(string userId)
{
_friendEssentialsWrapper.GetUserAvatar(userId, result => OnGetAvatarComplete(result, userId));
}To handle the avatar data from the SDK wrapper, create a callback called
OnGetAvatarComplete
.private void OnGetAvatarComplete(Result<Texture2D> result, string userId)
{
if (!result.IsError)
{
if (result.Value != null)
{
_usersResult.TryGetValue(userId, out var tempFiendHandler);
if (tempFiendHandler != null)
{
tempFiendHandler.GetComponent<FriendResultPanelHandler>().friendImage.sprite = Sprite.Create(result.Value,
new Rect(0f, 0f, result.Value.width, result.Value.height), Vector2.zero);
}
}
}
else
{
Debug.LogWarning($"{result.Error.Message}");
}
loadingPanel.gameObject.SetActive(false);
}To get the friendship status, create a function by adding the following code:
private void CheckFriendshipStatus(string userId)
{
_friendEssentialsWrapper.GetFriendshipStatus(userId, result => OnGetFriendshipStatusCompleted(userId, result));
}Create a callback to handle data when
CheckFriendshipStatus
is called.private void OnGetFriendshipStatusCompleted(string userId, Result<FriendshipStatus> result)
{
_usersResult.TryGetValue(userId, out var resultPanel);
if (!result.IsError)
{
if (resultPanel != null)
{
resultPanel.GetComponent<FriendResultPanelHandler>().friendStatus.text = "Not Friend";
if (result.Value.friendshipStatus == RelationshipStatusCode.Outgoing)
{
resultPanel.GetComponent<FriendResultPanelHandler>().friendStatus.text = "Waiting for response";
resultPanel.GetComponent<FriendResultPanelHandler>().sendInviteButton.GetComponentInChildren<TMP_Text>().text = "Request Sent";
resultPanel.GetComponent<FriendResultPanelHandler>().sendInviteButton.interactable = false;
}
if (result.Value.friendshipStatus == RelationshipStatusCode.Friend)
{
resultPanel.GetComponent<FriendResultPanelHandler>().friendStatus.text = "Already Friend";
resultPanel.GetComponent<FriendResultPanelHandler>().sendInviteButton.interactable = false;
}
if (result.Value.friendshipStatus == RelationshipStatusCode.Incoming)
{
resultPanel.GetComponent<FriendResultPanelHandler>().friendStatus.text = "Waiting for your response";
resultPanel.GetComponent<FriendResultPanelHandler>().sendInviteButton.interactable = false;
}
}
}
else
{
resultPanel.GetComponent<FriendResultPanelHandler>().friendStatus.text = "Error to get friendship status";
Debug.LogWarning($"{Logger.GetCurrentMethod()}");
}
}You need to create a function
GenerateFriendResult
to hold all the data from the search results. This function works by iterating the values fromOnUsersRetrieved
and instantiatingFriendResultComponent.prefab
to show the user avatar, friendship status, and the send invite button.private void GenerateFriendResult(int length, PagedPublicUsersInfo publicUsersInfo)
{
CurrentView = FindFriendsView.LoadSuccess;
loadingPanel.gameObject.SetActive(true);
for (int i = 0; i < length; i++)
{
var userId = publicUsersInfo.data[i].userId;
if (userId == _friendEssentialsWrapper.PlayerUserId)
{
Debug.LogWarning(_friendEssentialsWrapper.PlayerUserId);
continue;
}
var resultPanel = Instantiate(loadingSuccessPanel, Vector3.zero, Quaternion.identity, friendListContent);
resultPanel.name = $"{userId}";
_usersResult.TryAdd(publicUsersInfo.data[i].userId, resultPanel);
resultPanel.GetComponent<FriendResultPanelHandler>().friendName.text = publicUsersInfo.data[i].displayName;
resultPanel.GetComponent<FriendResultPanelHandler>().sendInviteButton.onClick.AddListener(() => SendFriendInvitation(userId));
CheckFriendshipStatus(userId);
RetrievedUserAvatar(userId);
}
loadingSuccessPanel.gameObject.SetActive(false);
}
Resources
- The files used in this tutorial are available in the Unity Byte Wars GitHub repository.
- Assets/Resources/Modules/FriendEssentials/Scripts/UI/FindFriendsMenuHandler_Starter.cs
- Assets/Resources/Modules/FriendEssentials/Prefabs/FindUser/FindFriendsMenuCanvas_Starter.prefab
- Assets/Resources/Modules/FriendEssentials/Prefabs/FindUser/FriendEntryComponent.prefab
- Assets/Resources/Modules/FriendEssentials/Scripts/UI/FriendResultPanelHandler.cs