Switching over to the RelayUnityTransport component, although I'm still using the other Relay allocation to send the NGO relay code so that needs to change. Also note that consecutive games in one lobby fail now, for using the wrong relay code.
privateulongm_localId;// This is not necessarily the same as the OwnerClientId, since all clients will see all spawned objects regardless of ownership.
Locator.Get.Messenger.OnReceiveMessage(MessageType.GameBeginning,null);// TODO: Might need to delay this a frame to ensure the client finished initializing (since I sometimes hit the "failed to join" message even when joining).
m_serverAddress=changed.RelayServer;// Note that this could be null.
m_lobby=lobby;// Most of the time this is redundant, but we need to get multiple members of the lobby to the Relay setup components, so might as well just hold onto the whole thing.
publicvoidOnLocalUserChange(LobbyUseruser)
{
m_isHost=user.IsHost;
Locator.Get.Messenger.OnReceiveMessage(MessageType.DisplayErrorPopup,"Failed to join the game.");
// TODO: Need to handle both failing to connect and connecting but failing to initialize.
// I.e. cleaning up networked objects *might* be necessary.
OnGameEnd();// TODO: This returns to the lobby. I think that's desirable?
{this.enabled=false;// Just disabling this script.
return;
}
m_rb=this.GetComponent<Rigidbody>();
Locator.Get.Messenger.Subscribe(this);
GetComponent<NetworkObject>().Spawn();
}
publicoverridevoidOnNetworkSpawn()
// Note: The SymbolObjects, which will be children of this object, need their NetworkTransforms to have IsLocalSpace set to true. Otherwise, they might get desynced.
// (This would manifest as packet loss errors.)
// Also note: The initial position of the SymbolObject prefab is set to be outside the camera view in the Z-direction, so that it doesn't interpolate past the actual
// position when it spawns on a client (as opposed to in the Y-direction, since this SymbolContainer is also moving downward).
// TODO: Does that matter if we delay for the instructions?
Rigidbodym_rb=this.GetComponent<Rigidbody>();
m_isConnected=true;
m_rb.velocity=Vector3.down;
if(m_hasGameStarted)
BeginMotion();
}
}
privatevoidBeginMotion()
{
// Note: The SymbolObjects, which will be children of this object, need their NetworkTransforms to have IsLocalSpace set to true. Otherwise, they might get desynced.
// (This would manifest as packet loss errors.)
// Also note: The initial position of the SymbolObject prefab is set to be outside the camera view in the Z-direction, so that it doesn't interpolate past the actual
// position when it spawns on a client (as opposed to in the Y-direction, since this SymbolContainer is also moving downward).
RelayCode=lobby.Data?.ContainsKey("RelayCode")==true?lobby.Data["RelayCode"].Value:null,// By providing RelayCode through the lobby data with Member visibility, we ensure a client is connected to the lobby before they could attempt a relay connection, preventing timing issues between them.
if(joinAllocation==null||this==null)// The returned JoinAllocation is null if allocation failed. this would be destroyed already if you quit the lobby while Relay is connecting.