Add match session browser menu - Browse dedicated server joinable sessions - (Unity module)
What's on the menu
A Unity user interface (UI) prefab for browsing match sessions has been prepared for you. You will use a starter version of this UI so you can follow along with this tutorial, adding code to it from scratch. The starter UI is available in the Resources section and consists of the following files:
- BrowseMatchMenuCanvas_Starter.cs: A C# that contains most of the match session browsing implementation. This class connects UI and the AccelByte Gaming Services (AGS) Game SDK service wrapper class to call its API.
- CS file:
Assets/Resources/Modules/MatchSessionDS/Scripts/UI/BrowseMatchMenuCanvas_Starter.cs
- CS file:
- BrowseMatchMenuCanvas_Starter.prefab: A GameObject prefab based on the Canvas Panel UI GameObject.
- Prefab file:
Assets/Resources/Modules/MatchSessionDS/Prefabs/BrowseMatchMenuCanvas_Starter.prefab
- Prefab file:
Browse match session canvas
The prefab you will use to implement browsing match sessions is BrowseMatchMenuCanvas_Starter.prefab
. The buttons and panels are already referenced in BrowseMatchMenuCanvas_Starter.cs
. In BrowseMatchMenuCanvas_Starter
class, the Start
function will look like the code below. This codes is for UI setup, setting the buttons callback, and setting up event listeners. The last line in Start
is for retrieving browsable match sessions.
private void Start()
{
...
BindEvent();
backButton.onClick.AddListener(MenuManager.Instance.OnBackPressed);
refreshBtn.onClick.AddListener(BrowseMatchSession);
scrollRect.onValueChanged.AddListener(OnScrollValueChanged);
GameManager.OnDisconnectedInMainMenu += OnDisconnectedFromMainMenu;
BrowseMatchSession();
}
Here is the preview of the browse match session canvas. You will use it to display available game session entries.
Match item canvas
The list of match sessions is displayed using a prefab component named MatchSessionItem.prefab
. This panel presents necessary information of sessions. The prefab leverages the MatchSessionItem.cs
script to update the user interface with provided data.
Ready the UI
In this section, you will learn how to implement UI functionality before calling the AGS Game SDK API to browse match sessions.
Open
BrowseMatchMenuCanvas_Starter
and modifyOnBrowseMatchSessionFinished
to handle the data returned from the function to browse match sessions as shown below.private void OnBrowseMatchSessionFinished(BrowseMatchResult result)
{
if (string.IsNullOrEmpty(result.ErrorMessage))
{
HideLoadingBackToMainPanel();
if (result.Result.Length<1)
{
noMatchFoundInfo.SetActive(true);
}
else
{
noMatchFoundInfo.SetActive(false);
RenderResult(result.Result);
}
}
else
{
ShowError(result.ErrorMessage);
}
}To handle the data retrieved from the next page of pagination, modify the
OnNextPageMatchSessionsRetrieved
function as shown below.private void OnNextPageMatchSessionsRetrieved(BrowseMatchResult nextPageResult)
{
if (string.IsNullOrEmpty(nextPageResult.ErrorMessage))
{
RenderResult(nextPageResult.Result, loadedModels.Count);
}
else
{
ShowError(nextPageResult.ErrorMessage);
}
}This step will differ depending on which module you are building from.
- If you are working from the Create Match Session Using DS module, open
Assets/Resources/Modules/MatchSessionDS/MatchSessionWithDSAssetConfig.asset
in the Unity Editor. - If you are working from the Create Match Session Using P2P module, open
Assets/Resources/Modules/MatchSessionP2P/MatchSessionWithP2PAssetConfig.asset
. Select theAssetConfig
file and tick the Is Starter Active checkbox. This will switch the script that's handling the UI from usingBrowseMatchMenuCanvas
to usingBrowseMatchMenuCanvas_Starter
.
- If you are working from the Create Match Session Using DS module, open
Play the game in the Editor. If your implementation was successful, when you navigate to the Browse Match Menu, you will see this log:
[BrowseMatchMenuCanvas_Starter.cs] [BrowseMatchSession] [Log] [linenumber] - Browse Match Session not yet implemented
Resources
- The files used in this tutorial section are available in the Unity Byte Wars GitHub repository.
- Assets/Resources/Modules/MatchSessionDS/MatchSessionWithDSAssetConfig.asset
- Assets/Resources/Modules/MatchSessionP2P/MatchSessionWithP2PAssetConfig.asset
- Assets/Resources/Modules/MatchSessionDS/Scripts/UI/BrowseMatchMenuCanvas_Starter.cs
- Assets/Resources/Modules/MatchSessionDS/Prefabs/BrowseMatchMenuCanvas_Starter.prefab