Put it all together - Create joinable sessions with peer-to-peer - (Unity module)
Browsing P2P sessions is not supported in WebGL builds due to the AccelByte Gaming Services (AGS) SDK for Unity lacking the P2P functionality for WebGL.
Connect the UI to the create, leave, and display current session status
In this tutorial, you will connect the UI to create a match session by using the MatchSessionP2PWrapper_Starter
class, which already incorporates the AGS Game SDK implementation. The CreateMatchSessionP2PHandler_Starter
class has been prepared for you as a UI handler for this tutorial.
Open the
CreateMatchSessionP2PHandler_Starter.cs
and add the code below:private MatchSessionP2PWrapper_Starter matchSessionP2PWrapper;
Go to the
ClickPeerToPeerButton
and incorporate this code into the function:public void ClickPeerToPeerButton()
{
InGameMode selectedGameMode = InGameMode.None;
MenuCanvas menu = MenuManager.Instance.GetCurrentMenu();
if (menu is MatchSessionServerTypeSelection serverTypeSelection)
{
selectedGameMode = serverTypeSelection.SelectedGameMode;
InitWrapper();
}
else
{
BytewarsLogger.LogWarning("Current menu is not server type selection menu while try to create match with DS");
return;
}
matchSessionP2PWrapper.CreateMatchSession(selectedGameMode, GameSessionServerType.PeerToPeer);
ShowLoading(
"Creating Match Session (P2P)...",
"Creating Match Session (P2P) is timed out.",
createMatchSessionTimeoutSec,
CancelCreateMatch);
}Go to
InitWrapper
and implement the following code:private void InitWrapper()
{
if (matchSessionP2PWrapper == null)
{
matchSessionP2PWrapper = TutorialModuleManager.Instance.GetModuleClass<MatchSessionP2PWrapper_Starter>();
}
...
}Now, set up the event binding in the
BindMatchSessionEvent
function along with the unbind inUnbindMatchSessionEvent
:private void BindMatchSessionEvent()
{
matchSessionP2PWrapper.BindEvents();
matchSessionP2PWrapper.BindMatchSessionP2PEvents();
matchSessionP2PWrapper.OnCreatedMatchSession += OnCreatedMatchSession;
matchSessionP2PWrapper.OnJoinedMatchSession += OnJoinedMatchSession;
matchSessionP2PWrapper.OnLeaveSessionCompleted += UnbindMatchSessionEvent;
}private void UnbindMatchSessionEvent()
{
matchSessionP2PWrapper.UnbindEvents();
matchSessionP2PWrapper.UnbindMatchSessionP2PEvents();
matchSessionP2PWrapper.OnCreatedMatchSession -= OnCreatedMatchSession;
matchSessionP2PWrapper.OnJoinedMatchSession -= OnJoinedMatchSession;
matchSessionP2PWrapper.OnLeaveSessionCompleted -= UnbindMatchSessionEvent;
}You have the
OnCreatedMatchSession
function that will be responsible for updating the loading status and showing the cancel button. This button usesCancelCreateMatch
as a callback. To be able to cancel matchmaking, update theCancelCreateMatch
function with the code below.private void CancelCreateMatch()
{
Reset();
ShowInfo("Match session creation cancelled");
matchSessionP2PWrapper.OnCreateMatchCancelled?.Invoke();
matchSessionP2PWrapper.CancelCreateMatchSession();
}Play the game in the editor. If successful, you will see this log:
[MatchSessionWrapper.cs] [OnCreateSessionCompleted] [Log] [linenumber] - Successfully created the match session
Congratulations! You successfully connected the UI with the wrapper class to create a match session.
Resources
- The file used in this tutorial is available in the Unity Byte Wars GitHub repository.