すべてを統合する - フレンドを追加する - (Unity モジ ュール)
Connect the UI to get incoming requests
- 
Open
FriendRequestsMenu_Starter.csand declare theFriendsEssentialsWrapper_Starterclass as a private variable.private FriendsEssentialsWrapper_Starter friendsEssentialsWrapper; - 
Navigate to the
OnEnable()function and add the code below to retrieve theFriendsEssentialsWrapper_Starter: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 passOnLoadIncomingFriendRequestsCompletedas the callback function to handle the result.private void LoadIncomingFriendRequests()
{
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Loading);
friendsEssentialsWrapper.LoadIncomingFriendRequests(OnLoadIncomingFriendRequestsCompleted);
} - 
Replace the
OnLoadIncomingFriendRequestsCompletedcallback 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)
{
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Error);
return;
}
if (result.Value.friendsId.Length <= 0)
{
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Empty);
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<AccountUserPlatformInfosResponse> result)
{
if (result.IsError)
{
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Error);
return;
}
ClearFriendRequestList();
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Not_Empty);
PopulateFriendRequestList(result.Value.Data);
} - 
Create a function called
PopulateFriendRequestList()to populate the friend request list.private void PopulateFriendRequestList(params AccountUserPlatformData[] userInfo)
{
foreach (AccountUserPlatformData baseUserInfo in userInfo)
{
CreateFriendEntry(baseUserInfo.UserId, AccelByteWarsOnlineUtility.GetDisplayName(baseUserInfo));
}
} - 
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;
FriendEntry playerEntryHandler = playerEntry.GetComponent<FriendEntry>();
playerEntryHandler.EntryView = FriendEntry.FriendEntryView.PendingInbound;
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
SentFriendRequestsMenu_Starter.csand declare theFriendsEssentialsWrapper_Starterclass 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 passOnLoadOutgoingFriendRequestsCompletedas the callback function to handle the result.private void LoadOutgoingFriendRequests()
{
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Loading);
friendsEssentialsWrapper.LoadOutgoingFriendRequests(OnLoadOutgoingRequestsCompleted);
} - 
Create a callback function called
OnLoadOutgoingRequestsCompleted(). This will handle the UI if theLoadOutgoingFriendRequestsfailed and retrieve user information based on the retrieved friend IDs if it succeed.private void OnLoadOutgoingRequestsCompleted(Result<Friends> result)
{
if (result.IsError)
{
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Error);
return;
}
if (result.Value.friendsId.Length <= 0)
{
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Empty);
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<AccountUserPlatformInfosResponse> result)
{
if (result.IsError)
{
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Error);
return;
}
ClearFriendRequestList();
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Not_Empty);
PopulateFriendRequestList(result.Value.Data);
} - 
Create a function called
PopulateFriendRequestList()to populate the sent friend request list.private void PopulateFriendRequestList(params AccountUserPlatformData[] userInfo)
{
foreach (AccountUserPlatformData baseUserInfo in userInfo)
{
CreateFriendEntry(baseUserInfo.UserId, AccelByteWarsOnlineUtility.GetDisplayName(baseUserInfo));
}
} - 
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;
FriendEntry playerEntryHandler = playerEntry.GetComponent<FriendEntry>();
playerEntryHandler.EntryView = FriendEntry.FriendEntryView.PendingOutbound;
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
FriendRequestsMenu_Starter.csandSentFriendRequestsMenu_Starter.cs, create a function calledRetrieveUserAvatar()to get the user avatar and a passOnGetAvatarCompletedas 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<FriendEntry>().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
FriendRequestsMenu_Starter.cs, create a function calledAcceptFriendInvitation()to accept an incoming friend request.private void AcceptFriendInvitation(string userId)
{
MenuManager.Instance.PromptMenu.ShowLoadingPrompt(FriendsEssentialsModels.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(FriendsEssentialsModels.PromptErrorTitle,
result.Error.Message, "OK", null);
return;
}
MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsEssentialsModels.PromptMessageTitle,
FriendsEssentialsModels.FriendRequestAcceptedMessage, "OK", null);
LoadIncomingFriendRequests();
} - 
Create a function called
DeclineFriendInvitation()to reject an incoming friend request.private void DeclineFriendInvitation(string userId)
{
MenuManager.Instance.PromptMenu.ShowLoadingPrompt(FriendsEssentialsModels.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(FriendsEssentialsModels.PromptErrorTitle,
result.Error.Message, "OK", null);
return;
}
MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsEssentialsModels.PromptMessageTitle,
FriendsEssentialsModels.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
SentFriendRequestsMenu_Starter.cs, create a function calledCancelFriendRequest()to cancel a friend request.private void CancelFriendRequest(string userId)
{
MenuManager.Instance.PromptMenu.ShowLoadingPrompt(FriendsEssentialsModels.CancelingFriendRequestMessage);
friendsEssentialsWrapper.CancelFriendRequests(userId, result => OnCancelFriendRequestCompleted(userId, result));
} - 
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(string userId, IResult result)
{
if (result.IsError)
{
MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsEssentialsModels.PromptErrorTitle,
result.Error.Message, "OK", null);
return;
}
MenuManager.Instance.PromptMenu.ShowPromptMenu("Message", FriendsEssentialsModels.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
FriendRequestsMenu_Starter.cs, modify theAwake()function to listen to theOnIncomingRequestandOnRequestCanceledevents. Add the code below:private void Awake()
{
backButton.onClick.AddListener(MenuManager.Instance.OnBackPressed);
FriendsEssentialsModels.OnIncomingRequest += OnFriendRequestUpdated;
FriendsEssentialsModels.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
SentFriendRequestsMenu_Starter.csand modify theAwake()function to listen to theOnRequestRejectedandOnRequestAcceptedevents. Add the code below:private void Awake()
{
backButton.onClick.AddListener(MenuManager.Instance.OnBackPressed);
FriendsEssentialsModels.OnRequestRejected += OnFriendRequestUpdated;
FriendsEssentialsModels.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.