Class EntityQuery | Entities | 0.7.0-preview.19
docs.unity3d.com
    Show / Hide Table of Contents

    Class EntityQuery

    Use an EntityQuery object to select entities with components that meet specific requirements.

    Inheritance
    Object
    EntityQuery
    Namespace: Unity.Entities
    Syntax
    public class EntityQuery : IDisposable
    Remarks

    An entity query defines the set of component types that an archetype must contain in order for its chunks and entities to be selected and specifies whether the components accessed through the query are read-only or read-write.

    For simple queries, you can create an EntityQuery based on an array of component types. The following example defines a EntityQuery that finds all entities with both Rotation and RotationSpeed components.

    The query uses ComponentType.ReadOnly instead of the simpler typeof expression to designate that the system does not write to RotationSpeed. Always specify read-only when possible, since there are fewer constraints on read-only access to data, which can help the Job scheduler execute your Jobs more efficiently.

    For more complex queries, you can use an EntityQueryDesc object to create the entity query. A query description provides a flexible query mechanism to specify which archetypes to select based on the following sets of components:

    • All = All component types in this array must exist in the archetype
    • Any = At least one of the component types in this array must exist in the archetype
    • None = None of the component types in this array can exist in the archetype

    For example, the following query includes archetypes containing Rotation and RotationSpeed components, but excludes any archetypes containing a Frozen component:

    Note: Do not include completely optional components in the query description. To handle optional components, use IJobChunk and the ArchetypeChunk.Has() method to determine whether a chunk contains the optional component or not. Since all entities within the same chunk have the same components, you only need to check whether an optional component exists once per chunk -- not once per entity.

    Within a system class, use the ComponentSystemBase.GetEntityQuery() function to get a EntityQuery instance. Outside a system, use the EntityManager.CreateEntityQuery() function.

    You can filter entities based on whether they have changed or whether they have a specific value for a shared component. Once you have created an EntityQuery object, you can reset and change the filter settings, but you cannot modify the base query.

    Use an EntityQuery for the following purposes:

    • To get a native array of a the values for a specific IComponentData type for all entities matching the query
    • To get an native array of the ArchetypeChunk objects matching the query
    • To schedule an IJobChunk job
    • To control whether a system updates using [ComponentSystemBase.RequireForUpdate(query)]

    Note that Entities.ForEach defines an entity query implicitly based on the methods you call. You can access this implicit EntityQuery object using Entities.WithStoreEntityQueryInField. However, you cannot create an Entities.ForEach construction based on an existing EntityQuery object.

    Properties

    IsCreated

    Declaration
    public bool IsCreated { get; }
    Property Value
    Type Description
    Boolean

    IsEmptyIgnoreFilter

    Reports whether this query would currently select zero entities.

    Declaration
    public bool IsEmptyIgnoreFilter { get; }
    Property Value
    Type Description
    Boolean

    True, if this EntityQuery matches zero existing entities. False, if it matches one or more entities.

    Methods

    AddChangedVersionFilter(ComponentType)

    Declaration
    public void AddChangedVersionFilter(ComponentType componentType)
    Parameters
    Type Name Description
    ComponentType componentType

    AddDependency(JobHandle)

    Adds another job handle to this EntityQuery's dependencies.

    Declaration
    public JobHandle AddDependency(JobHandle job)
    Parameters
    Type Name Description
    JobHandle job
    Returns
    Type Description
    JobHandle
    Remarks

    An entity query uses jobs internally when required to create arrays of entities and chunks. This junction adds an external job as a dependency for those internal jobs.

    AddSharedComponentFilter<SharedComponent>(SharedComponent)

    Declaration
    public void AddSharedComponentFilter<SharedComponent>(SharedComponent sharedComponent)
        where SharedComponent : struct, ISharedComponentData
    Parameters
    Type Name Description
    SharedComponent sharedComponent
    Type Parameters
    Name Description
    SharedComponent

    CalculateChunkCount()

    Calculates the number of chunks that match this EntityQuery.

    Declaration
    public int CalculateChunkCount()
    Returns
    Type Description
    Int32

    The number of chunks based on the current EntityQuery properties.

    Remarks

    The EntityQuery must run the query and apply any filters to calculate the chunk count.

    CalculateChunkCountWithoutFiltering()

    Calculates the number of chunks that match this EntityQuery, ignoring any set filters.

    Declaration
    public int CalculateChunkCountWithoutFiltering()
    Returns
    Type Description
    Int32

    The number of chunks based on the current EntityQuery properties.

    Remarks

    The EntityQuery must run the query to calculate the chunk count.

    CalculateEntityCount()

    Calculates the number of entities selected by this EntityQuery.

    Declaration
    public int CalculateEntityCount()
    Returns
    Type Description
    Int32

    The number of entities based on the current EntityQuery properties.

    Remarks

    The EntityQuery must run the query and apply any filters to calculate the entity count.

    CalculateEntityCountWithoutFiltering()

    Calculates the number of entities selected by this EntityQuery, ignoring any set filters.

    Declaration
    public int CalculateEntityCountWithoutFiltering()
    Returns
    Type Description
    Int32

    The number of entities based on the current EntityQuery properties.

    Remarks

    The EntityQuery must run the query to calculate the entity count.

    CompareComponents(NativeArray<ComponentType>)

    Compares a list of component types to the types defining this EntityQuery.

    Declaration
    public bool CompareComponents(NativeArray<ComponentType> componentTypes)
    Parameters
    Type Name Description
    NativeArray<ComponentType> componentTypes

    An array of ComponentType objects.

    Returns
    Type Description
    Boolean

    True, if the list of types, including any read/write access specifiers, matches the list of required component types of this EntityQuery.

    Remarks

    Only required types in the query are used as the basis for the comparison. If you include types that the query excludes or only includes as optional, the comparison returns false. Do not include the Entity type, which is included implicitly.

    CompareComponents(ComponentType[])

    Compares a list of component types to the types defining this EntityQuery.

    Declaration
    public bool CompareComponents(ComponentType[] componentTypes)
    Parameters
    Type Name Description
    ComponentType[] componentTypes

    An array of ComponentType objects.

    Returns
    Type Description
    Boolean

    True, if the list of types, including any read/write access specifiers, matches the list of required component types of this EntityQuery.

    Remarks

    Only required types in the query are used as the basis for the comparison. If you include types that the query excludes or only includes as optional, the comparison returns false.

    CompareQuery(EntityQueryDesc[])

    Compares a query description to the description defining this EntityQuery.

    Declaration
    public bool CompareQuery(EntityQueryDesc[] queryDesc)
    Parameters
    Type Name Description
    EntityQueryDesc[] queryDesc

    The query description to compare.

    Returns
    Type Description
    Boolean

    True, if the query description contains the same components with the same read/write access modifiers as this EntityQuery.

    Remarks

    The All, Any, and None components in the query description are compared to the corresponding list in this EntityQuery.

    CompleteDependency()

    Ensures all jobs running on this EntityQuery complete.

    Declaration
    public void CompleteDependency()
    Remarks

    An entity query uses jobs internally when required to create arrays of entities and chunks. This function completes those jobs and returns when they are finished.

    CopyFromComponentDataArray<T>(NativeArray<T>)

    Declaration
    public void CopyFromComponentDataArray<T>(NativeArray<T> componentDataArray)
        where T : struct, IComponentData
    Parameters
    Type Name Description
    NativeArray<T> componentDataArray
    Type Parameters
    Name Description
    T

    CopyFromComponentDataArrayAsync<T>(NativeArray<T>, out JobHandle)

    Declaration
    public void CopyFromComponentDataArrayAsync<T>(NativeArray<T> componentDataArray, out JobHandle jobhandle)
        where T : struct, IComponentData
    Parameters
    Type Name Description
    NativeArray<T> componentDataArray
    JobHandle jobhandle
    Type Parameters
    Name Description
    T

    CreateArchetypeChunkArray(Allocator)

    Synchronously creates an array of the chunks containing entities matching this EntityQuery.

    Declaration
    public NativeArray<ArchetypeChunk> CreateArchetypeChunkArray(Allocator allocator)
    Parameters
    Type Name Description
    Allocator allocator

    Allocator to use for the array.

    Returns
    Type Description
    NativeArray<ArchetypeChunk>

    NativeArray of all the chunks in this ComponentChunkIterator.

    Remarks

    This method blocks until the internal job that performs the query completes.

    CreateArchetypeChunkArrayAsync(Allocator, out JobHandle)

    Asynchronously creates an array of the chunks containing entities matching this EntityQuery.

    Declaration
    public NativeArray<ArchetypeChunk> CreateArchetypeChunkArrayAsync(Allocator allocator, out JobHandle jobhandle)
    Parameters
    Type Name Description
    Allocator allocator

    Allocator to use for the array.

    JobHandle jobhandle

    An out parameter assigned the handle to the internal job that gathers the chunks matching this EntityQuery.

    Returns
    Type Description
    NativeArray<ArchetypeChunk>

    NativeArray of all the chunks containing entities matching this query.

    Remarks

    Use jobhandle as a dependency for jobs that use the returned chunk array. .

    Dispose()

    Disposes this EntityQuery instance.

    Declaration
    public void Dispose()
    Remarks

    Do not dispose the EntityQuery instances created using Entities. The system automatically disposes of its own entity queries.

    Exceptions
    Type Condition
    InvalidOperationException

    Thrown if you attempt to dispose an EntityQuery belonging to a SystemBase instance.

    GetArchetypeChunkIterator()

    Gets an ArchetypeChunkIterator which can be used to iterate over every chunk returned by this EntityQuery.

    Declaration
    public ArchetypeChunkIterator GetArchetypeChunkIterator()
    Returns
    Type Description
    ArchetypeChunkIterator

    ArchetypeChunkIterator for this EntityQuery

    GetCombinedComponentOrderVersion()

    Declaration
    public int GetCombinedComponentOrderVersion()
    Returns
    Type Description
    Int32

    GetDependency()

    Combines all dependencies in this EntityQuery into a single JobHandle.

    Declaration
    public JobHandle GetDependency()
    Returns
    Type Description
    JobHandle

    JobHandle that represents the combined dependencies of this EntityQuery

    Remarks

    An entity query uses jobs internally when required to create arrays of entities and chunks.

    GetSingleton<T>()

    Gets the value of a singleton component.

    Declaration
    public T GetSingleton<T>()
        where T : struct, IComponentData
    Returns
    Type Description
    T

    A copy of the singleton component.

    Type Parameters
    Name Description
    T

    The component type.

    Remarks

    A singleton component is a component of which only one instance exists in the world and which has been set with SetSingleton<T>(T).

    Exceptions
    Type Condition
    InvalidOperationException

    GetSingletonEntity()

    Declaration
    public Entity GetSingletonEntity()
    Returns
    Type Description
    Entity

    HasFilter()

    Returns if the entity query has a filter applied to it.

    Declaration
    public bool HasFilter()
    Returns
    Type Description
    Boolean

    Returns true if the query has a filter, returns false if the query does not have a filter.

    ResetFilter()

    Resets this EntityQuery's filter.

    Declaration
    public void ResetFilter()
    Remarks

    Removes references to shared component data, if applicable, then resets the filter type to None.

    SetChangedVersionFilter(ComponentType)

    Filters out entities in chunks for which the specified component has not changed.

    Declaration
    public void SetChangedVersionFilter(ComponentType componentType)
    Parameters
    Type Name Description
    ComponentType componentType

    ComponentType to mark as changed on this EntityQuery's filter.

    Remarks

    Saves a given ComponentType's index in RequiredComponents in this group's Changed filter.

    SetChangedVersionFilter(ComponentType[])

    Declaration
    public void SetChangedVersionFilter(ComponentType[] componentType)
    Parameters
    Type Name Description
    ComponentType[] componentType

    SetSharedComponentFilter<SharedComponent1>(SharedComponent1)

    Filters this EntityQuery so that it only selects entities with shared component values matching the values specified by the sharedComponent1 parameter.

    Declaration
    public void SetSharedComponentFilter<SharedComponent1>(SharedComponent1 sharedComponent1)
        where SharedComponent1 : struct, ISharedComponentData
    Parameters
    Type Name Description
    SharedComponent1 sharedComponent1

    The shared component values on which to filter.

    Type Parameters
    Name Description
    SharedComponent1

    The type of shared component. (The type must also be one of the types used to create the EntityQuery.

    SetSharedComponentFilter<SharedComponent1, SharedComponent2>(SharedComponent1, SharedComponent2)

    Filters this EntityQuery based on the values of two separate shared components.

    Declaration
    public void SetSharedComponentFilter<SharedComponent1, SharedComponent2>(SharedComponent1 sharedComponent1, SharedComponent2 sharedComponent2)
        where SharedComponent1 : struct, ISharedComponentData where SharedComponent2 : struct, ISharedComponentData
    Parameters
    Type Name Description
    SharedComponent1 sharedComponent1

    Shared component values on which to filter.

    SharedComponent2 sharedComponent2

    Shared component values on which to filter.

    Type Parameters
    Name Description
    SharedComponent1

    The type of shared component. (The type must also be one of the types used to create the EntityQuery.

    SharedComponent2

    The type of shared component. (The type must also be one of the types used to create the EntityQuery.

    Remarks

    The filter only selects entities for which both shared component values specified by the sharedComponent1 and sharedComponent2 parameters match.

    SetSingleton<T>(T)

    Sets the value of a singleton component.

    Declaration
    public void SetSingleton<T>(T value)
        where T : struct, IComponentData
    Parameters
    Type Name Description
    T value

    An instance of type T containing the values to set.

    Type Parameters
    Name Description
    T

    The component type.

    Remarks

    For a component to be a singleton, there can be only one instance of that component in a World. The component must be the only component in its archetype and you cannot use the same type of component as a normal component.

    To create a singleton, create an entity with the singleton component as its only component, and then use SetSingleton() to assign a value.

    For example, if you had a component defined as:

    public struct Singlet: IComponentData{ public int Value; }

    You could create a singleton as follows:

    var singletonEntity = entityManager.CreateEntity(typeof(Singlet));
    var singletonGroup = entityManager.CreateEntityQuery(typeof(Singlet));
    singletonGroup.SetSingleton<Singlet>(new Singlet {Value = 1});

    You can set and get the singleton value from a EntityQuery or a ComponentSystem.

    Exceptions
    Type Condition
    InvalidOperationException

    Thrown if more than one instance of this component type exists in the world or the component type appears in more than one archetype.

    ToComponentDataArray<T>()

    Declaration
    public T[] ToComponentDataArray<T>()
        where T : class, IComponentData
    Returns
    Type Description
    T[]
    Type Parameters
    Name Description
    T

    ToComponentDataArray<T>(Allocator)

    Creates a NativeArray containing the components of type T for the selected entities.

    Declaration
    public NativeArray<T> ToComponentDataArray<T>(Allocator allocator)
        where T : struct, IComponentData
    Parameters
    Type Name Description
    Allocator allocator

    The type of memory to allocate.

    Returns
    Type Description
    NativeArray<T>

    An array containing the specified component for all the entities selected by the EntityQuery.

    Type Parameters
    Name Description
    T

    The component type.

    Exceptions
    Type Condition
    InvalidOperationException

    Thrown if you ask for a component that is not part of the group.

    ToComponentDataArrayAsync<T>(Allocator, out JobHandle)

    Creates a NativeArray containing the components of type T for the selected entities.

    Declaration
    public NativeArray<T> ToComponentDataArrayAsync<T>(Allocator allocator, out JobHandle jobhandle)
        where T : struct, IComponentData
    Parameters
    Type Name Description
    Allocator allocator

    The type of memory to allocate.

    JobHandle jobhandle

    An out parameter assigned a handle that you can use as a dependency for a Job that uses the NativeArray.

    Returns
    Type Description
    NativeArray<T>

    An array containing the specified component for all the entities selected by the EntityQuery.

    Type Parameters
    Name Description
    T

    The component type.

    ToEntityArray(Allocator)

    Creates a NativeArray containing the selected entities.

    Declaration
    public NativeArray<Entity> ToEntityArray(Allocator allocator)
    Parameters
    Type Name Description
    Allocator allocator

    The type of memory to allocate.

    Returns
    Type Description
    NativeArray<Entity>

    An array containing all the entities selected by the EntityQuery.

    Remarks

    This version of the function blocks until the Job used to fill the array is complete.

    ToEntityArrayAsync(Allocator, out JobHandle)

    Creates a NativeArray containing the selected entities.

    Declaration
    public NativeArray<Entity> ToEntityArrayAsync(Allocator allocator, out JobHandle jobhandle)
    Parameters
    Type Name Description
    Allocator allocator

    The type of memory to allocate.

    JobHandle jobhandle

    An out parameter assigned a handle that you can use as a dependency for a Job that uses the NativeArray.

    Returns
    Type Description
    NativeArray<Entity>

    An array containing all the entities selected by the EntityQuery.

    Extension Methods

    EntityQueryManagedComponentExtensions.GetSingleton<T>(EntityQuery)
    EntityQueryManagedComponentExtensions.SetSingleton<T>(EntityQuery, T)
    EntityQueryExtensionsForComponentArray.ToComponentArray<T>(EntityQuery)
    EntityQueryExtensionsForTransformAccessArray.GetTransformAccessArray(EntityQuery)
    In This Article
    • Properties
      • IsCreated
      • IsEmptyIgnoreFilter
    • Methods
      • AddChangedVersionFilter(ComponentType)
      • AddDependency(JobHandle)
      • AddSharedComponentFilter<SharedComponent>(SharedComponent)
      • CalculateChunkCount()
      • CalculateChunkCountWithoutFiltering()
      • CalculateEntityCount()
      • CalculateEntityCountWithoutFiltering()
      • CompareComponents(NativeArray<ComponentType>)
      • CompareComponents(ComponentType[])
      • CompareQuery(EntityQueryDesc[])
      • CompleteDependency()
      • CopyFromComponentDataArray<T>(NativeArray<T>)
      • CopyFromComponentDataArrayAsync<T>(NativeArray<T>, out JobHandle)
      • CreateArchetypeChunkArray(Allocator)
      • CreateArchetypeChunkArrayAsync(Allocator, out JobHandle)
      • Dispose()
      • GetArchetypeChunkIterator()
      • GetCombinedComponentOrderVersion()
      • GetDependency()
      • GetSingleton<T>()
      • GetSingletonEntity()
      • HasFilter()
      • ResetFilter()
      • SetChangedVersionFilter(ComponentType)
      • SetChangedVersionFilter(ComponentType[])
      • SetSharedComponentFilter<SharedComponent1>(SharedComponent1)
      • SetSharedComponentFilter<SharedComponent1, SharedComponent2>(SharedComponent1, SharedComponent2)
      • SetSingleton<T>(T)
      • ToComponentDataArray<T>()
      • ToComponentDataArray<T>(Allocator)
      • ToComponentDataArrayAsync<T>(Allocator, out JobHandle)
      • ToEntityArray(Allocator)
      • ToEntityArrayAsync(Allocator, out JobHandle)
    • Extension Methods
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023