docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class GhostFieldAttribute

    Attribute used to specify how and which fields and properties of Unity.Entities.IComponentData or Unity.Entities.IBufferElementData should be replicated. When a component or buffer contains at least one field that is annotated with a GhostFieldAttribute, a struct implementing the component serialization is automatically code-generated.

    Inheritance
    object
    Attribute
    GhostFieldAttribute
    Inherited Members
    Attribute.Equals(object)
    Attribute.GetCustomAttribute(Assembly, Type)
    Attribute.GetCustomAttribute(Assembly, Type, bool)
    Attribute.GetCustomAttribute(MemberInfo, Type)
    Attribute.GetCustomAttribute(MemberInfo, Type, bool)
    Attribute.GetCustomAttribute(Module, Type)
    Attribute.GetCustomAttribute(Module, Type, bool)
    Attribute.GetCustomAttribute(ParameterInfo, Type)
    Attribute.GetCustomAttribute(ParameterInfo, Type, bool)
    Attribute.GetCustomAttributes(Assembly)
    Attribute.GetCustomAttributes(Assembly, bool)
    Attribute.GetCustomAttributes(Assembly, Type)
    Attribute.GetCustomAttributes(Assembly, Type, bool)
    Attribute.GetCustomAttributes(MemberInfo)
    Attribute.GetCustomAttributes(MemberInfo, bool)
    Attribute.GetCustomAttributes(MemberInfo, Type)
    Attribute.GetCustomAttributes(MemberInfo, Type, bool)
    Attribute.GetCustomAttributes(Module)
    Attribute.GetCustomAttributes(Module, bool)
    Attribute.GetCustomAttributes(Module, Type)
    Attribute.GetCustomAttributes(Module, Type, bool)
    Attribute.GetCustomAttributes(ParameterInfo)
    Attribute.GetCustomAttributes(ParameterInfo, bool)
    Attribute.GetCustomAttributes(ParameterInfo, Type)
    Attribute.GetCustomAttributes(ParameterInfo, Type, bool)
    Attribute.GetHashCode()
    Attribute.IsDefaultAttribute()
    Attribute.IsDefined(Assembly, Type)
    Attribute.IsDefined(Assembly, Type, bool)
    Attribute.IsDefined(MemberInfo, Type)
    Attribute.IsDefined(MemberInfo, Type, bool)
    Attribute.IsDefined(Module, Type)
    Attribute.IsDefined(Module, Type, bool)
    Attribute.IsDefined(ParameterInfo, Type)
    Attribute.IsDefined(ParameterInfo, Type, bool)
    Attribute.Match(object)
    Attribute.TypeId
    object.Equals(object, object)
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    Namespace: Unity.NetCode
    Assembly: Unity.NetCode.dll
    Syntax
    [AttributeUsage(AttributeTargets.Property|AttributeTargets.Field)]
    public class GhostFieldAttribute : Attribute
    Remarks

    Note that "enableable components" (Unity.Entities.IEnableableComponent) will still have their fields replicated, even when disabled. See GhostEnabledBitAttribute to replicate the enabled flag itself.

    Properties

    Composite

    Only applicable on GhostFieldAttributes applied to a non primitive struct containing multiple fields. If this value is not set (a.k.a. false, the default), a 'change bit' will be included 'per field, for every field inside the nested struct'. There will be no 'change bit' for the struct itself. I.e. If a single field inside the sub-struct changes, only that fields 'change bit' will be set. Otherwise (if this Composite bool is set, a.k.a. true), we instead use a single 'change bit' for 'the entire nested struct'. I.e. If any fields inside the sub-struct change, the single 'change bit' for the entire struct will be set. Check the Serialize/Deserialize code-generated methods in Library\NetCodeGenerated_Backup for examples.

    Declaration
    public bool Composite { get; set; }
    Property Value
    Type Description
    bool

    MaxSmoothingDistance

    The maximum distance between two snapshots for which smoothing will be applied. If the value changes more than this between two received snapshots the smoothing action will not be performed.

    Declaration
    public float MaxSmoothingDistance { get; set; }
    Property Value
    Type Description
    float
    Remarks

    For quaternions the value specified should be sin(theta / 2) - where theta is the maximum angle you want to apply smoothing for.

    Quantization

    Floating point numbers will be multiplied by this number and rounded to an integer, enabling better delta-compression via huffman encoding. Quantization is not supported for integer numbers and is disabled by default for floats. To send a floating point number unquantized, use 0. Examples: Quantization=0 implies full precision. Quantization=1 implies precision of 1f (i.e. round float values to integers). Quantization=2 implies precision of 0.5f. Quantization=10 implies precision of 0.1f. Quantization=20 implies precision of 0.05f. Quantization=1000 implies precision of 0.001f.

    Declaration
    public int Quantization { get; set; }
    Property Value
    Type Description
    int

    SendData

    Default true. If unset (false), instructs code-generation to not include this field in the serialization data. I.e. Do not replicate this field. This is particularly useful for non primitive members (like structs), which will have all fields serialized by default.

    Declaration
    public bool SendData { get; set; }
    Property Value
    Type Description
    bool

    Smoothing

    Default is Clamp.

    Declaration
    public SmoothingAction Smoothing { get; set; }
    Property Value
    Type Description
    SmoothingAction

    SubType

    Allows you to specify a custom serializer for this GhostField using the GhostFieldSubType API.

    Declaration
    public int SubType { get; set; }
    Property Value
    Type Description
    int
    Remarks

    Why GhostFieldSubType is not an enum: The reason is that there are unfortunately caveats, some due to our compilation pipeline and others due to the limitation of the SourceGenerator api. First: MS SourceGenerator are additive only. That means we cannot modify the syntaxtree, removing or adding nodes to it (not the way Analyzers does). To overcome that limitation, a possible solution to inject the enums literals into the assembly is to use a small IL post processor instead. Because NetCode runtime assembly is re-imported every time a sub-type is added or removed, the assumption was that the IL post-processing will then correctly modify the dll before any dependent dll is compiled. Although it does, and Unity.NetCode.dll contains the correct metadata, the ILPostProcessorRunner run at a later time and some dlls are not compile correctly (depend on timing). With further investigation it might be possible to address that problem, however it seems like fighting against the compilation process again, something we wanted to avoid. Because all of that, a partial class to hold the integral constants it uses instead and users can add new const literals.

    Why the AssemblyDefinitionReference? Using source generator to add a partial class directly to NetCode.dll works fine but unfortunately will miss the IDE auto-completion functionality. No IDE at the moment provide support for that out of the box. VS has some workaround for normal C# projects (removing the original file from the solution etc) or by restarting the IDE, but Rider or VSCode does not work the same way. By using the Assembly Definition Reference, we are actually doing in principle the same job and completion works, making the user experience a little more pleasant.

    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)