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:_sessionEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<SessionEssentialsWrapper_Starter>();
Create new functions to bind and unbind the events from
_sessionEssentialsWrapper
:private void BindToWrapperEvent()
{
_sessionEssentialsWrapper.OnCreateSessionCompleteEvent += OnCreateSessionCompleted;
_sessionEssentialsWrapper.OnLeaveSessionCompleteEvent += OnLeaveSessionCompleted;
}private void UnbindToWrapperEvent()
{
_sessionEssentialsWrapper.OnCreateSessionCompleteEvent -= OnCreateSessionCompleted;
_sessionEssentialsWrapper.OnLeaveSessionCompleteEvent -= OnLeaveSessionCompleted;
}In the
Start
function, add the following code:if (_sessionEssentialsWrapper == null)
{
_sessionEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<SessionEssentialsWrapper_Starter>();
BindToWrapperEvent();
}Go to the
OnEnable()
function and update the code as follows:private void OnEnable()
{
CurrentView = SessionMenuView.Default;
if (_sessionEssentialsWrapper == null)
{
return;
}
BindToWrapperEvent();
}Navigate to the
OnDisable()
function and unsubscribe from the events with the following:private void OnDisable()
{
if (_sessionEssentialsWrapper == null)
{
return;
}
UnbindToWrapperEvent();
}Update the
OnEliminationButtonClicked()
function. You need to define a variable to hold the value forSessionRequestPayload
, which will be passed to_sessionEssentialsWrapper.CreateSession
. The code below usesunity-elimination-none
as the match pool. The final code for this function will appear as follows:private void OnEliminationButtonClicked()
{
var sessionRequest = new SessionRequestPayload
{
SessionType = SessionType.none,
InGameMode = InGameMode.None,
SessionTemplateName = "unity-elimination-none",
TutorialType = TutorialType.SessionEssentials
};
CurrentView = SessionMenuView.Creating;
var button = creatingPanel.gameObject.GetComponentInChildren<Button>();
button.gameObject.SetActive(true);
_sessionEssentialsWrapper.CreateSession(sessionRequest);
}Create
OnLeaveSessionButtonClicked()
, which will leave a session:private void OnLeaveSessionButtonClicked()
{
_sessionEssentialsWrapper.LeaveSession(_sessionResponsePayload.Result.Value.id);
}Update the functions that are declared as the event listeners. There are two functions:
OnCreateSessionCompleted()
andOnLeaveSessionCompleted()
.IsTutorialTypeMatch
will filter the result based onTutorialType
.private void OnCreateSessionCompleted(SessionResponsePayload response)
{
if (!response.IsError)
{
CurrentView = SessionMenuView.Joining;
StartCoroutine(DelayCallback(sessionView => Helper(response, sessionView)));
}
}private void OnLeaveSessionCompleted(SessionResponsePayload response)
{
if (response.IsError) return;
IsTutorialTypeMatch(_sessionResponsePayload.TutorialType);
_sessionResponsePayload = null;
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.