Hybrid GhostObjects and entities in the same project
Adopt GhostObjects in a project that already uses the entity-based Unity Netcode ghost workflow.
GhostObjects run on the same replication pipeline as the entity-based ghost workflow, so adopting them in an existing Netcode project is mostly additive. Your existing ghosts, prediction systems, and connection code continue to work, and GhostObject ghosts replicate alongside your entity ghosts. This page maps entity-based concepts to GhostObjects, covers the points you need to reconcile so the two workflows coexist cleanly, and explains how GhostBehaviour scripts and systems share state.
Use this page if you already replicate ghosts with GhostAuthoringComponent and want to author some objects as GameObjects instead. For an introduction to GhostObjects and their building blocks, refer to Introduction to GhostObjects. To replicate state with GhostObjects, refer to Replicate state over the network.
Map entity-based concepts to GhostObjects
The following table lists the GhostObject counterpart of each entity-based concept:
| Entity-based workflow | GhostObjects |
|---|---|
GhostAuthoringComponent on a prefab |
GhostObject component |
GhostFieldAttribute on a component field |
GhostField<T>, or a GhostComponentRef<T> to an IComponentData |
GhostOwner component |
Ghost.OwnerNetworkId, with Has Owner enabled |
| A system in the prediction loop | GhostBehaviour.PredictionUpdate |
IInputComponentData on a ghost |
A GhostInput<T> field using an IInputComponentData |
A system in GhostInputSystemGroup |
GhostBehaviour.PredictionGatherInput |
The NetworkTime singleton |
Netcode.Time |
| A connection entity | A Connection like Netcode.LocalConnection |
Adding NetworkStreamInGame to a connection |
Connection.EnableGhostReplication(true) |
IRpcCommand and your own RPC system |
RPC methods and custom messages |
ICustomMessage derives from IRpcCommand, so custom messages follow the same rules as entity-based RPCs. To receive an existing RPC in GameObject code, change its interface from IRpcCommand to ICustomMessage. Your systems still send and receive it. Consume each message type in one place only: a system, Netcode.RPC.Query, or a Handle method.
Add GhostObject prefabs alongside your entity ghosts
You don't need to convert or modify your existing ghosts. Unity Netcode registers GhostObject prefabs into the same ghost collection as your baked entity ghost prefabs, and both replicate through the same pipeline.
Create your GameObject-based ghosts and replicate their state as described in Replicate state over the network. Your GhostObject ghosts and entity ghosts then coexist in the same world. For more information about ghost replication, refer to Synchronization with ghosts.
Reconcile world creation and connection
An existing Netcode project usually creates its client and server worlds through a bootstrap or its own offline-to-online flow, and connects them with its own code. The Netcode class can do the same, but it's an optional convenience rather than a requirement. It doesn't replace ClientServerBootstrap, and you can use both: the bootstrap decides which worlds exist at startup, and the Netcode class gives you static access to runtime operations such as connecting. For more information about the Netcode class, refer to Manage connections.
The Netcode methods reuse your existing worlds. Netcode.Connect uses ClientServerBootstrap.ClientWorld, and Netcode.Listen and Netcode.StartAsHost use ClientServerBootstrap.ServerWorld. They create a world only when it doesn't exist yet, so you can adopt the Netcode class gradually or not at all. For more information about connecting worlds, refer to Connecting server and clients.
To control which world connects, call NetcodeWorld.Connect() or NetcodeWorld.Listen() on the world itself.
If you adopt the Netcode class, make sure your bootstrap doesn't also create and connect the worlds. For more information, refer to Let the Netcode class create the worlds.
Target a specific world
The static Netcode members track the active world for you. Netcode sets the active world when SimulationSystemGroup begins, and clears it when SimulationSystemGroup ends. When one process runs several worlds, such as a binary-worlds host or thin clients, get the world from the object you work with instead:
Ghost.Worldis theNetcodeWorlda ghost belongs to.Connection.Worldis the world a connection entity belongs to, andConnection.ConnectionEntityis its connection entity. On a server, every client connection belongs to the server world.NetcodeWorld.OnConnectionEventreports the connection events of one world only, unlikeNetcode.OnConnectionEvent.NetcodeWorld.Connect,NetcodeWorld.Listen, andNetcodeWorld.Shutdownact on one world only.
Netcode.Time reads the time of the world that's updating, so inside a prediction method it matches Ghost.World. Outside a world update, for example in Update, read Ghost.World.NetworkTime to get the time of the ghost's world.
To send a custom message to the clients of one world, pass that world and a SendDirection to Netcode.RPC.Invoke. The following example sends a message from a specific server world to its clients:
Netcode.RPC.Invoke(
new ScoreUpdate { PlayerNetworkId = 2, Score = 40 },
serverWorld,
SendDirection.ServerToClient);
On a single-world host, this also reaches the host's own client, which Netcode.AllConnections doesn't include. On the receiving side, Netcode.RPC.Query<T>(world) returns only the messages received in that world. Without an argument, Query<T>() reads every client and server world, but not thin client worlds.
To register a prefab in one world only, call Netcode.RegisterPrefab(prefab, world). Netcode.RegisterPrefab(prefab) registers it in every client, server, and thin client world, including worlds created later.
Share state between GhostBehaviour scripts and systems
A GhostBehaviour and your systems can work on the same ghost entity.
Choose how to declare replicated state
A GhostBehaviour declares replicated state in one of two ways:
| Field type | Use it when |
|---|---|
GhostField<T> |
Only GhostBehaviour code uses the value. Netcode generates the component behind it, so you can't access that component from your own systems. |
GhostComponentRef<T> |
Your GhostBehaviour scripts and systems share an IComponentData. It points to your own IComponentData on the ghost entity, which systems can read and write like any other component. |
GhostField<T> is the GameObject counterpart of GhostFieldAttribute, but the two aren't interchangeable. In a GhostBehaviour, declare the field as GhostField<T>. Apply GhostFieldAttribute to the fields of an IComponentData or IBufferElementData struct, including the struct behind a GhostComponentRef<T>. To set serialization options such as Quantization on a GhostField<T>, pass a FieldConfig to its constructor. FieldConfig has the same properties as GhostFieldAttribute.
GhostComponentRef<T>.Read() returns a copy of the component, so pass it to Write() after you change it. Two GhostBehaviour scripts on the same ghost that declare a GhostComponentRef<T> of the same type share one component on the underlying entity.
Access the ghost entity
Each GhostObject maps to one ghost entity. Ghost.Entity returns the associated entity, and Ghost.World returns the associated world. The components of every GhostBehaviour on the GameObject and its children are on the associated entity. A child GameObject that only has GhostBehaviour scripts gets no entity of its own, so Netcode doesn't replicate its transform.
Note
Nested GhostObject components in the same prefab aren't supported. To replicate a child's transform, make the child a separate ghost prefab with its own GhostObject component, spawn it, and attach it to the parent ghost with Parent Replication.
The entity's LocalTransform holds the tick-accurate position and rotation. Ghost.GhostTransform is a GhostComponentRef<LocalTransform> to it, and Ghost.Position and Ghost.Rotation read it. The scale is in a PostTransformMatrix component, unless you enable Uniform Scale. In that case, the entity has no PostTransformMatrix, and LocalTransform.Scale holds the scale.
Access replicated data from jobs
You can't use Netcode or GhostField<T>.Value in jobs. To work with replicated data in a Burst-compiled job, use a GhostComponentRef<T> and access its component with the usual Entities APIs, such as IJobEntity or ComponentLookup<T>.