Put it all together - Manage friends - (Unity module)
Connect the UI to get blocked players
Open
BlockedPlayersMenuHandler_Starter.cs
and declare the module wrappers by adding the following code:private ManagingFriendsWrapper_Starter managingFriendsWrapper;
private FriendsEssentialsWrapper_Starter friendsEssentialsWrapper;Replace the existing
LoadBlockedPlayers()
implementation with the code below to call theGetBlockedPlayers()
function from theManagingFriendsWrapper_Starter
class.private void LoadBlockedPlayers()
{
CurrentView = BlockedFriendsView.Loading;
managingFriendsWrapper.GetBlockedPlayers(OnLoadBlockedPlayersCompleted);
}Replace the
OnLoadBlockedPlayersCompleted
callback function with the code below to handle the result.private void OnLoadBlockedPlayersCompleted(Result<BlockedList> result)
{
if (result.IsError)
{
CurrentView = BlockedFriendsView.LoadFailed;
return;
}
BlockedData[] blockedData = result.Value.data;
if (blockedData.Length <= 0)
{
CurrentView = BlockedFriendsView.Default;
return;
}
IEnumerable<string> blockedPlayerIds = blockedData.Select(player => player.blockedUserId);
GetBulkUserInfo(blockedPlayerIds.ToArray());
}Create a function called
GetBulkUserInfo()
to get the user information of the blocked players.private void GetBulkUserInfo(params string[] userIds)
{
friendsEssentialsWrapper.GetBulkUserInfo(userIds, OnGetBulkUserInfoCompleted);
}Create a callback function called
OnGetBulkUserInfoCompleted()
to handle the result.private void OnGetBulkUserInfoCompleted(Result<ListBulkUserInfoResponse> result)
{
if (result.IsError)
{
return;
}
CurrentView = BlockedFriendsView.LoadSuccess;
ClearBlockedPlayers();
PopulateBlockedPlayers(result.Value.data);
}Create a function called
PopulateBlockedPlayers()
to populate the blocked players list.private void PopulateBlockedPlayers(params BaseUserInfo[] userInfo)
{
foreach (BaseUserInfo baseUserInfo in userInfo)
{
CreatePlayerEntry(baseUserInfo.userId, baseUserInfo.displayName);
}
}Create a function called
CreatePlayerEntry()
to create a player entry for each blocked player. If the player does not have a display name, the function will use the first five characters of the user ID as the display name.private void CreatePlayerEntry(string userId, string displayName)
{
GameObject playerEntry = Instantiate(playerEntryPrefab, resultContentPanel);
playerEntry.name = userId;
if (string.IsNullOrEmpty(displayName))
{
string truncatedUserId = userId[..5];
displayName = $"Player-{truncatedUserId}";
}
BlockedPlayerEntryHandler playerEntryHandler = playerEntry.GetComponent<BlockedPlayerEntryHandler>();
playerEntryHandler.UserId = userId;
playerEntryHandler.FriendName.text = displayName;
blockedPlayers.Add(userId, playerEntry);
}Update the
OnEnable()
function to call theLoadBlockedPlayers()
function and define the module wrappers. Replace the existing implementation with the code below.private void OnEnable()
{
if (managingFriendsWrapper == null)
{
managingFriendsWrapper = TutorialModuleManager.Instance.GetModuleClass<ManagingFriendsWrapper_Starter>();
}
if (friendsEssentialsWrapper == null)
{
friendsEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<FriendsEssentialsWrapper_Starter>();
}
if (managingFriendsWrapper != null && friendsEssentialsWrapper != null)
{
LoadBlockedPlayers();
}
}
Connect the UI to display avatars of blocked players
Open
BlockedPlayerEntryHandler.cs
and create a function calledRetrieveUserAvatar()
to get the user avatarprivate void RetrieveUserAvatar(string userId)
{
friendsEssentialsWrapper.GetUserAvatar(userId, result => OnGetAvatarCompleted(userId, result));
}Create a callback function called
OnGetAvatarCompleted()
to handle the result.private void OnGetAvatarCompleted(string userId, Result<Texture2D> result)
{
if (result.IsError)
{
return;
}
if (!blockedPlayers.TryGetValue(userId, out GameObject playerEntry))
{
return;
}
if (!playerEntry.TryGetComponent(out BlockedPlayerEntryHandler playerEntryHandler))
{
return;
}
Rect rect = new Rect(0f, 0f, result.Value.width, result.Value.height);
playerEntryHandler.FriendImage.sprite = Sprite.Create(result.Value, rect, Vector2.zero);
}Update the
CreatePlayerEntry()
function to call theRetrieveUserAvatar()
function. Add the highlighted code below.private void CreatePlayerEntry(string userId, string displayName)
{
//...
blockedPlayers.Add(userId, playerEntry);
RetrieveUserAvatar(userId);
}
Connect the UI to unblock a player
Open
BlockedPlayerEntryHandler.cs
and create a function calledUnblockPlayer()
to unblock a player by their user IDprivate void UnblockPlayer(string userId)
{
MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptConfirmTitle,
FriendsHelper.UnblockPlayerConfirmationMessage,
"Yes",
confirmAction: () =>
{
MenuManager.Instance.PromptMenu.ShowLoadingPrompt(FriendsHelper.UnblockingPlayerMessage);
managingFriendsWrapper.UnblockPlayer(userId, result => OnUnblockPlayerCompleted(userId, result));
},
"No", null);
}Create a callback function called
OnUnblockPlayerCompleted()
to handle the result. The function will show a prompt menu to confirm the unblocking action.private void OnUnblockPlayerCompleted(string userId, Result<UnblockPlayerResponse> result)
{
if (result.IsError)
{
MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptErrorTitle,
result.Error.Message, "OK", null);
return;
}
MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptMessageTitle,
FriendsHelper.UnblockPlayerCompletedMessage, "OK", null);
LoadBlockedPlayers();
}Update the
CreatePlayerEntry()
function to call theUnblockPlayer()
function when the unblock button is clicked. Add the code below.private void CreatePlayerEntry(string userId, string displayName)
{
//...
playerEntryHandler.FriendName.text = displayName;
playerEntryHandler.UnblockButton.onClick.AddListener(() => UnblockPlayer(userId));
blockedPlayers.Add(userId, playerEntry);
//...
}
Connect the UI to block and unfriend a player
Open
FriendDetailsMenuHandler_Starter.cs
and declare the module wrappers by adding the following code:private ManagingFriendsWrapper_Starter managingFriendsWrapper;
Update the
OnEnable()
function to define the module wrappers. Add the code below.private void OnEnable()
{
if (managingFriendsWrapper == null)
{
managingFriendsWrapper = TutorialModuleManager.Instance.GetModuleClass<ManagingFriendsWrapper_Starter>();
}
}Update the
Unfriend()
function to enable unfriending players by their user IDs. This function will show a prompt menu to confirm the unfriending action. Replace the existing implementation with the code below.private void Unfriend()
{
MenuManager.Instance.PromptMenu.ShowPromptMenu(
FriendsHelper.PromptConfirmTitle,
FriendsHelper.UnfriendConfirmationMessage,
"Yes",
confirmAction: () =>
{
MenuManager.Instance.PromptMenu.ShowLoadingPrompt(FriendsHelper.UnfriendingMessage);
managingFriendsWrapper.Unfriend(UserId, OnUnfriendCompleted);
},
"No", null);
}Update the
OnUnfriendCompleted()
function to handle the result. Replace the existing implementation with the code below.private void OnUnfriendCompleted(Result result)
{
if (result.IsError)
{
MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptErrorTitle,
result.Error.Message, "OK", null);
return;
}
BytewarsLogger.Log($"Successfully unfriended player with user ID: {UserId}");
MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptMessageTitle,
FriendsHelper.UnfriendCompletedMessage, "OK", null);
MenuManager.Instance.OnBackPressed();
}Update the
BlockPlayer()
to enable blocking players by their user IDs. This function will show a prompt menu to confirm the blocking action. Replace the existing implementation with the code below.private void BlockPlayer()
{
MenuManager.Instance.PromptMenu.ShowPromptMenu(
FriendsHelper.PromptConfirmTitle,
FriendsHelper.BlockPlayerConfirmationMessage,
"Yes",
confirmAction: () =>
{
MenuManager.Instance.PromptMenu.ShowLoadingPrompt(FriendsHelper.BlockingPlayerMessage);
managingFriendsWrapper.BlockPlayer(UserId, OnBlockPlayerComplete);
},
"No", null);
}Update the
OnBlockPlayerCompleted()
function to handle the result. Replace the existing implementation with the code below.private void OnBlockPlayerComplete(Result<BlockPlayerResponse> result)
{
if (result.IsError)
{
MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptErrorTitle,
result.Error.Message, "OK", null);
return;
}
BytewarsLogger.Log($"Successfully blocked player with user ID: {UserId}");
MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptMessageTitle,
FriendsHelper.BlockPlayerCompletedMessage, "OK", null);
MenuManager.Instance.OnBackPressed();
}
Resources
- The files used in this tutorial are available in the Unity Byte Wars GitHub repository.
- Assets/Resources/Modules/ManagingFriends/ManagingFriendsAssetConfig.asset
- Assets/Resources/Modules/ManagingFriends/Scripts/UI/BlockedPlayersMenuHandler_Starter.cs
- Assets/Resources/Modules/ManagingFriends/Prefabs/BlockedPlayers/BlockedPlayersMenuCanvas_Starter.prefab
- Assets/Resources/Modules/FriendsEssentials/Scripts/UI/FriendDetailsMenuHandler_Starter.cs
- Assets/Resources/Modules/FriendsEssentials/Prefabs/FriendDetails/FriendDetailsMenuCanvas_Starter.prefab