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

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

Last updated on November 25, 2024
備考

Browsing P2P sessions is not supported in WebGL builds due to the AccelByte Gaming Services (AGS) SDK for Unity lacking the P2P functionality for WebGL.

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;
  2. Modify ClickPeerToPeerButton as shown below. This code will prepare the 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("Current menu is not server type selection menu while try to matchmaking with Peer to Peer");
    }
    switch (selectedGameMode)
    {
    case InGameMode.OnlineDeathMatchGameMode:
    matchmakingP2PWrapper.StartP2PMatchmaking(InGameMode.OnlineDeathMatchGameMode);
    ShowLoading($"Start {teamdeathmatchInfo}...",
    $"Start {teamdeathmatchInfo} is timed out",
    matchmakingTimeoutSec);
    break;
    case InGameMode.OnlineEliminationGameMode:
    matchmakingP2PWrapper.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 == null)
    {
    matchmakingP2PWrapper = TutorialModuleManager.Instance.GetModuleClass<MatchmakingSessionP2PWrapper_Starter>();
    }
    ...
    }
  4. Now, set up the event binding in the BindMatchmakingEvent function, along with unbinding in UnbindMatchmakingEvents.

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

    matchmakingP2PWrapper.BindMatchmakingEvent();
    matchmakingP2PWrapper.OnMatchTicketP2PCreated += OnMatchTicketP2PCreated;
    matchmakingP2PWrapper.OnMatchmakingWithP2PMatchFound += OnMatchFound;
    matchmakingP2PWrapper.OnMatchmakingWithP2PTicketExpired += OnMatchmakingWithP2PTicketExpired;
    matchmakingP2PWrapper.OnMatchmakingWithP2PJoinSessionStarted += OnMatchmakingWithP2PJoinSessionStarted;
    matchmakingP2PWrapper.OnMatchmakingWithP2PJoinSessionCompleted += OnMatchmakingWithP2PJoinSessionCompleted;
    matchmakingP2PWrapper.OnMatchmakingWithP2PCanceled += OnMatchmakingWithP2PCanceledAsync;
    matchmakingP2PWrapper.OnMatchmakingWithP2PError += ErrorPanelAsync;
    matchmakingP2PWrapper.OnMatchmakingError += ErrorPanelAsync;
    matchmakingP2PWrapper.OnIntentionallyLeaveSession += Reset;
    matchmakingP2PWrapper.OnInvitedToSession += OnInvitedToGameSession;
    matchmakingP2PWrapper.OnRejectGameSessionCompleteEvent += OnSessionRejectedAsync;
    }
    private void UnbindMatchmakingEvents()
    {
    if (matchmakingP2PWrapper == null)
    {
    return;
    }

    matchmakingP2PWrapper.UnbindMatchmakingEvent();
    matchmakingP2PWrapper.OnMatchTicketP2PCreated -= OnMatchTicketP2PCreated;
    matchmakingP2PWrapper.OnMatchmakingWithP2PMatchFound += OnMatchFound;
    matchmakingP2PWrapper.OnMatchmakingWithP2PTicketExpired -= OnMatchmakingWithP2PTicketExpired;
    matchmakingP2PWrapper.OnMatchmakingWithP2PJoinSessionStarted -= OnMatchmakingWithP2PJoinSessionStarted;
    matchmakingP2PWrapper.OnMatchmakingWithP2PJoinSessionCompleted -= OnMatchmakingWithP2PJoinSessionCompleted;
    matchmakingP2PWrapper.OnMatchmakingWithP2PCanceled -= OnMatchmakingWithP2PCanceledAsync;
    matchmakingP2PWrapper.OnMatchmakingWithP2PError -= ErrorPanelAsync;
    matchmakingP2PWrapper.OnMatchmakingError -= ErrorPanelAsync;
    matchmakingP2PWrapper.OnIntentionallyLeaveSession -= Reset;
    matchmakingP2PWrapper.OnInvitedToSession -= OnInvitedToGameSession;
    matchmakingP2PWrapper.OnRejectGameSessionCompleteEvent -= OnSessionRejectedAsync;
    }
  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.CancelP2PMatchmaking();
    ShowLoading("Cancelling Match", "Cancelling match is timed out", cancelMatchmakingTimeoutSec);
    }
  6. When the match found, the player have option to accept or reject to join the game session. To accept and reject the game session, update the AcceptMatch() and RejectMatch() functions with the code below.

    private void AcceptMatch()
    {
    matchmakingP2PWrapper.AcceptSessionInvitation();
    ShowLoading("Joining Match", "Rejecting Match is timed out", cancelMatchmakingTimeoutSec);
    }
    private void RejectMatch()
    {
    matchmakingP2PWrapper.RejectSessionInvitation();
    ShowLoading("Rejecting Match", "Rejecting Match is timed out", cancelMatchmakingTimeoutSec);
    }
  7. Lastly, add the following code to display a timeout timer in case joining the P2P host takes too long. If the connection times out, the code will automatically trigger to exit the game session.

    private void OnMatchmakingWithP2PJoinSessionCompleted(bool isLeader)
    {
    if (isLeader)
    {
    ShowLoading("Hosting P2P Game", "Hosting P2P Game timed out", joinSessionTimeoutSec, matchmakingP2PWrapper.LeaveCurrentSession, false);
    }
    else
    {
    ShowLoading("Waiting for P2P Host", "Waiting for for P2P Host timed out", joinSessionTimeoutSec, matchmakingP2PWrapper.LeaveCurrentSession, false);
    }
    }

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