ドキュメント

​
​

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
法規事項プライバシーポリシークッキーDocumentation Terms of Use私の個人情報を販売または共有しないプライバシーに関する選択 (クッキー設定)

"Unity" の名称、Unity のロゴ、およびその他の Unity の商標は、米国およびその他の国における Unity Technologies またはその関係会社の商標または登録商標です (詳しくはこちら)。その他の名称またはブランドは該当する所有者の商標です。

一部のページは利便性向上のため機械翻訳を使用しており、内容に不正確な表現が含まれる場合があります。内容に齟齬または不一致が生じた場合は、英語版を正本とします。

  • このページ
    • Installation and configuration

    • Set up the NetworkManager

    • Host player

    • Joining player

    • Note about Unity Web platform support


このページの問題を報告する