メインコンテンツまでスキップ

すべてを統合する - 専用サーバーで参加可能なセッションを作成する - (Unity モジュール)

Last updated on October 23, 2024

Hook up the create, leave, and display session UI

In this tutorial, you will connect the UI to create a match session by using the MatchSessionDSWrapper_Starter class, which already incorporates the AGS Game SDK implementation. The CreateMatchSessionDSHandler_Starter class has been provided as a UI handler for this tutorial.

  1. Open the CreateMatchSessionDSHandler_Starter.cs and add the code below:

    private MatchSessionDSWrapper_Starter matchSessionDSWrapper_starter;
  2. You will use matchSessionDSWrapper.CreateMatchSession to create a match session. Go to the ClickDedicatedServerButton and replace the code with this:

    public void ClickDedicatedServerButton()
    {
    InGameMode selectedGameMode = InGameMode.None;
    GameSessionServerType selectedSessionServerType =
    TutorialModuleManager.Instance.IsModuleActive(TutorialType.MultiplayerDSEssentials) ?
    GameSessionServerType.DedicatedServerAMS :
    GameSessionServerType.DedicatedServer;

    MenuCanvas menu = MenuManager.Instance.GetCurrentMenu();
    if (menu is MatchSessionServerTypeSelection serverTypeSelection)
    {
    selectedGameMode = serverTypeSelection.SelectedGameMode;
    InitWrapper();
    }
    else
    {
    BytewarsLogger.LogWarning("The current server type selection menu is the wrong menu to try to create matches with dedicated servers.");
    return;
    }

    matchSessionDSWrapper_starter.CreateMatchSession(selectedGameMode, selectedSessionServerType);
    ShowLoading(
    "Creating Match Session (Dedicated Server)...",
    "Creating Match Session (Dedicated Server) has timed out.",
    createMatchSessionTimeoutSec,
    CancelCreateMatch);
    }
  3. Go to InitWrapper and implement the following code within the function:

        private void InitWrapper()
    {
    if (matchSessionDSWrapper_starter == null)
    {
    matchSessionDSWrapper_starter = TutorialModuleManager.Instance.GetModuleClass<MatchSessionDSWrapper_Starter>();
    }

    BindMatchSessionEvent();

    MatchSessionServerTypeSelection.OnBackButtonCalled -= OnBackButtonFromServerSelection;
    MatchSessionServerTypeSelection.OnBackButtonCalled += OnBackButtonFromServerSelection;
    }
  4. Now, set up the event binding in the BindMatchSessionEvent function along with the unbinding in UnbindMatchSessionEvent.

    private void BindMatchSessionEvent()
    {
    matchSessionDSWrapper_starter.BindEvents();
    matchSessionDSWrapper_starter.BindMatchSessionDSEvents();
    matchSessionDSWrapper_starter.OnCreatedMatchSession += OnCreatedMatchSession;
    matchSessionDSWrapper_starter.OnJoinedMatchSession += OnJoinedMatchSession;
    matchSessionDSWrapper_starter.OnLeaveSessionCompleted += UnbindMatchSessionEvent;
    }
    private void UnbindMatchSessionEvent()
    {
    matchSessionDSWrapper_starter.UnbindEvents();
    matchSessionDSWrapper_starter.UnbindMatchSessionDSEvents();
    matchSessionDSWrapper_starter.OnCreatedMatchSession -= OnCreatedMatchSession;
    matchSessionDSWrapper_starter.OnJoinedMatchSession -= OnJoinedMatchSession;
    matchSessionDSWrapper_starter.OnLeaveSessionCompleted -= UnbindMatchSessionEvent;
    }
  5. OnCreatedMatchSession is responsible for updating the loading status and showing the cancel button. This button uses CancelCreateMatch as a callback. To be able to cancel matchmaking, update the CancelCreateMatch function with the code below.

    private void CancelCreateMatch()
    {
    ShowInfo("Match session creation canceled");
    matchSessionDSWrapper_starter.OnCreateMatchCancelled?.Invoke();
    matchSessionDSWrapper_starter.CancelCreateMatchSession();
    }
  6. Play the game from the Unity editor. If successful, you will see this log:

     [MatchSessionWrapper.cs] [OnCreateSessionCompleted] [Log] [linenumber] - Successfully created the match session

Congratulations! You successfully connected the UI with the wrapper class to create a match session.

Resources