Skip to main content

Put it all together - Introduction to Session - (Unity module)

Last updated on March 22, 2024

Connect the UI to create and leave a session

  1. Open the SessionMenuHandler_Starter.cs file and add the following code:

    private SessionEssentialsWrapper_Starter _sessionEssentialsWrapper;
  2. Add the following line to the Awake() function:

    _sessionEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<SessionEssentialsWrapper_Starter>();
  3. 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;
    }
  4. In the Start function, add the following code:

     if (_sessionEssentialsWrapper == null)
    {
    _sessionEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<SessionEssentialsWrapper_Starter>();
    BindToWrapperEvent();
    }
  5. Go to the OnEnable() function and update the code as follows:

    private void OnEnable()
    {
    CurrentView = SessionMenuView.Default;
    if (_sessionEssentialsWrapper == null)
    {
    return;
    }

    BindToWrapperEvent();
    }
  6. Navigate to the OnDisable() function and unsubscribe from the events with the following:

    private void OnDisable()
    {
    if (_sessionEssentialsWrapper == null)
    {
    return;
    }

    UnbindToWrapperEvent();
    }
  7. Update the OnEliminationButtonClicked() function. You need to define a variable to hold the value for SessionRequestPayload, which will be passed to _sessionEssentialsWrapper.CreateSession. The code below uses unity-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);
    }
  8. Create OnLeaveSessionButtonClicked(), which will leave a session:

    private void OnLeaveSessionButtonClicked()
    {
    _sessionEssentialsWrapper.LeaveSession(_sessionResponsePayload.Result.Value.id);
    }
  9. Update the functions that are declared as the event listeners. There are two functions: OnCreateSessionCompleted() and OnLeaveSessionCompleted(). IsTutorialTypeMatch will filter the result based on TutorialType.

    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();
    }
  10. 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