docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Enableable components overview

    You can use enableable components on IComponentData and IBufferElementData components to disable or enable individual components on an entity at runtime. To make components enableable, inherit them from IEnableableComponent.

    Enableable components are ideal for states that you expect to change often and unpredictably, or where the number of state permutations are high on a frame-by-frame basis. Adding and removing components is the preferable way to manage components for low-frequency state changes, where you expect the state to persist for many frames.

    You can also use enableable components instead of a set of zero-size tag components to represent entity states. This reduces the number of unique entity archetypes, and encourages better chunk usage to reduce memory consumption.

    Structural changes

    Enableable components don't create structural changes, unlike adding and removing components. ECS treats a disabled component as though the entity doesn't have that component when determining if an entity matches an entity query. This means that an entity with a disabled component doesn't match a query that requires the component, and matches a query that excludes the component, assuming it meets all other query criteria.

    The semantics of existing component operations don't change. EntityManager considers an entity with a disabled component to still have the component.

    For example, if component T is disabled on entity E these methods do the following:

    Method Outcome
    HasComponent<T>(E) Returns true.
    GetComponent<T>(E) Returns component T’s current value.
    SetComponent<T>(E,value) Updates the component T’s value.
    RemoveComponent<T>(E) Removes component T from E.
    AddComponent<T>(E) Quietly does nothing, because the component already exists.

    Additional resources

    • Use enableable components
    • Look up arbitrary data
    In This Article
    • Structural changes
    • Additional 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)