Use Relay with Netcode for GameObjects
Integrate Relay with Netcode for GameObjects using the unified Multiplayer Services SDK.
Read time 3 minutesLast updated 14 hours ago
Relay works seamlessly with Netcode for GameObjects, which is a Unity package that provides networking capabilities to
GameObjectMonoBehaviourInstallation and configuration
- In the Unity Editor's Package Manager, select Unity Registry.
- Search for the following package: .
com.unity.services.multiplayer - Select the package, then Install. Refer to Package Manager.
- Repeat for .
com.unity.netcode.gameobjects
Set up the NetworkManager
After installing the packages, you can now set up theNetworkManager- Add a new GameObject to your scene.
- Add the NetworkManager MonoBehaviour.
- In the MonoBehaviour Properties, select the UnityTransport transport. After selecting UnityTransport, you’ll see a called Unity Transport (script) at the bottom of the components list.
MonoBehaviour - Set the Protocol Type of the new component to Relay Unity Transport.
Host player
TheStartHostWithRelay- udp
- dtls
- wss
wssStartServerStartHostpublic 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;}
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
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 above code snippets, pass wss asconnectionTypeSetRelayServerDataNetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(AllocationUtils.ToRelayServerData(allocation, connectionType));NetworkManager.Singleton.GetComponent<UnityTransport>().UseWebSockets = true;