文档

​
​

Development

User Acquisition

Monetization

工业

Multiplayer Services SDK

All Services

Multiplayer Services SDK

此页面不支持所选语言。
Multiplayer
​
​
Multiplayer Services SDK
  • Overview
  • Get started
  • Use multiplayer sessions
  • Manage sessions
  • Connect players through a relay
  • Networking
    • Ports and protocols
    • Locations and regions
    • Quality of service
    • Scaling
    • DTLS encryption
    • Advanced configuration
      • Relay allocation service
      • Relay allocating, binding, and joining
      • Relay vs Lobby
      • Relay message protocol
      • Connection flow
      • Relay REST API
      • Manually configure Relay with Netcode for GameObjects
  • Matchmaking
  • Monitor and debug sessions
  • Tutorials
  • Reference
  1. Multiplayer Services SDK

Manually configure Relay with Netcode for GameObjects

Integrate Relay with Netcode for GameObjects using the unified Multiplayer Services SDK.
阅读时间3 分钟
最后更新于 2 个月前

Multiplayer Services work seamlessly with Netcode for GameObjects, which is a Unity package that provides networking capabilities to
GameObject
and
MonoBehaviour
workflows.
This tutorial demonstrates how to use the underlying Relay API in the Multiplayer Services package with Netcode for GameObjects.
注意
For most users, the unified Multiplayer Services package replaces the Relay standalone package, which is deprecated in Unity 6. Consider migrating to the unified package to facilitate a smooth transition. Visit the migration guide for a step-by-step transition process.

Installation and configuration

  1. In the Unity Editor's Package Manager, select Unity Registry.
  2. Search for the following package:
    com.unity.services.multiplayer
    .
  3. Select the package, then Install. Refer to Package Manager.
  4. Repeat for
    com.unity.netcode.gameobjects
    .
You must first configure your Unity project for Unity before using Relay with Netcode for GameObjects. Refer to Get started with Multiplayer Services to learn how to link your project in the project settings.

Set up the NetworkManager

After installing the packages, you can now set up the
NetworkManager
:
  1. Add a new GameObject to your scene.
  2. Add the NetworkManager MonoBehaviour.
  3. In the MonoBehaviour Properties, select the UnityTransport transport. After selecting UnityTransport, you’ll see a
    MonoBehaviour
    called Unity Transport (script) at the bottom of the components list.
  4. Set the Protocol Type of the new component to Relay Unity Transport.

Host player

The
StartHostWithRelay
function shows how to create a Relay allocation, and request a join code. This function requires the maximum number of connections the allocation is expecting and the host player connection type.
The connection type must be one of the following options:
  • udp
  • dtls
  • wss
Refer to DTLS to learn more about DTLS encryption, and to Web Platform Support to learn about using
wss
.
This code should be adapted to your needs to use a different authentication mechanism, a different error handling, or to use a different connection type. Similarly, you can call
StartServer
instead of
StartHost
to start a server instead of a host.
注意
The Multiplayer Services package introduces the concept of sessions. Check out Build a session with Netcode for GameObjects to learn how to use it.
public async Task<string> StartHostWithRelay(int maxConnections, string connectionType){ await UnityServices.InitializeAsync(); if (!AuthenticationService.Instance.IsSignedIn) { await AuthenticationService.Instance.SignInAnonymouslyAsync(); } var allocation = await RelayService.Instance.CreateAllocationAsync(maxConnections); NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(AllocationUtils.ToRelayServerData(allocation, connectionType)); var joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId); return NetworkManager.Singleton.StartHost() ? joinCode : null;}
注意
If this code succeeds in calling the
NetworkManager.Singleton.StartHost()
, it returns the join code retrieved from the Relay allocation.

Joining player

When your game client functions as a joining player, the relay join code, retrieved in the previous step when the host created the allocation, must be passed to find the allocation. The following code samples show how to join an allocation with a join code and configure the connection type.
The connection type must be one of the following options:
  • udp
  • dtls
  • wss
Refer to DTLS to learn more about DTLS encryption, and to Web Platform Support to learn about using wss.
注意
The Multiplayer Services package introduces the concept of session. Check out Build a session with Netcode for GameObjects to learn how to use it.
public async Task<bool> StartClientWithRelay(string joinCode, string connectionType){ await UnityServices.InitializeAsync(); if (!AuthenticationService.Instance.IsSignedIn) { await AuthenticationService.Instance.SignInAnonymouslyAsync(); } var allocation = await RelayService.Instance.JoinAllocationAsync(joinCode: joinCode); NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(AllocationUtils.ToRelayServerData(allocation, connectionType)); return !string.IsNullOrEmpty(joinCode) && NetworkManager.Singleton.StartClient();}

Note about Unity Web platform support

To use Relay with Netcode for GameObjects in Unity Web platform supported game, upgrade the Unity Transport Package to 2.0.0 or later and configure the Unity transport component to use Web Sockets.
Using the previous code snippets, pass wss as
connectionType
and use the following to
SetRelayServerData
:
NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(AllocationUtils.ToRelayServerData(allocation, connectionType));NetworkManager.Singleton.GetComponent<UnityTransport>().UseWebSockets = true;

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

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

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

  • 在本页上
    • Installation and configuration

    • Set up the NetworkManager

    • Host player

    • Joining player

    • Note about Unity Web platform support


报告此页面的问题