Struct ComponentDataFromEntity<T>
A [NativeContainer] that provides access to all instances of components of type T, indexed by Entity.
Namespace: Unity.Entities
Syntax
[NativeContainer]
public struct ComponentDataFromEntity<T>
where T : struct, IComponentData
Type Parameters
Name | Description |
---|---|
T | The type of IComponentData to access. |
Remarks
ComponentDataFromEntity is a native container that provides array-like access to components of a specific type. You can use ComponentDataFromEntity to look up data associated with one entity while iterating over a different set of entities. For example, Unity.Transforms stores the Entity object of parent entities in a Parent component and looks up the parent's LocalToWorld matrix using ComponentDataFromEntity<LocalToWorld> when calculating the world positions of child entities.
To get a ComponentDataFromEntity, call GetComponentDataFromEntity<T>(Boolean).
Pass a ComponentDataFromEntity container to a job by defining a public field of the appropriate type in your IJob implementation. You can safely read from ComponentDataFromEntity in any job, but by default, you cannot write to components in the container in parallel jobs (including Unity.Entities.IJobForEach`1 and IJobChunk). If you know that two instances of a parallel job can never write to the same index in the container, you can disable the restriction on parallel writing by adding NativeDisableParallelForRestrictionAttribute to the ComponentDataFromEntity field definition in the job struct.
If you would like to access an entity's components outside of a job, consider using the EntityManager methods GetComponentData<T>(Entity) and SetComponentData<T>(Entity, T) instead, to avoid the overhead of creating a ComponentDataFromEntity object.
Properties
Item[Entity]
Gets the IComponentData instance of type T for the specified entity.
Declaration
public T this[Entity entity] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
Entity | entity | The entity. |
Property Value
Type | Description |
---|---|
T | An IComponentData type. |
Remarks
You cannot use ComponentDataFromEntity to get zero-sized IComponentData. Use Exists(Entity) to check whether an entity has the zero-sized component instead.
Normally, you cannot write to components accessed using a ComponentDataFromEntity instance in a parallel Job. This restriction is in place because multiple threads could write to the same component, leading to a race condition and nondeterministic results. However, when you are certain that your algorithm cannot write to the same component from different threads, you can manually disable this safety check by putting the [NativeDisableParallelForRestrictions] attribute on the ComponentDataFromEntity field in the Job.
Exceptions
Type | Condition |
---|---|
ArgumentException | Thrown if T is zero-size. |
Methods
DidChange(Entity, UInt32)
Reports whether any of IComponentData components of the type T, in the chunk containing the specified Entity, could have changed.
Declaration
public bool DidChange(Entity entity, uint version)
Parameters
Type | Name | Description |
---|---|---|
Entity | entity | The entity. |
UInt32 | version | The version to compare. In a system, this parameter should be set to the current LastSystemVersion at the time the job is run or scheduled. |
Returns
Type | Description |
---|---|
Boolean | True, if the version number stored in the chunk for this component is more recent than the version
passed to the |
Remarks
Note that for efficiency, the change version applies to whole chunks not individual entities. The change version is incremented even when another job or system that has declared write access to a component does not actually change the component value.
Exists(Entity)
Reports whether the specified Entity instance still refers to a valid entity and that it has a component of type T.
Declaration
public bool Exists(Entity entity)
Parameters
Type | Name | Description |
---|---|---|
Entity | entity | The entity. |
Returns
Type | Description |
---|---|
Boolean | True if the entity has a component of type T, and false if it does not. Also returns false if the Entity instance refers to an entity that has been destroyed. |
Remarks
To report if the provided entity has a component of type T, this function confirms whether the EntityArchetype of the provided entity includes components of type T.
HasComponent(Entity)
Reports whether the specified Entity instance still refers to a valid entity and that it has a component of type T.
Declaration
public bool HasComponent(Entity entity)
Parameters
Type | Name | Description |
---|---|---|
Entity | entity | The entity. |
Returns
Type | Description |
---|---|
Boolean | True if the entity has a component of type T, and false if it does not. Also returns false if the Entity instance refers to an entity that has been destroyed. |
Remarks
To report if the provided entity has a component of type T, this function confirms whether the EntityArchetype of the provided entity includes components of type T.