Thin clients
Understand how thin clients simulate additional players in the Unity Editor so you can test multiplayer interactions without extra hardware or testers.
Design thin clients to run as little logic as possible so that they don't put a heavy load on the CPU while testing. Each additional thin client adds extra work to compute each frame.
To create thin clients, set Num Thin Clients in the PlayMode Tool window, or create thin client worlds from a class that extends ClientServerBootstrap, as described in Client and server worlds networking model. This page describes which systems run on thin client worlds, when to use thin clients, and how to set up their inputs.
Thin client system filtering
Only systems that you explicitly mark with the WorldSystemFilterFlags.ThinClientSimulation flag run on thin client worlds. Unity doesn't render thin client data, so thin clients are invisible to the presentation.
In some cases, you might need to check whether your system logic runs for thin clients, and then early out or cancel processing. Use the World.IsThinClient() extension methods in these cases. Note that World.IsClient returns true for both thin and full clients.
Thin client workflow recommendations
You can use thin clients in a variety of ways to help test multiplayer games. Consider the following use cases:
- Use thin clients to quickly test client flows such as team assignment, spawn locations, and leaderboards.
- Use thin clients to create builds with large numbers of simulated players, allowing stress and soak testing of your game servers. For example, you can add a configuration option to automatically create
nthin client worlds (alongside your normal client world) and have each thin client "follow the leader" and automatically attempt to join the same IP address and port as your main client world. Thus, you can use your existing UI flows to get these thin clients into the stress test target server. - Use thin clients controlled by a second input source. Multiplayer games often have complex PvP interactions, and therefore you often wish to have an AI perform a specific action while your client is interacting with it. For example: crouch, go prone, jump, run diagonally backwards, reload, enable shield, activate ability, and so on. Hooking thin client controls up to keyboard commands allows you to test these situations without requiring a play-test (or a second developer). You can also use thin clients to mirror inputs of the tester, with similarly good results.
Set up inputs for thin clients
Thin clients don't work with AutoCommandTarget by default, because AutoCommandTarget requires the same ghost to exist on both the client and the server, and thin clients don't create ghosts. So you need to set up the CommandTarget component on the connection entity yourself.
IInputComponentData is the primary input API. It automatically handles writing inputs (from your input struct) directly to the replicated dynamic buffer. When you bake a ghost entity that contains an IInputComponentData composed struct, Unity automatically adds an ICommandData dynamic buffer to the entity. However, this baking process isn't available on thin clients, because thin clients don't create ghost entities.
ICommandData is also supported with thin clients (details here), but you'll need to perform the same thin client set up work described below as with IInputComponentData.
Send input from a thin client
To send input from a thin client, follow these steps:
- Create an entity containing your
IInputComponentData(orICommandData) component and the code-generatedInputBufferData<YourInputComponent>dynamic buffer. This might appear to throw a missing assembly definition error in your IDE, but it will work. - If you use
IInputComponentData, add aGhostOwnercomponent to this entity and set itsNetworkIdfield to the thin client's ownNetworkId.- Unity Netcode only copies an
IInputComponentDatainto its command buffer if the entity has aGhostOwnerwhoseNetworkIdmatches the connection. If you omit the component, or leaveNetworkIdat its default value of0, the thin client sends no input and Unity Netcode doesn't report an error.
- Unity Netcode only copies an
- Set up the
CommandTargetcomponent to point to this entity. In a[WorldSystemFilter(WorldSystemFilterFlags.ThinClientSimulation)]system:var myDummyGhostCharacterControllerEntity = state.EntityManager.CreateEntity( typeof(MyNamespace.MyInputComponent), typeof(InputBufferData<MyNamespace.MyInputComponent>), typeof(GhostOwner)); var myConnectionEntity = SystemAPI.GetSingletonEntity<NetworkId>(); // Inputs are only collected from entities whose GhostOwner matches this connection. state.EntityManager.SetComponentData( myDummyGhostCharacterControllerEntity, new GhostOwner { NetworkId = SystemAPI.GetSingleton<NetworkId>().Value }); // This tells the netcode package which entity it should be sending inputs for. state.EntityManager.SetComponentData( myConnectionEntity, new CommandTarget { targetEntity = myDummyGhostCharacterControllerEntity }); // The input entity is only needed once, so stop this system from creating another every frame. state.Enabled = false;
After the thin client connects and enters the game, the server receives its input in the same command stream as a full client. If the server receives no input from the thin client, check two things. The input entity must have a GhostOwner whose NetworkId matches the value on the thin client's connection entity, and CommandTarget.targetEntity must point to that same entity.
Set the command target on the server
On the server, you spawn the actual character controller ghost for the thin client, which Unity Netcode replicates to all full clients. If your player ghosts use AutoCommandTarget, you only need to set the CommandTarget for thin clients. If you don't use AutoCommandTarget, you probably already set it for all clients.
state.EntityManager.SetComponentData(thinClientConnectionEntity, new CommandTarget { targetEntity = thinClientsCharacterControllerGhostEntity });