Put it all together - Introduction to Session - (Unity module)
Connect the UI to create and leave a session
Open the
SessionMenuHandler_Starter.cs
file and add the following code:private SessionEssentialsWrapper_Starter sessionEssentialsWrapper;
Add the following line to the
Awake()
function:private void Awake()
{
sessionEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<SessionEssentialsWrapper_Starter>();
...
}Create new functions to bind and unbind the events from
sessionEssentialsWrapper
:private void SubcsribeSessionEvents()
{
sessionEssentialsWrapper.OnCreateSessionCompleteEvent += OnCreateSessionCompleted;
sessionEssentialsWrapper.OnLeaveSessionCompleteEvent += OnLeaveSessionCompleted;
}private void UnSubscribeSessionEvents()
{
sessionEssentialsWrapper.OnCreateSessionCompleteEvent -= OnCreateSessionCompleted;
sessionEssentialsWrapper.OnLeaveSessionCompleteEvent -= OnLeaveSessionCompleted;
}In the
Start
function, add the following code:if (sessionEssentialsWrapper == null)
{
sessionEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<SessionEssentialsWrapper_Starter>();
SubscribeSessionEvents();
}Go to the
OnEnable()
function and update the code as follows:private void OnEnable()
{
CurrentView = SessionMenuView.Default;
if (sessionEssentialsWrapper == null)
{
return;
}
SubscribeSessionEvents();
}Navigate to the
OnDisable()
function and unsubscribe from the events with the following:private void OnDisable()
{
if (sessionEssentialsWrapper == null)
{
return;
}
UnSubscribeSessionEvents();
}Update the
OnEliminationButtonClicked()
function. You need to define a variable to hold the value forSessionV2GameSessionCreateRequest
, which will be passed tosessionEssentialsWrapper.CreateSession
. The code below usesunity-elimination-none
as the match pool which we can get from theGameSessionConfig
class. The final code for this function will appear as follows:private void OnEliminationButtonClicked()
{
Dictionary<InGameMode,
Dictionary<GameSessionServerType,
SessionV2GameSessionCreateRequest>> sessionConfig = GameSessionConfig.SessionCreateRequest;
if (!sessionConfig.TryGetValue(InGameMode.None, out var matchTypeDict))
{
BytewarsLogger.LogWarning("InGame Mode Not found");
return;
}
if (!matchTypeDict.TryGetValue(GameSessionServerType.None, out var request))
{
BytewarsLogger.LogWarning("GameSessionServerType Not found");
return;
}
CurrentView = SessionMenuView.Creating;
Button button = creatingPanel.gameObject.GetComponentInChildren<Button>();
button.gameObject.SetActive(true);
sessionEssentialsWrapper.CreateSession(request);
}In the
OnLeaveSessionButtonClicked()
function, you will implement logic to leave a session. Replace the existing implementation with the code below:private void OnLeaveSessionButtonClicked()
{
sessionEssentialsWrapper.LeaveSession(cachedSessionId);
}Update the functions that are declared as the event listeners. There are two functions:
OnCreateSessionCompleted()
andOnLeaveSessionCompleted()
.private void OnCreateSessionCompleted(Result<SessionV2GameSession> result)
{
if (!result.IsError)
{
CurrentView = SessionMenuView.Joining;
StartCoroutine(DelayCallback(sessionView => Helper(result, sessionView)));
}
else
{
CurrentView = SessionMenuView.Failed;
}
}private void OnLeaveSessionCompleted(Result<SessionV2GameSession> result)
{
if (!result.IsError)
{
MenuManager.Instance.OnBackPressed();
}
else
{
BytewarsLogger.LogWarning($"{result.Error.Message}");
MenuManager.Instance.OnBackPressed();
}
}Creating a session will automatically join the session, so
DelayCallback
has been added as the delay to show the message from creating a session panel to joining a session, and then join the session.
Resources
- The files used in this tutorial section are available in the Unity Byte Wars GitHub repository.