Skip to main content

Put it all together - Search for players - (Unity module)

Last updated on October 24, 2024

Connect the UI to search player and send friend request

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

    private FriendEssentialsWrapper_Starter _friendEssentialsWrapper;
  2. To initiate the _friendEssentialsWrapper functionality at the start, add the following code within Start():

    _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>();
    }
  3. 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);
    }
  4. 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);
    }
    }
  5. 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));
    }
  6. 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";
    }
    }
    }
  7. To get the avatar from a player, create a function called RetrievedUserAvatar.

    private void RetrievedUserAvatar(string userId)
    {
    _friendEssentialsWrapper.GetUserAvatar(userId, result => OnGetAvatarComplete(result, userId));
    }
  8. 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);
    }
  9. 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));
    }
  10. 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()}");
    }
    }
  11. You need to create a function GenerateFriendResult to hold all the data from the search results. This function works by iterating the values from OnUsersRetrieved and instantiating FriendResultComponent.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