文档

​
​

Development

User Acquisition

Monetization

工业

Multiplayer Services SDK

All Services

Multiplayer Services SDK

此页面不支持所选语言。
Multiplayer
​
​
Multiplayer Services SDK
  • Overview
  • Get started
  • Use multiplayer sessions
    • Integrate Multiplayer sessions
      • Initialize UGS and authenticate the player
      • Create a session
      • Create sessions in the Editor
      • Find and join a session
      • Matchmaking
      • Migrate session host
      • Leave a session
      • Get a list of joined sessions
      • Delete a session
      • Session configuration options
    • Game server hosting support
  • Manage sessions
  • Connect players through a relay
  • Networking
  • Matchmaking
  • Monitor and debug sessions
  • Tutorials
  • Reference
  1. Multiplayer Services SDK

Create a session

Configure session options and choose network connection types to add multiplayer support to your project.
阅读时间2 分钟
最后更新于 3 天前

To start a multiplayer game, the host must create a session that other players join to participate in the game. A session represents a group of players that are connected together for a set period of time during a multiplayer game. This page outlines what you must include in your script to create a session.
注意
To access the API for the Multiplayer Services SDK, import the SDK's namespace in your script:
using Unity.Services.Multiplayer;

The CreateOrJoinSessionAsync method

Use the
CreateOrJoinSessionAsync
method to do the following:
  • Join an existing session with the given ID.
  • Create a session if the ID doesn't exist.
CreateOrJoinSessionAsync
will create a session if it doesn't exist yet, while subsequent calls put users into the created session. This approach ensures that only one session is created when multiple clients attempt to join the same session, often referred to as race conditions.
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.

Example: Creating a session with a client-hosted solution

The following example demonstrates how to create a session using a Unity client-hosted solution:
async Task 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}");}
Note the following points from the previous code:
  • The variable
    MaxPlayers
    sets the maximum number of players allowed in the session, including the host. In this example, the maximum number of players is two.
  • The variable options of class
    SessionOptions
    can determine other session options, such as the session’s name, and whether it is password protected.
  • The use of
    WithRelayNetwork()
    (or
    WithDistributedAuthorityNetwork()
    ) configures the session to use Relay (or Distributed Authority) networking rather than direct connections (in other words, with a fixed IP address and port). This is the recommended best practice for peer-to-peer connectivity over the Internet. Refer to Relay or Distributed Authority for more information.
  • The line
    var session = await MultiplayerService.Instance.CreateSessionAsync(options);
    creates the session, and makes the player the host.
  • The final line,
    Debug.Log($"Session {session.Id} created! Join code: {session.Code}");
    , displays the session ID and join code in the console of the Unity Editor. The host would then share this join code with the other players. Alternatively, you could create a variable to get the join code, as in
    var joinCode = session.Code;
    and use that variable instead.

Additional resources

  • Create sessions in the Editor using ScriptableObjects
  • Network connection types
  • Network connection options
  • Netcode separation

Copyright © 2026 Unity Technologies
法律信息隐私政策CookiesDocumentation Terms of Use请勿出售或分享我的个人信息您的隐私选择(Cookie 设置)

“Unity”、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属公司在美国和其他地方的商标或注册商标(此处查看更多信息)。其他名称或品牌是其各自所有者的商标。

为方便起见,一些页面是机器翻译的,可能包含不准确的内容。如有信息不一致的情况,以英文版本为准。

  • 在本页上
    • The CreateOrJoinSessionAsync method

    • Session options properties

    • Example: Creating a session with a client-hosted solution

    • Additional resources


报告此页面的问题