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

Put it all together - Create joinable sessions with peer-to-peer - (Unity module)

Last updated on July 28, 2025
備考

AccelByte Gaming Services (AGS) SDK for Unity does not support P2P on WebGL. This module cannot be used in WebGL builds.

Connect the UI to create and join match sessions

  1. Open CreateMatchSessionP2PMenu_Starter class and define a variable to store the wrapper you created.

    private MatchSessionP2PWrapper_Starter matchSessionP2PWrapper;
  2. Replace the CreateMatchSession() function with the code below. This function sends a request to create a new game session using the session template based on the selected game mode. The session template is retrieved using GetGameSessionRequestModel() from the AccelByteWarsOnlineSessionModels class.

    private void CreateMatchSession()
    {
    // Get the session template name from the game config.
    SessionV2GameSessionCreateRequest request =
    AccelByteWarsOnlineSessionModels.GetGameSessionRequestModel(CreateMatchSessionMenu.SelectedGameMode, GameSessionServerType.PeerToPeer);
    if (request == null)
    {
    BytewarsLogger.LogWarning("Failed to create match session. Session type is not supported.");
    stateSwitcher.ErrorMessage = InvalidSessionTypeMessage;
    stateSwitcher.SetState(MatchSessionMenuState.Error);
    return;
    }

    // Create a new match session.
    stateSwitcher.SetState(MatchSessionMenuState.CreateMatch);
    matchSessionP2PWrapper.CreateGameSession(request, OnJoinSessionComplete);
    }
  3. Replace the JoinMatchSession() function with the code below. This function sends a request to join a game session by its session ID.

    private void JoinMatchSession(string sessionId)
    {
    stateSwitcher.SetState(MatchSessionMenuState.JoinMatch);
    matchSessionP2PWrapper.JoinGameSession(sessionId, OnJoinSessionComplete);
    }
  4. Create the callback function to switch the menu to the corresponding state when the game session is created or joined.

    private void OnJoinSessionComplete(Result<SessionV2GameSession> result)
    {
    // Abort if already attempt to cancel join the match.
    if (stateSwitcher.CurrentState is
    not (MatchSessionMenuState.CreateMatch or MatchSessionMenuState.JoinMatch))
    {
    return;
    }

    if (result.IsError)
    {
    stateSwitcher.ErrorMessage = result.Error.Message;
    stateSwitcher.SetState(MatchSessionMenuState.Error);
    return;
    }

    stateSwitcher.SetState(MatchSessionMenuState.JoinedMatch);
    }
  5. Replace the LeaveMatchSession() function with the code below. This function checks whether there is a valid session to leave. If not, it simply close the menu and return to the previous menu.

    private void LeaveMatchSession(string sessionId)
    {
    // Simply return from the menu if the session is empty.
    if (string.IsNullOrEmpty(sessionId))
    {
    MenuManager.Instance.OnBackPressed();
    return;
    }

    stateSwitcher.SetState(MatchSessionMenuState.LeaveMatch);
    matchSessionP2PWrapper.LeaveGameSession(sessionId, OnLeaveMatchSessionComplete);
    }
  6. Create the callback function to handle when leave session completes. If the request fails, switch to error state. Otherwise, close the menu and return to the previous menu.

    private void OnLeaveMatchSessionComplete(Result result)
    {
    if (result.IsError && result.Error.Code != ErrorCode.SessionIdNotFound)
    {
    stateSwitcher.ErrorMessage = result.Error.Message;
    stateSwitcher.SetState(MatchSessionMenuState.Error);
    return;
    }

    MenuManager.Instance.OnBackPressed();
    }
  7. Finally, replace the pre-defined OnEnable() function with the code below. This code initialize the wrapper first then determine whether to create or join a game session when the menu is displayed. It also rebinds the retry and cancel buttons accordingly.

    private void OnEnable()
    {
    matchSessionP2PWrapper ??= TutorialModuleManager.Instance.GetModuleClass<MatchSessionP2PWrapper_Starter>();
    if (!matchSessionP2PWrapper)
    {
    return;
    }

    if (sessionToJoin == null)
    {
    stateSwitcher.OnRetryButtonClicked = CreateMatchSession;
    stateSwitcher.OnCancelButtonClicked = () => LeaveMatchSession(AccelByteWarsOnlineSession.CachedSession.id);
    CreateMatchSession();
    }
    else
    {
    stateSwitcher.OnRetryButtonClicked = () => JoinMatchSession(sessionToJoin.Session.id);
    stateSwitcher.OnCancelButtonClicked = () => LeaveMatchSession(sessionToJoin.Session.id);
    JoinMatchSession(sessionToJoin.Session.id);
    }
    }

Resources