メインコンテンツまでスキップ

すべてを統合する - フレンドを追加する - (Unity モジ ュール)

Last updated on May 30, 2024

Connect the UI to display sent and received friend request list, accept, decline, and cancel friend request

This section will cover both FriendRequestMenuHandler_Starter.cs for showing incoming friend request and SentFriendRequestMenuHandler_Starter.cs for showing outgoing friend request.

  1. Open FriendRequestMenuHandler_Starter.cs and SentFriendRequestMenuHandler_Starter.cs and add a field that will hold the reference to the SDK wrapper.

    private FriendEssentialsWrapper_Starter _friendEssentialsWrapper;
  2. Update the Awake function in each file with the following code:

    private void Awake()
    {
    _friendEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<FriendEssentialsWrapper_Starter>();
    }
  3. Go to OnEnable() in each file and update it with the following code:

    private void OnEnable()
    {
    if(_friendEssentialsWrapper==null)
    _friendEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<FriendEssentialsWrapper_Starter>();
    GetFriendRequest();
    }
  4. In each file, update the GetFriendRequest function that you already created with the following code:

    private void GetFriendRequest()
    {
    if (_friendEssentialsWrapper == null) return;
    _friendEssentialsWrapper.LoadIncomingFriendRequests(OnLoadIncomingFriendRequestsCompleted);
    }
  5. In each file, to update the friend list based on the invoked event, you will create a function called UpdateFriendList. This function will be attached to the event action in the Start function.

    private void UpdateFriendList()
    {
    if (transform.gameObject.activeSelf)
    {
    ClearFriendRequestList();
    GetFriendRequest();
    }
    }
  6. In each file, update Start() to listen to the action events from the FriendEssentialsWrapper_Starter class and invoke the UpdateFriendList function whenever an event is triggered.

    void Start()
    {
    _panels = new List<RectTransform>()
    {
    defaultPanel,
    loadingPanel,
    loadingSuccessPanel,
    loadingFailedPanel
    };

    backButton.onClick.AddListener(MenuManager.Instance.OnBackPressed);
    FriendEssentialsWrapper_Starter.OnIncomingAdded += UpdateFriendList;
    }
  7. In each file, create a callback function to update the user interface (UI) and the data once the friend request loading is finished.

    private void OnLoadIncomingFriendRequestsCompleted(Result<Friends> result)
    {
    if (!result.IsError)
    {
    CurrentView = FriendRequestsView.LoadingSuccess;
    if (result.Value.friendsId.Length > 0)
    {
    UserInfo(result.Value);
    }
    else
    {
    CurrentView = FriendRequestsView.Default;
    }
    }
    else
    {
    CurrentView = FriendRequestsView.LoadingFailed;
    }
    }
  8. You have declared the UserInfo function without actually creating the corresponding function. To address this, in each file, create the necessary function that retrieves user information in bulk.

    private void UserInfo(Friends friends)
    {
    _friendEssentialsWrapper.GetBulkUserInfo(friends.friendsId, OnGetBulkUserInfoCompleted);
    }
  9. In each file, create a callback function that will handle the UI update after the GetBulkUserInfo loads successfully.

    private void OnGetBulkUserInfoCompleted(Result<ListBulkUserInfoResponse> result)
    {
    if (!result.IsError)
    {
    GenerateEntryResult(result.Value);
    }
    }
  10. In each file, create a function to generate the Friend Entry component. This component will display the avatar, username, and a button based on the list of user information retrieved earlier.

    private void GenerateEntryResult(ListBulkUserInfoResponse friends)
    {
    var friendRequestComponent = loadingSuccessPanel.GetChild(0);
    Debug.Log(friendRequestComponent.name);
    foreach (var baseUserInfo in friends.data)
    {
    var resultComponent = Instantiate(friendRequestComponent, Vector3.zero, Quaternion.identity, loadingSuccessPanel);
    resultComponent.gameObject.SetActive(true);
    resultComponent.gameObject.name = baseUserInfo.userId;
    var friendEntryComponent = resultComponent.GetComponentInChildren<FriendRequestsEntryHandler>();
    friendEntryComponent.friendName.text = String.IsNullOrEmpty(baseUserInfo.displayName) ? "Bytewars Player Headless" : baseUserInfo.displayName;
    friendEntryComponent.acceptButton.onClick.AddListener(() =>
    {
    OnAcceptFriend(baseUserInfo.userId);
    });
    friendEntryComponent.declineButton.onClick.AddListener(() =>
    {
    OnDeclineFriend(baseUserInfo.userId);
    });
    _friendRequest.TryAdd(baseUserInfo.userId, (RectTransform)resultComponent);
    RetrieveAvatar(baseUserInfo.userId);
    }
    }
  11. In the previous step, you might have noticed you declared OnAcceptFriend and OnCancelRequest, but haven't yet created the functions. Those functions are to hold the functionalities to accept and reject friend requests. In FriendRequestMenuHandler_Starter.cs, create both functions with the following code:

    private void OnDeclineFriend(string userId)
    {
    _friendEssentialsWrapper.DeclineFriend(userId, result => OnAcceptOrDecline(userId, result));
    }

    private void OnAcceptFriend(string userId)
    {
    _friendEssentialsWrapper.AcceptFriend(userId, result => OnAcceptOrDecline(userId, result));
    }
  12. To handle the result from OnAcceptFriend and OnCancelRequest, create a callback function that will trigger if those functions.

    private void OnAcceptOrDecline(string userId, Result result)
    {
    if (!result.IsError)
    {
    var target = GameObject.Find(userId);
    Destroy(target);
    }
    }
  13. In SentFriendRequestMenuHandler_Starter.cs, you will notice that you have already declared OnCancelRequest. Create a function for this using following code:

    private void OnCancelRequest(string userId)
    {
    _friendEssentialsWrapper.CancelFriendRequests(userId, result => OnCancelRequestCompleted(userId, result));
    }
  14. Still in SentFriendRequestMenuHandler_Starter.cs, create a callback for _friendEssentialsWrapper.CancelFriendRequests. This callback will update the UI if a player cancels the outgoing friend request.

    private void OnCancelRequestCompleted(string userId, Result result)
    {
    if (!result.IsError)
    {
    var target = GameObject.Find(userId);
    Destroy(target);
    }
    }
  15. Create the RetrieveAvatar function for both files, FriendRequestMenuHandler_Starter.cs and SentFriendRequestMenuHandler_Starter.cs. This function will retrieve the avatar for each friend.

    private void RetrieveAvatar(string userId)
    {
    loadingPanel.gameObject.SetActive(true);
    _friendEssentialsWrapper.GetUserAvatar(userId, result => OnGetAvatarCompleted(userId, result));
    }
  16. Create a callback function in both files to handle the UI changes based on the avatar obtained from the previous step.

    private void OnGetAvatarCompleted(string userId, Result<Texture2D> result)
    {
    if (!result.IsError)
    {
    var incomingRequestEntry = GameObject.Find(userId);
    incomingRequestEntry.GetComponent<FriendRequestsEntryHandler>().friendImage.sprite = Sprite.Create(result.Value,
    new Rect(0f, 0f, result.Value.width, result.Value.height), Vector2.zero);
    }
    loadingPanel.gameObject.SetActive(false);
    }

Resources