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

Put it all together - Quick match with peer-to-peer - (Unity module)

Last updated on October 23, 2024

Hook up the UI for the P2P matchmaking status

In this tutorial, you will call the peer-to-peer (P2P) matchmaking function in the MatchmakingSessionP2PWrapper_Starter class from the MatchmakingSessionP2PHandler_Starter class.

  1. Open MatchmakingSessionP2PHandler_Starter.cs file. Paste the code below into the class.

    private MatchmakingSessionP2PWrapper_Starter matchmakingP2PWrapper_starter;
  2. Modify ClickPeerToPeerButton as shown below. This code will prepare the AccelByte Gaming Services (AGS) Game SDK service wrapper and start matchmaking based on the game mode.

    public void ClickPeerToPeerButton()
    {
    MenuCanvas menu = MenuManager.Instance.GetCurrentMenu();
    if (menu is MatchmakingSessionServerTypeSelection serverTypeSelection)
    {
    InitWrapper();
    selectedGameMode = serverTypeSelection.SelectedGameMode;
    }
    else
    {
    BytewarsLogger.LogWarning("The current selection menu type is not the correct menu type for matchmaking with peer-to-peer.");
    }
    switch (selectedGameMode)
    {
    case InGameMode.OnlineDeathMatchGameMode:
    matchmakingP2PWrapper_starter.StartP2PMatchmaking(InGameMode.OnlineDeathMatchGameMode);
    ShowLoading($"Start {teamdeathmatchInfo}...",
    $"Start {teamdeathmatchInfo} is timed out",
    matchmakingTimeoutSec);
    break;
    case InGameMode.OnlineEliminationGameMode:
    matchmakingP2PWrapper_starter.StartP2PMatchmaking(InGameMode.OnlineEliminationGameMode);
    ShowLoading($"Start {eliminationInfo}...",
    $"Start {eliminationInfo} is timed out",
    matchmakingTimeoutSec);
    break;
    default:
    string errorMsg = $"No Peer To Peer MatchPoolName for {selectedGameMode}";
    BytewarsLogger.LogWarning(errorMsg);
    ShowError(errorMsg);
    break;
    }
    }
  3. Go to InitWrapper and update the InitWrapper function with the code below:

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

    BindMatchmakingEvent();

    MatchmakingSessionServerTypeSelection.OnBackButtonCalled -= OnBackButtonFromServerSelection;
    MatchmakingSessionServerTypeSelection.OnBackButtonCalled += OnBackButtonFromServerSelection;
    }
  4. Now, set up the event binding in the BindMatchmakingEvent function, along with unbinding in UnbindMatchmakingEvents.

    private void BindMatchmakingEvent()
    {
    if (matchmakingP2PWrapper_starter == null)
    {
    return;
    }

    matchmakingP2PWrapper_starter.BindMatchmakingEvent();
    matchmakingP2PWrapper_starter.OnMatchTicketP2PCreated += OnMatchTicketP2PCreated;
    matchmakingP2PWrapper_starter.OnMatchmakingWithP2PMatchFound += OnMatchmakingWithP2PMatchFound;
    matchmakingP2PWrapper_starter.OnMatchmakingWithP2PTicketExpired += OnMatchmakingWithP2PTicketExpired;
    matchmakingP2PWrapper_starter.OnMatchmakingWithP2PJoinSessionStarted += OnMatchmakingWithP2PJoinSessionStarted;
    matchmakingP2PWrapper_starter.OnMatchmakingWithP2PJoinSessionCompleted += OnMatchmakingWithP2PJoinSessionCompleted;
    matchmakingP2PWrapper_starter.OnMatchmakingWithP2PCanceled += OnMatchmakingWithP2PCanceledAsync;
    matchmakingP2PWrapper_starter.OnMatchmakingWithP2PError += ErrorPanelAsync;
    matchmakingP2PWrapper_starter.OnMatchmakingError += ErrorPanelAsync;
    matchmakingP2PWrapper_starter.OnIntentionallyLeaveSession += Reset;
    }
    private void UnbindMatchmakingEvents()
    {
    if (matchmakingP2PWrapper_starter == null)
    {
    return;
    }

    matchmakingP2PWrapper_starter.UnbindMatchmakingEvent();
    matchmakingP2PWrapper_starter.OnMatchTicketP2PCreated -= OnMatchTicketP2PCreated;
    matchmakingP2PWrapper_starter.OnMatchmakingWithP2PMatchFound -= OnMatchmakingWithP2PMatchFound;
    matchmakingP2PWrapper_starter.OnMatchmakingWithP2PTicketExpired -= OnMatchmakingWithP2PTicketExpired;
    matchmakingP2PWrapper_starter.OnMatchmakingWithP2PJoinSessionStarted -= OnMatchmakingWithP2PJoinSessionStarted;
    matchmakingP2PWrapper_starter.OnMatchmakingWithP2PJoinSessionCompleted -= OnMatchmakingWithP2PJoinSessionCompleted;
    matchmakingP2PWrapper_starter.OnMatchmakingWithP2PCanceled -= OnMatchmakingWithP2PCanceledAsync;
    matchmakingP2PWrapper_starter.OnMatchmakingWithP2PError -= ErrorPanelAsync;
    matchmakingP2PWrapper_starter.OnMatchmakingError -= ErrorPanelAsync;
    matchmakingP2PWrapper_starter.OnIntentionallyLeaveSession -= Reset;
    }
  5. You have OnMatchTicketP2PCreated that will responsible for updating the loading status and showing the cancel button. This button uses CancelP2PMatchmaking as a callback. To be able to cancel matchmaking, update the CancelP2PMatchmaking function with the code below.

    private void CancelP2PMatchmaking()
    {
    matchmakingP2PWrapper_starter.CancelP2PMatchmaking();
    ShowLoading("Canceling Match", "Canceling match is timed out", cancelMatchmakingTimeoutSec);
    }

Now, when you quick play using the Elimination game mode using P2P, the game will start a P2P matchmaking via the MatchmakingSessionP2PWrapper_Starter class.

Resources