A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.unity.com/ugs/en-us/manual/mps-sdk/manual/create-session below:

Create a session

Create a session#

Note: The Multiplayer Services SDK uses sessions to manage groups of players. Sessions relies internally on different combinations of Unity Gaming Services such as Relay, Distributed Authority, Lobby, Matchmaker and Multiplay Hosting, and thus contributes to the billing of those services.

To start a multiplayer game, the host must create a session that other players join to participate in the game. This page outlines what you must include in your script to create a session. Consider the following factors when you create a session:

Note: To access the API for the Multiplayer Services SDK, import the SDK's namespace in your script:

using Unity.Services.Multiplayer;
The CreateOrJoinSessionAsync method#

The CreateOrJoinSessionAsync method either joins the session with the given ID, or if it does not exist, creates the session. This is convenient in cases where race condition can happen between clients and you are not sure which client will create the session.

var session = await MultiplayerService.Instance.CreateOrJoinSessionAsync(sessionId, options)
Session options properties#

The session options determine various properties of your session, such as the maximum number of players or whether the session is password protected.

Refer to Class SessionOptions for a full list of options.

Type of network connection#

Another consideration for your session is the network connection type with which the players join the session. Players can join with a direct network connection, or via a client hosted solution provided by Unity (Relay or Distributed Authority) Using one of our client hosted solutions for network connection provides the most compatibility, and is the recommended best practice for peer-to-peer games played over the Internet.

Refer to Class SessionOptions > Methods for more information.

Example#

Consider the following example code you might add to your script to create a session with a Unity client-hosted solution.

async void StartSessionAsHost()
{
    var options = new SessionOptions
    {
      MaxPlayers = 2
    }.WithRelayNetwork(); // or WithDistributedAuthorityNetwork() to use Distributed Authority instead of Relay
    var session = await MultiplayerService.Instance.CreateSessionAsync(options);
    Debug.Log($"Session {session.Id} created! Join code: {session.Code}");
}

In this code:

Advanced usage: Specifying a custom network handler#

It is possible to specify a custom network handler implementing INetworkHandler, overriding the default integration with Netcode for Game Objects and Netcode for Entities.

var options = new SessionOptions { MaxPlayers = 2 }
        .WithRelayNetwork()
        .WithNetworkHandler(new CustomEntitiesNetworkHandler());

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4