Class GhostObject
Bridge class between your GameObject and the underlying Netcode ghost entity. This is your main access point to the underlying ghost's data. It'll be in charge of initializing your GameObject's ghost and keeping it in sync with the underlying ghost.
Inherited Members
Namespace: Unity.Netcode
Assembly: Unity.NetCode.dll
Syntax
[DisallowMultipleComponent]
[DefaultExecutionOrder(-2147483647)]
[AddComponentMenu("Multiplayer/Ghost Object", 50)]
public sealed class GhostObject : BaseGhostSettings
Remarks
This uses Netcode for Entities to create a ghost entity for your GameObject. The pattern is similar to a Rigidbody associated with a GameObject. The rigidbody will have its own representation in an internal physics world and will match with its GameObject counterpart.
Fields
GhostTransform
The current non-smoothed, tick-accurate transform attached to this ghost.
Declaration
[NonSerialized]
public GhostComponentRef<LocalTransform> GhostTransform
Field Value
| Type | Description |
|---|---|
| GhostComponentRef<LocalTransform> |
SkipAutomaticPrefabRegistration
Prefabs are automatically tracked by Netcode. However this means once a prefab is registered, its settings can't be modified anymore. In order to programmatically update those settings, you need to make sure to update them before prefab registration. This setting allows you to control when prefab registration happens. Make sure to call RegisterPrefab(GameObject, NetcodeWorld) yourself.
Declaration
[Tooltip("Prefabs are automatically tracked by Netcode. However this means once a prefab is registered, its settings can't be modified anymore. In order to programmatically update those settings, you need to make sure to update them before prefab registration. This setting allows you to control when prefab registration happens. Make sure to call Netcode.RegisterPrefab yourself.")]
[SerializeField]
public bool SkipAutomaticPrefabRegistration
Field Value
| Type | Description |
|---|---|
| bool |
UseUniformScale
By default, GhostObject ghosts support (and replicate) non-uniform (per-axis) 3D scale, stored in a
PostTransformMatrix component on the ghost entity. Enable this to treat the scale as uniform
instead: no PostTransformMatrix is added, and the scale is driven by
Unity.Transforms.LocalTransform.Scale (taken from transform.localScale.x; y and z are ignored).
Saves the PostTransformMatrix chunk memory and its per-snapshot change bit on ghosts that never scale non-uniformly.
Declaration
[Tooltip("By default, GhostObject ghosts support (and replicate) non-uniform (per-axis) 3D scale, stored in a PostTransformMatrix component on the ghost entity.\n\nEnable this to treat the scale as uniform instead: no PostTransformMatrix is added, and the scale is driven by LocalTransform.Scale (taken from transform.localScale.x; y and z are ignored).\n\nSaves the PostTransformMatrix chunk memory and its per-snapshot change bit on ghosts that never scale non-uniformly.")]
[SerializeField]
public bool UseUniformScale
Field Value
| Type | Description |
|---|---|
| bool |
Properties
CanWriteState
Whether this ghost is being predicted. See PredictedGhost.
Declaration
public bool CanWriteState { get; }
Property Value
| Type | Description |
|---|---|
| bool |
DelaySpawning
By default, instantiating an object will immediately spawn that object to all clients. Set this field to true to delay spawning the object. Once this object is Instantiated, you will need to manually call Spawn() to spawn this object.
Declaration
public bool DelaySpawning { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Entity
The underlying generated ghost Unity.Entities.Entity associated with this GameObject. Updating the GhostObject's data will update the entity's data which will then be synchronized using Netcode for Entities.
Declaration
public Entity Entity { get; }
Property Value
| Type | Description |
|---|---|
| Entity |
GhostId
The networked ID for this ghost, shared across the network. ghostId Assigned before OnGhostSpawn() on the server and on client ghosts spawned from the network. A client predicted spawn has no ID until the server confirms it; reading it before that throws. GhostInfo exposes the raw value (0 while unassigned). If you plan to use other fields like SpawnTick or GhostType consider using GhostInfo instead for better batched performance
Declaration
public int GhostId { get; }
Property Value
| Type | Description |
|---|---|
| int |
GhostInfo
Various metadata associated with this ghost. GhostInstance
Declaration
public GhostInstance GhostInfo { get; }
Property Value
| Type | Description |
|---|---|
| GhostInstance |
GhostType
The ghost type (which prefab) was used to spawn this ghost. ghostType If you plan to use other fields like SpawnTick or GhostId consider using GhostInfo instead for better batched performance
Declaration
public int GhostType { get; }
Property Value
| Type | Description |
|---|---|
| int |
IsClient
Whether this ghost is spawned in a client world.
Declaration
public bool IsClient { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsServer
Whether this ghost is spawned in a server world.
Declaration
public bool IsServer { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsSpawned
Whether this ghost is currently spawned and linked to its entity.
Declaration
public bool IsSpawned { get; }
Property Value
| Type | Description |
|---|---|
| bool |
OwnerNetworkId
Declaration
public NetworkId OwnerNetworkId { get; set; }
Property Value
| Type | Description |
|---|---|
| NetworkId |
ParentReplication
When true, the parent of this GhostObject will be replicated to clients.
When false (the default), the parent will not be replicated and the OnParentChanged event will not be invoked.
Ghost parenting is prediction compatible: a client can parent a ghost that is predicting, and the authority overrides it.
A client cannot parent a ghost that is interpolating.
Declaration
public bool ParentReplication { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Position
The current non-smoothed, tick-accurate position (in local space) of the ghost.
Declaration
public Vector3 Position { get; set; }
Property Value
| Type | Description |
|---|---|
| Vector3 |
Rotation
The current non-smoothed position (in local space) of the ghost. The ghost and gameobject transform may not be necessarily in sync (i.e when switching prediction mode, when smoothing host ghosts or with prediction error smoothing).
Declaration
public Quaternion Rotation { get; set; }
Property Value
| Type | Description |
|---|---|
| Quaternion |
SpawnTick
The tick at which this ghost was spawn. spawnTick If you plan to use other fields like GhostId or GhostType consider using GhostInfo instead for better batched performance
Declaration
public NetworkTick SpawnTick { get; }
Property Value
| Type | Description |
|---|---|
| NetworkTick |
World
The world this ghost belongs to. Can be a client or server world
Declaration
public NetcodeWorld World { get; }
Property Value
| Type | Description |
|---|---|
| NetcodeWorld |
Methods
Despawn()
Despawns this GhostObject and destroys the associated GameObject it on all connected clients.
Declaration
public void Despawn()
Remarks
This function is only valid to be used if DelaySpawning is set to true. Otherwise, this object can only be despawned from the network by destroying this GhostObject.
GetHashCode()
Value-based hash of all serialized ghost settings edited via the authoring inspector. Used to detect whether an inspector edit actually changed a setting (a clamped drag can leave the value unchanged), so we only trigger a re-bake when something really changed.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int | A hash of all serialized ghost settings. |
Overrides
PublishTransform()
Takes the tick-accurate transform and applies it to the transform
Declaration
public void PublishTransform()
Spawn()
Spawns this GhostObject across the network.
Declaration
public void Spawn()
Remarks
This function is only valid to be used if DelaySpawning is set to true. Otherwise, this object will be automatically spawned over the network when the object is instantiated.
Events
OnParentChanged
Event that is invoked when a valid parent of this GhostObject changes. Only triggers when ParentReplication is enabled.
Declaration
public event Action<Transform, Transform> OnParentChanged
Event Type
| Type | Description |
|---|---|
| Action<Transform, Transform> |
Remarks
When this event is triggered it will provide the previous parent and the new parent Transform as parameters.
Parenting can be cancelled by Netcode when invalid, which means MonoBehaviour parenting changed events can be later reverted.
Use this callback for when you want to make sure a parenting change actually happened.