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 IJobForEach<T0> 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
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.