I’m running into an issue setting up Unity Transport after configuring Relay.
Here is the code:
var data = await relayConnection.HostGame(1);
var utp = NetworkManager.Singleton.GetComponent<UnityTransport>();
utp.SetHostRelayData(data.IPv4Address, data.Port, data.AllocationIDBytes, data.Key, data.ConnectionData, true);
Debug.Log(utp.ConnectionData.Address + " vs: " + data.IPv4Address);
Debug.Log(utp.ConnectionData.Port + " vs: " + data.Port);
After relayConnection.HostGame(1), which I simply took from the Netcode documentation, I get the JoinCode, I can see the allocation in the dashboard and I can even get the client to connect to it.
However, after calling SetHostRelayData and NetworkManager.Singleton.StartHost(), even though the internal m_RelayServerData parameters are being set, the NetworkManager doesn’t actually connect to the Client (ie, I don’t see any connected clients, and the NetworkManager on the client side says disconnected)
Question
Is there something else needed to make them setup correctly?
Also, the source code for netcode seems to suggest that ConnectionData is ignored if Relay is used (see here and snippet below), but SetClientRelayData does set the ConnectionData properties, which is why I initially thought that SetHostRelayData wasn’t setting anything. I can’t figure out where in the code the distinction is, as SetClientRelayData calls the same method internally as SetHostRelayData.
SetConnectionData
/// <summary>
/// Sets IP and Port information. This will be ignored if using the Unity Relay and you should call <see cref="SetRelayServerData"/>
/// </summary>
/// <param name="endPoint">The remote end point</param>
/// <param name="listenEndPoint">The local listen endpoint</param>
public void SetConnectionData(NetworkEndPoint endPoint, NetworkEndPoint listenEndPoint = default)
HostGame (from docs)
public async Task<RelayHostData> HostGame(int maxConn)
{
//Initialize the Unity Services engine
await UnityServices.InitializeAsync();
//Always autheticate your users beforehand
if (!AuthenticationService.Instance.IsSignedIn)
{
//If not already logged, log the user in
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
//Ask Unity Services to allocate a Relay server
Allocation allocation = await Unity.Services.Relay.RelayService.Instance.CreateAllocationAsync(maxConn);
//Populate the hosting data
RelayHostData data = new RelayHostData
{
// WARNING allocation.RelayServer is deprecated
IPv4Address = allocation.RelayServer.IpV4,
Port = (ushort)allocation.RelayServer.Port,
AllocationID = allocation.AllocationId,
AllocationIDBytes = allocation.AllocationIdBytes,
ConnectionData = allocation.ConnectionData,
Key = allocation.Key,
};
//Retrieve the Relay join code for our clients to join our party
data.JoinCode = await Unity.Services.Relay.RelayService.Instance.GetJoinCodeAsync(data.AllocationID);
return data;
}