docs.unity.cn
    Show / Hide Table of Contents

    Method GetComponent

    GetComponent<T>(Entity)

    Look up the value of a component for an entity.

    Declaration
    protected T GetComponent<T>(Entity entity)
        where T : struct, IComponentData
    Parameters
    Type Name Description
    Entity entity

    The entity.

    Returns
    Type Description
    T

    A struct of type T containing the component value.

    Type Parameters
    Name Description
    T

    The type of component to retrieve.

    Remarks

    Use this method to look up data in another entity using its Entity object. For example, if you have a component that contains an Entity field, you can look up the component data for the referenced entity using this method.

    When iterating over a set of entities via Entities.ForEach, do not use this method to access data of the current entity in the set. This function is much slower than accessing the data directly (by passing the component containing the data to your lambda iteration function as a parameter).

    When you call this method on the main thread, it invokes GetComponentData<T>(Entity). (An Entities.ForEach function invoked with Run() executes on the main thread.) When you call this method inside a job scheduled using Entities.ForEach, this method gets replaced with component access methods through ComponentDataFromEntity<T>.

    In both cases, this lookup method results in a slower, indirect memory access. When possible, organize your data to minimize the need for indirect lookups.

    Exceptions
    Type Condition
    ArgumentException

    Thrown if the component type has no fields.

    Back to top Copyright © 2022 Unity Technologies
    Generated by DocFX
    on Wednesday, July 6, 2022
    Terms of use