docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Customizing replication with GhostComponentAttribute

    Use GhostComponentAttribute and its properties to customize how replication is handled by your runtime.

    You must annotate your components with GhostFieldAttribute to mark them for serialization and replication before you can use GhostComponentAttribute.

    GhostComponentAttribute properties

    Use these properties to customize how replication is modified using GhostComponentAttribute. For more details, refer to the GhostComponentAttribute API documentation.

    Property Default value Description
    OwnerSendType All Use the OwnerSendType property to specify (using SendToOwnerType) which group of clients to replicate the component to. For example, you can use this property to replicate input commands only to other players (as you already known your own). Refer to OwnerSendType details.
    PrefabType All Use the PrefabType property to specify (using GhostPrefabType) which versions of the prefab the component should be available on. For example, you can use this property to remove rendering-related components from the server world's version of the ghost. Refer to PrefabType details.
    SendDataForChildEntity false Use the SendDataForChildEntity property to specify whether to replicate the component when it's attached to a child of a ghost entity. Replicating the children of ghost entities is significantly slower than replicating the parent ghost entities. This property also applies to [GhostEnabledBit]. Refer to SendDataForChildEntity details.
    SendTypeOptimization AllClients Use the SendTypeOptimization property to specify (using GhostSendType) whether to replicate the component when the ghost is predicted or interpolated. For example, you can use this property to only replicate PhysicsVelocity when actually predicting the physics of a ghost. Refer to SendTypeOptimization details.
    [GhostComponent(PrefabType=GhostPrefabType.All, SendTypeOptimization=GhostSendType.OnlyInterpolatedClients, SendDataForChildEntity=false)]
    public struct MyComponent : IComponentData
    {
        [GhostField(Quantized=1000)] public float3 Value;
    }
    

    OwnerSendType details

    To specify which clients a component is replicated to (based on ownership), use the OwnerSendType property of GhostComponentAttribute. OwnerSendType can be one of the following, as defined by SendToOwnerType.

    • None: The component isn't replicated to any clients.
    • All: The component is replicated to all clients.
    • SendToOwner: The component is only replicated to the client that owns the ghost.
    • SendToNonOwner: The component is replicated to all clients except the client that owns the ghost.

    PrefabType details

    To specify which versions of a ghost prefab a component is available on, use the PrefabType property of GhostComponentAttribute. PrefabType can be one of the following, as defined by GhostPrefabType.

    • None: The component isn't available on any ghost prefab type.
    • All: The component is available on the server and all clients.
    • Server: The component is only available on the server.
    • Client: The component is only available on clients, regardless of whether the ghost is predicted or interpolated.
    • AllPredicted: The component is available on the server and on clients when the ghost is predicted.
    • PredictedClient: The component is only available on clients when the ghost is predicted.
    • InterpolatedClient: The component is only available on clients when the ghost is interpolated.

    For example, if you add [GhostComponent(PrefabType=GhostPrefabType.Client)] to RenderMesh, the ghost won't have a RenderMesh when it's instantiated on the server world, but will have one when instantiated on the client world.

    Note

    Runtime prediction switching therefore has the potential to add and remove components on a ghost live as the prediction mode changes.

    SendDataForChildEntity details

    Use the SendDataForChildEntity property of GhostComponentAttribute to specify whether to replicate the component when it's attached to a child of a ghost entity. Note that serializing and replicating child entities is computationally expensive, so this property defaults to false.

    SendTypeOptimization details

    To specify which clients a component is replicated to (based on whether the ghost is interpolated or predicted on that client), use the SendTypeOptimization property of GhostComponentAttribute. SendTypeOptimization can be one of the following, as defined by GhostSendType.

    • DontSend: The component isn't replicated to any clients. Netcode for Entities won't modify the component on clients which don't receive it.
    • AllClients: The component is replicated to all clients.
    • OnlyPredictedClients: The component is only replicated to clients that are predicting the ghost.
    • OnlyInterpolatedClients: The component is only replicated to clients that are interpolating the ghost.
    Note

    Setting either SendTypeOptimization and/or OwnerSendType (to specify to which types of client(s) the component should be replicated to) won't affect the presence of the component on the prefab, nor modify the component on the clients which didn't receive it.

    Additional resources

    • GhostComponentAttribute API documentation
    • Serializing and synchronizing with GhostFieldAttribute
    • SendToOwnerType API documentation
    • GhostPrefabType API documentation
    • GhostSendType API documentation
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)