Put it all together - Create joinable sessions with dedicated servers - (Unity module)
Connect the UI to the create, leave, and display current session status
Open the project in Unity and open the
CreateMatchSessionHandler_Starter
script. You will call the AccelByte Gaming Services (AGS) Game SDK for Unity API to create a match session via the wrapper class. Before you can start the API call, you need to define the wrapper first. Add the code bellow to theCreateMatchSessionHandler_Starter
file.private MatchSessionDSWrapper_Starter matchSessionDSWrapper_Starter;
Then, add the code below into
Start()
.private void Start()
{
matchSessionDSWrapper_Starter = TutorialModuleManager.Instance.GetModuleClass<MatchSessionDSWrapper_Starter>();
//... some code omitted for brevity
}Modify the
CreateMatchSession
function as follows:private void CreateMatchSession()
{
ShowLoading("Creating Match Session...", CancelCreateMatch);
matchSessionDSWrapper_Starter.Create(gameMode,
selectedSessionServerType, OnCreatedMatchSession);
}You need to define the
CancelCreateMatch
function. This function will cancel the match session creation using the starter version ofMatchSessionWrapper
class.private void CancelCreateMatch()
{
if(shownRectTransform!=null)
shownRectTransform.gameObject.SetActive(true);
loadingPanel.gameObject.SetActive(false);
matchSessionDSWrapper_Starter.CancelCreateMatchSession();
}You still need to define the
OnCreatedMatchSession
function to handle the match creation result.private static void OnCreatedMatchSession(string errorMessage)
{
instance.dsBtn.interactable = true;
if (!String.IsNullOrEmpty(errorMessage))
{
instance.ShowError(errorMessage);
}
else
{
//show loading, MatchSessionWrapper_Starter will move UI to lobby when connected to DS
instance.ShowLoading("Joining Session");
Debug.Log($"success create session");
}
}Play the game from Unity Editor. If successful, you will see this log:
[MatchSessionDSWrapper_Starter.cs] [OnCreateGameSessionResult] [Log] [linenumber] - create session result:
Congratulations! You successfully connected the UI with the wrapper class to create a match session. In the Play Test section, we will test create match session implementation.
Resources
- The file used in this tutorial is available in the Unity Byte Wars GitHub repository.