Use Relay with Netcode for GameObjects (Relay SDK)

Important: If you're using the unified Multiplayer Services package, refer to Use Relay with Netcode for GameObjects.

The following instructions demonstrate how to use Relay with Netcode for GameObjects using the standalone Relay SDK.

Note: 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

These steps describe a deprecated process. Refer to Use Relay with Netcode for GameObjects for the latest instructions.

  1. In the Unity Editor's Package Manager, select Unity Registry.
  2. Search for the following package:com.unity.services.relay.
  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 Relay to learn how to link your project in the project settings.

Set up the NetworkManager

These steps describe a deprecated process. Refer to Use Relay with Netcode for GameObjects for the latest instructions.

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

These steps describe a deprecated process. Refer to Use Relay with Netcode for GameObjects for the latest instructions.

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.

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(new RelayServerData(allocation, connectionType));
    var joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
    return NetworkManager.Singleton.StartHost() ? joinCode : null;
}

Note: If this code succeeds in calling the NetworkManager.Singleton.StartHost(), it returns the join code retrieved from the Relay allocation.

Joining player

These steps describe a deprecated process. Refer to Use Relay with Netcode for GameObjects for the latest instructions.

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.

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);
    NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(new RelayServerData(allocation, connectionType));
    return !string.IsNullOrEmpty(joinCode) && NetworkManager.Singleton.StartClient();
}

Note about Unity Web platform support

These steps describe a deprecated process. Refer to Use Relay with Netcode for GameObjects for the latest instructions.

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 as connectionType and use the following to SetRelayServerData:

NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(new RelayServerData(allocation, connectionType));
NetworkManager.Singleton.GetComponent<UnityTransport>().UseWebSockets = true;