すべてを統合する - フレンドを追加する - (Unity モジ ュール)
Connect the UI to get incoming requests
Open
FriendRequestMenuHandler_Starter.cs
and declare theFriendsEssentialsWrapper_Starter
class as a private variable.private FriendsEssentialsWrapper_Starter friendsEssentialsWrapper;
Navigate to the
OnEnable()
function and add the code below to retrieve thefriendsEssentialsWrapper
:private void OnEnable()
{
if (friendsEssentialsWrapper == null)
{
friendsEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<FriendsEssentialsWrapper_Starter>();
}
}Replace the
LoadIncomingFriendRequests()
function with the following code to call theLoadIncomingFriendRequests()
function from the wrapper class and passOnLoadIncomingFriendRequestsCompleted
as the callback function to handle the result.private void LoadIncomingFriendRequests()
{
CurrentView = FriendRequestsView.Loading;
friendsEssentialsWrapper.LoadIncomingFriendRequests(OnLoadIncomingFriendRequestsCompleted);
}Replace the
OnLoadIncomingFriendRequestsCompleted
callback function to show the UI if the result failed or empty, or retrieve user information from the friend ID list from the result.private void OnLoadIncomingFriendRequestsCompleted(Result<Friends> result)
{
if (result.IsError)
{
CurrentView = FriendRequestsView.LoadFailed;
return;
}
if (result.Value.friendsId.Length <= 0)
{
CurrentView = FriendRequestsView.Default;
return;
}
GetBulkUserInfo(result.Value);
}Create a function called
GetBulkUserInfo()
as a main function to get the user information from the list of friend IDs.private void GetBulkUserInfo(Friends friends)
{
friendsEssentialsWrapper.GetBulkUserInfo(friends.friendsId, OnGetBulkUserInfoCompleted);
}Create a callback function called
OnGetBulkUserInfoCompleted()
to display the user information retrieved fromGetBulkUserInfo
function.private void OnGetBulkUserInfoCompleted(Result<ListBulkUserInfoResponse> result)
{
if (result.IsError)
{
CurrentView = FriendRequestsView.LoadFailed;
return;
}
ClearFriendRequestList();
CurrentView = FriendRequestsView.LoadSuccess;
PopulateFriendRequestList(result.Value.data);
}Create a function called
PopulateFriendRequestList()
to populate the friend request list.private void PopulateFriendRequestList(params BaseUserInfo[] userInfo)
{
foreach (BaseUserInfo baseUserInfo in userInfo)
{
CreateFriendEntry(baseUserInfo.userId, baseUserInfo.displayName);
}
}Create a function called
CreateFriendEntry()
to create a friend entry for each friend request.private void CreateFriendEntry(string userId, string displayName)
{
GameObject playerEntry = Instantiate(friendEntryPrefab, resultContentPanel);
playerEntry.name = userId;
if (string.IsNullOrEmpty(displayName))
{
string truncatedUserId = userId[..5];
displayName = $"Player-{truncatedUserId}";
}
FriendRequestsEntryHandler playerEntryHandler = playerEntry.GetComponent<FriendRequestsEntryHandler>();
playerEntryHandler.UserId = userId;
playerEntryHandler.FriendName.text = displayName;
friendRequests.Add(userId, playerEntry);
}Update the
OnEnable()
function to load the friend request list when the UI is enabled. Add the code below.private void OnEnable()
{
//...
if (friendsEssentialsWrapper != null)
{
LoadIncomingFriendRequests();
}
}
Connect the UI to get outgoing requests
Open
SentFriendRequestMenuHandler_Starter.cs
and declare theFriendsEssentialsWrapper_Starter
class as a private variable.private FriendsEssentialsWrapper_Starter friendsEssentialsWrapper;
Navigate to the
OnEnable()
function and add the code below to retrieve thefriendsEssentialsWrapper
:private void OnEnable()
{
if (friendsEssentialsWrapper == null)
{
friendsEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<FriendsEssentialsWrapper_Starter>();
}
}Replace the
LoadOutgoingFriendRequests()
with the code below to call theLoadOutgoingFriendRequests()
function from the wrapper class and passOnLoadOutgoingFriendRequestsCompleted
as the callback function to handle the result.private void LoadOutgoingFriendRequests()
{
CurrentView = SentFriendRequestsView.Loading;
friendsEssentialsWrapper.LoadOutgoingFriendRequests(OnLoadOutgoingRequestsCompleted);
}Create a callback function called
OnLoadOutgoingRequestsCompleted()
. This will handle the UI if theLoadOutgoingFriendRequests
failed and retrieve user information based on the retrieved friend IDs if it succeed.private void OnLoadOutgoingRequestsCompleted(Result<Friends> result)
{
if (result.IsError)
{
CurrentView = SentFriendRequestsView.LoadFailed;
return;
}
if (result.Value.friendsId.Length <= 0)
{
CurrentView = SentFriendRequestsView.Default;
return;
}
GetBulkUserInfo(result.Value);
}Create a function called
GetBulkUserInfo()
. This will be the main function that retrieves the user information from the list of friend IDs.private void GetBulkUserInfo(Friends friends)
{
friendsEssentialsWrapper.GetBulkUserInfo(friends.friendsId, OnGetBulkUserInfoCompleted);
}Create a callback function called
OnGetBulkUserInfoCompleted()
to display the user information retrieved fromGetBulkUserInfo
function.private void OnGetBulkUserInfoCompleted(Result<ListBulkUserInfoResponse> result)
{
if (result.IsError)
{
CurrentView = SentFriendRequestsView.LoadFailed;
return;
}
ClearFriendRequestList();
CurrentView = SentFriendRequestsView.LoadSuccess;
PopulateFriendRequestList(result.Value.data);
}Create a function called
PopulateFriendRequestList()
to populate the sent friend request list.private void PopulateFriendRequestList(params BaseUserInfo[] userInfo)
{
foreach (BaseUserInfo baseUserInfo in userInfo)
{
CreateFriendEntry(baseUserInfo.userId, baseUserInfo.displayName);
}
}Create a function called
CreateFriendEntry()
to create a sent friend entry for each friend request.private void CreateFriendEntry(string userId, string displayName)
{
GameObject playerEntry = Instantiate(friendEntryPrefab, resultContentPanel);
playerEntry.name = userId;
if (string.IsNullOrEmpty(displayName))
{
string truncatedUserId = userId[..5];
displayName = $"Player-{truncatedUserId}";
}
SentFriendRequestsEntryHandler playerEntryHandler = playerEntry.GetComponent<SentFriendRequestsEntryHandler>();
playerEntryHandler.UserId = userId;
playerEntryHandler.FriendName.text = displayName;
friendRequests.Add(userId, playerEntry);
}Update the
OnEnable()
function to load the sent friend request list when the UI is enabled. Add the code below:private void OnEnable()
{
//...
if (friendsEssentialsWrapper != null)
{
LoadOutgoingFriendRequests();
}
}
Connect the UI to display avatars of friend requests
In both
FriendRequestMenuHandler_Starter.cs
andSentFriendRequestMenuHandler_Starter.cs
, create a function calledRetrieveUserAvatar()
to get the user avatar and a passOnGetAvatarCompleted
as the result handler.private void RetrieveUserAvatar(string userId)
{
friendsEssentialsWrapper.GetUserAvatar(userId, result => OnGetAvatarCompleted(result, userId));
}Create a callback function called
OnGetAvatarCompleted()
in both files to handle the result fromRetrieveUserAvatar()
.private void OnGetAvatarCompleted(Result<Texture2D> result, string userId)
{
if (result.IsError)
{
BytewarsLogger.LogWarning($"Unable to get avatar for user Id: {userId}, " +
$"Error Code: {result.Error.Code}, " +
$"Error Message: {result.Error.Message}");
return;
}
if (result.Value == null || !friendRequests.TryGetValue(userId, out GameObject friendEntry))
{
return;
}
Image friendImage = friendEntry.GetComponent<SentFriendRequestsEntryHandler>().FriendImage;
Rect imageRect = new Rect(0f, 0f, result.Value.width, result.Value.height);
friendImage.sprite = Sprite.Create(result.Value, imageRect, Vector2.zero);
}Update the
CreateFriendEntry()
function in both files to call theRetrieveUserAvatar()
function.private void CreateFriendEntry(string userId, string displayName)
{
//...
friendRequests.Add(userId, playerEntry);
RetrieveUserAvatar(userId);
}
Connect the UI to accept, reject, and cancel friend requests
In
FriendRequestMenuHandler_Starter.cs
, create a function calledAcceptFriendInvitation()
to accept an incoming friend request.private void AcceptFriendInvitation(string userId)
{
MenuManager.Instance.PromptMenu.ShowLoadingPrompt(FriendsHelper.AcceptingFriendRequestMessage);
friendsEssentialsWrapper.AcceptFriend(userId, OnAcceptInvitationCompleted);
}Create a callback function called
OnAcceptInvitationCompleted()
to handle the result. It will show a confirmation that the request was completed and update the friend request list.private void OnAcceptInvitationCompleted(IResult result)
{
if (result.IsError)
{
MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptErrorTitle,
result.Error.Message, "OK", null);
return;
}
MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptMessageTitle,
FriendsHelper.FriendRequestAcceptedMessage, "OK", null);
LoadIncomingFriendRequests();
}Create a function called
DeclineFriendInvitation()
to reject an incoming friend request.private void DeclineFriendInvitation(string userId)
{
MenuManager.Instance.PromptMenu.ShowLoadingPrompt(FriendsHelper.RejectingFriendRequestMessage);
friendsEssentialsWrapper.DeclineFriend(userId, OnDeclineInvitationCompleted);
}Create a callback function called
OnDeclineInvitationCompleted()
to handle the result. It will show a confirmation that the request was completed and update the friend request list.private void OnDeclineInvitationCompleted(IResult result)
{
if (result.IsError)
{
MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptErrorTitle,
result.Error.Message, "OK", null);
return;
}
MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptMessageTitle,
FriendsHelper.FriendRequestRejectedMessage, "OK", null);
LoadIncomingFriendRequests();
}Add the code below to
CreateFriendEntry()
to connect the accept and reject buttons to theAcceptFriendInvitation()
andDeclineFriendInvitation()
functions.private void CreateFriendEntry(string userId, string displayName)
{
//...
playerEntryHandler.UserId = userId;
playerEntryHandler.FriendName.text = displayName;
playerEntryHandler.AcceptButton.onClick.AddListener(() => AcceptFriendInvitation(userId));
playerEntryHandler.RejectButton.onClick.AddListener(() => DeclineFriendInvitation(userId));
friendRequests.Add(userId, playerEntry);
//...
}Open
SentFriendRequestMenuHandler_Starter.cs
, create a function calledCancelFriendRequest()
to cancel a friend request.private void CancelFriendRequest(string userId)
{
MenuManager.Instance.PromptMenu.ShowLoadingPrompt(FriendsHelper.CancelingFriendRequestMessage);
friendsEssentialsWrapper.CancelFriendRequests(userId, OnCancelFriendRequestCompleted);
}Create a callback function called
OnCancelFriendRequestCompleted()
to handle the result. It will show a confirmation that the request was completed and update the sent friend request list.private void OnCancelFriendRequestCompleted(IResult result)
{
if (result.IsError)
{
MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptErrorTitle,
result.Error.Message, "OK", null);
return;
}
MenuManager.Instance.PromptMenu.ShowPromptMenu("Message", FriendsHelper.FriendRequestCanceledMessage, "OK", null);
LoadOutgoingFriendRequests();
}Add the code below to
CreateFriendEntry()
to connect the cancel button to theCancelFriendRequest()
function.private void CreateFriendEntry(string userId, string displayName)
{
//...
playerEntryHandler.UserId = userId;
playerEntryHandler.FriendName.text = displayName;
playerEntryHandler.CancelButton.onClick.AddListener(() => CancelFriendRequest(userId));
friendRequests.Add(userId, playerEntry);
//...
}
Connect the UI to listen to friend request events
Open
FriendRequestMenuHandler_Starter.cs
, modify theAwake()
function to listen to theOnIncomingRequest
andOnRequestCanceled
events. Add the code below:private void Awake()
{
CurrentView = FriendRequestsView.Default;
backButton.onClick.AddListener(MenuManager.Instance.OnBackPressed);
FriendsEssentialsWrapper.OnIncomingRequest += OnFriendRequestUpdated;
FriendsEssentialsWrapper.OnRequestCanceled += OnFriendRequestUpdated;
}Create a function called
OnIncomingFriendRequest()
to handle the incoming friend request event. It will update the friend request list.private void OnFriendRequestUpdated(string userId)
{
if (!gameObject.activeSelf)
{
return;
}
LoadIncomingFriendRequests();
}Open
SentFriendRequestMenuHandler_Starter.cs
and modify theAwake()
function to listen to theOnRequestRejected
andOnRequestAccepted
events. Add the code below:private void Awake()
{
CurrentView = SentFriendRequestsView.Default;
backButton.onClick.AddListener(MenuManager.Instance.OnBackPressed);
FriendsEssentialsWrapper.OnRequestRejected += OnFriendRequestUpdated;
FriendsEssentialsWrapper.OnRequestAccepted += OnFriendRequestUpdated;
}Create a function called
OnFriendRequestUpdated()
to handle the friend request updated event. It will update the sent friend request list.private void OnFriendRequestUpdated(string userId)
{
if (!gameObject.activeSelf)
{
return;
}
LoadOutgoingFriendRequests();
}
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/FriendRequestEntryComponent.prefab
- Assets/Resources/Modules/FriendsEssentials/Scripts/UI/FriendRequestEntryHandler.cs
- Assets/Resources/Modules/FriendsEssentials/Prefabs/SentFriendRequests/SentFriendRequestMenuCanvas_Starter.prefab
- Assets/Resources/Modules/FriendsEssentials/Scripts/UI/SentFriendRequestsMenuHandler_Starter.cs
- Assets/Resources/Modules/FriendsEssentials/Prefabs/SentFriendRequests/SentFriendRequestEntryComponent.prefab
- Assets/Resources/Modules/FriendsEssentials/Scripts/UI/SentFriendRequestsEntryHandler.cs