Put it all together - Browse dedicated server joinable sessions - (Unity module)
Browsing P2P sessions is not supported in WebGL builds due to the AGS SDK for Unity lacking the P2P functionality for WebGL. As a result, some code snippets include the UNITY_WEBGL
preprocessor to handle this limitation.
Hook up the session UI
A C# file has been provided called BrowseMatchMenuCanvas_Starter.cs
to show the UI for the browse match session. Inside it, you will see the starter wrappers already defined as variables.
private MatchSessionWrapper matchSessionWrapper;
private BrowseMatchSessionWrapper_Starter browseMatchSessionWrapper;
private MatchSessionDSWrapper_Starter matchSessionDSWrapper;
private MatchSessionP2PWrapper_Starter matchSessionP2PWrapper;
private void Start()
{
browseMatchSessionWrapper = TutorialModuleManager.Instance.GetModuleClass<BrowseMatchSessionWrapper_Starter>();
matchSessionWrapper = TutorialModuleManager.Instance.GetModuleClass<MatchSessionWrapper>();
matchSessionDSWrapper = TutorialModuleManager.Instance.GetModuleClass<MatchSessionDSWrapper_Starter>();
#if !UNITY_WEBGL
matchSessionP2PWrapper = TutorialModuleManager.Instance.GetModuleClass<MatchSessionP2PWrapper_Starter>();
#endif
...
}
Browse joinable match session
Open
BrowseMatchMenuCanvas_Starter.cs
file with your code editor. You will call the AccelByte Gaming Services (AGS) Game SDK API to browse match sessions via the wrapper class. ModifyBrowseMatchSession
function as follows:private void BrowseMatchSession()
{
ResetList();
browseMatchSessionWrapper.BrowseMatch(OnBrowseMatchSessionFinished);
ShowLoading("Getting Match Sessions...", CancelBrowseMatchSession);
}To handle pagination received from the AGS Game SDK API, modify the
OnScrollValueChanged
function to call the starter wrapper class as shown below.private void OnScrollValueChanged(Vector2 scrollPos)
{
//scroll reach bottom
if (scrollPos.y <= 0)
{
browseMatchSessionWrapper.QueryNextMatchSessions(OnNextPageMatchSessionsRetrieved);
}
}Go to
BindEvent()
function and add the code below.noteYou need to add the code to the UI Handler script for the module you are currently working on (dedicated servers or peer-to-peer).
- MatchSession With DS
- MatchSession With P2P
private void BindEvent()
{
...
matchSessionDSWrapper.BindMatchSessionDSEvents();
...
}private void BindEvent()
{
...
#if !UNITY_WEBGL
matchSessionP2PWrapper.BindMatchSessionP2PEvents();
#endif
...
}Go to the
UnbindEvent()
function and add this line to that function.infoYou need to add the code to the UI Handler script for the module you are currently working on (dedicated servers or peer-to-peer).
- MatchSession With DS
- MatchSession With P2P
private void UnbindEvent()
{
...
matchSessionDSWrapper.UnbindMatchSessionDSEvents();
...
}private void UnbindEvent()
{
...
#if !UNITY_WEBGL
matchSessionP2PWrapper.UnbindMatchSessionP2PEvents();
#endif
...
}
Join match session
Open the
BrowseMatchMenuCanvas_Starter.cs
file with your code editor. Then, go to theJoinMatch
function and implement the code below within the function. This code usesJoinMatchSession
fromMatchSessionWrapper
that was explained in the previous tutorial. This function is used from parent class because this module will handle joining to a game session for dedicated servers and peer-to-peer.private void JoinMatch(JoinMatchSessionRequest request)
{
ShowLoading("Joining Match Session...", CancelJoinMatchSession);
matchSessionWrapper.JoinMatchSession(request.MatchSessionId, request.GameMode);
}To cancel joining a match, update the
CancelJoinMatchSession
function with the code below:private void CancelJoinMatchSession()
{
HideLoadingBackToMainPanel();
matchSessionWrapper.CancelJoinMatchSession();
}
Resources
- The file used in this tutorial section is available in the Unity Byte Wars GitHub repository: