すべてを統合する - フレンドを管理する - (Unity モジュール)
Last updated on October 23, 2024
Connect the UI to block and unblock player
Open
FriendDetailsMenuHandler_Starter.cs
and add field following code:private ManagingFriendsWrapper_Starter _managingFriendsWrapper;
Add the following code to
Start()
:_managingFriendsWrapper = TutorialModuleManager.Instance.GetModuleClass<ManagingFriendsWrapper_Starter>();
Update the
OnUnfriendClicked()
by with the following code:_managingFriendsWrapper.Unfriend(UserID, OnUnfriendCompleted);
Create
OnUnfriendCompleted
that be trigger after the unfriend process completes.private void OnUnfriendCompleted(Result result)
{
if (!result.IsError)
{
Debug.Log($"sucessfully unfriend {UserID}");
}
else
{
Debug.LogWarning($"Error OnUnfriendCompleted");
}
}To block a player, update
OnBlockeClicked()
by adding the following code:_managingFriendsWrapper.BlockPlayer(UserID, OnBlockPlayerComplete);
Create a callback function called
OnBlockPlayerComplete
.private void OnBlockPlayerComplete(Result<BlockPlayerResponse> result)
{
if (!result.IsError)
{
Debug.Log($"sucessfully blocked {UserID}");
}
else
{
Debug.LogWarning($"Error OnBlockPlayerComplete");
}
}
Connect the UI to display blocked players and to unblock players
Open
BlockedPlayersMenuHandler_Starter.cs
and add the following code:private ManagingFriendsWrapper_Starter _managingFriendsWrapper;
Update
Awake()
by adding the following code:_managingFriendsWrapper = TutorialModuleManager.Instance.GetModuleClass<ManagingFriendsWrapper_Starter>();
Update
GetBlockedPlayers
with the following code:private void GetBlockedPlayers()
{
CurrentView = BlockedFriendsView.Default;
_managingFriendsWrapper.GetBlockedPlayers(OnLoadBlockedPlayersCompleted);
}Create a callback function to update the UI and data once the friend request loading is finished.
private void OnLoadBlockedPlayersCompleted(Result<BlockedList> result)
{
if (!result.IsError)
{
CurrentView = BlockedFriendsView.LoadingSuccess;
if (result.Value.data.Length > 0)
{
UserInfo(result.Value);
}
else
{
CurrentView = BlockedFriendsView.Default;
}
}
else
{
CurrentView = BlockedFriendsView.LoadingFailed;
}
}Update the
OnUnblockFriend
, this function serves as callback when we press the unblock button.private void OnUnblockFriend(string userId)
{
_managingFriendsWrapper.UnblockPlayer(userId, result => OnUnblockedCompleted(userId, result));
}
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/FriendEssentials/Scripts/UI/FriendDetailsMenuHandler_Starter.cs
- Assets/Resources/Modules/FriendEssentials/Prefabs/FriendDetails/FriendDetailsMenuCanvas_Starter.prefab