docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    EntityQuery filters

    To further sort entities, you can use a filter to exclude entities based on the following:

    • Shared component filter: Filter the set of entities based on specific values of a shared component.
    • Change filter: Filter the set of Entities based on whether the value of a specific component type has changed.
    • Enableable components

    The filters you set remain in effect until you call ResetFilter on the query object.

    To ignore the query's active chunk filters, use the EntityQuery methods that have names ending in IgnoreFilter. These methods are generally more efficient than the filtering equivalents. For example, see IsEmpty vs. IsEmptyIgnoreFilter.

    Use a shared component filter

    To use a shared component filter, include the shared component in the EntityQuery along with any other needed components and call the SetSharedComponentFilter method. Then pass in a struct of the same ISharedComponent type that contains the values to select. All values must match. You can add up to two different shared components to the filter.

    You can change the filter at any time, but if you change the filter, it doesn't change any existing arrays of entities or components that you received from the group ToComponentDataArray<T> or ToEntityArray methods. You must recreate these arrays.

    The following example defines a shared component named SharedGrouping and a system that only processes entities that have the group field set to 1.

    
    struct SharedGrouping : ISharedComponentData
    {
        public int Group;
    }
    
    [RequireMatchingQueriesForUpdate]
    partial class ImpulseSystem : SystemBase
    {
        EntityQuery query;
    
        protected override void OnCreate()
        {
            query = new EntityQueryBuilder(Allocator.Temp)
                .WithAllRW<ObjectPosition>()
                .WithAll<Displacement, SharedGrouping>()
                .Build(this);
        }
    
        protected override void OnUpdate()
        {
            // By default (without a filter), count all entities that have the required components.
            query.ResetFilter();
            int unfilteredCount = query.CalculateEntityCount();
            // With a filter, only entities in chunks that have SharedGrouping=1 will be counted.
            query.SetSharedComponentFilter(new SharedGrouping { Group = 1 });
            int filteredCount = query.CalculateEntityCount();
            // Many query methods include a variant that ignores any active filters. These variants are generally
            // more efficient, and should be used when conservative upper-bound results are acceptable.
            int ignoreFilterCount = query.CalculateEntityCountWithoutFiltering();
        }
    }
    
    

    Use a change filter

    If you only need to update entities when a component value has changed, use the SetChangedVersionFilter method to add that component to the EntityQuery filter. For example, the following EntityQuery only includes entities from chunks that another system has already written to the Translation component:

    
    EntityQuery query;
    
    protected override void OnCreate()
    {
        query = new EntityQueryBuilder(Allocator.Temp)
            .WithAllRW<LocalToWorld>()
            .WithAll<ObjectPosition>()
            .Build(this);
        query.SetChangedVersionFilter(typeof(ObjectPosition));
    
    }
    

    For efficiency, the change filter applies to whole archetype chunks, and not individual entities. The change filter also only checks whether a system that declared write access to the component has run, and not whether it changed any data. For example, if another job that can write to that component type accesses the chunk, then the change filter includes all entities in that chunk. This is why you should always declare read-only access to components that you don't need to modify.

    Filtering by enableable components

    Enableable components allow components on individual entities to be enabled and disabled at runtime. Disabling components on an entity doesn't move that entity into a new archetype, but for the purposes of EntityQuery matching, the entity is treated as if it doesn't have the component. Specifically:

    • If an entity has component T disabled, it will not match queries that require component T (using WithAll<T>()).
    • If an entity has component T disabled, it will match queries that exclude component T (using WithNone<T>()).

    Most EntityQuery operations, such as ToEntityArray and CalculateEntityCount, automatically filter out entities whose enableable components would cause them not to match the query. To disable this filtering, use the IgnoreFilter variants of these operations, or pass the EntityQueryOptions.IgnoreComponentEnabledState at query creation time.

    See the enableable components documentation for more details.

    Further resources

    • Create an EntityQuery
    • Shared components
    • Change filter
    • Enableable components

    Did you find this page useful? Please give it a rating:

    Thanks for rating this page!

    Report a problem on this page

    What kind of problem would you like to report?

    • This page needs code samples
    • Code samples do not work
    • Information is missing
    • Information is incorrect
    • Information is unclear or confusing
    • There is a spelling/grammar error on this page
    • Something else

    Thanks for letting us know! This page has been marked for review based on your feedback.

    If you have time, you can provide more information to help us fix the problem faster.

    Provide more information

    You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:

    You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:

    You've told us there is information missing from this page. Please tell us more about what's missing:

    You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:

    You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:

    You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:

    You've told us this page has a problem. Please tell us more about what's wrong:

    Thank you for helping to make the Unity documentation better!

    Your feedback has been submitted as a ticket for our documentation team to review.

    We are not able to reply to every ticket submitted.

    In This Article
    • Use a shared component filter
    • Use a change filter
    • Filtering by enableable components
    • Further resources
    Back to top
    Copyright © 2024 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)