Interface IJobChunk | Entities | 0.13.0-preview.24
docs.unity3d.com
    Show / Hide Table of Contents

    Interface IJobChunk

    IJobChunk is a type of Job that iterates over a set of ArchetypeChunk instances.

    Namespace: Unity.Entities
    Syntax
    [JobProducerType(typeof(JobChunkExtensions.JobChunkProducer<>))]
    public interface IJobChunk
    Remarks

    Create and schedule an IJobChunk Job inside the OnUpdate() function of a SystemBase implementation. The Job component system calls the Execute function once for each ArchetypeChunk found by the EntityQuery used to schedule the Job.

    To pass data to the Execute function beyond the parameters of the Execute() function, add public fields to the IJobChunk struct declaration and set those fields immediately before scheduling the Job. You must pass the component type information for any components that the Job reads or writes using a field of type, ComponentTypeHandle<T>. Get this type information by calling the appropriate GetComponentTypeHandle<T>(Boolean) function for the type of component.

    For more information see Using IJobChunk.

        [GenerateAuthoringComponent]
    public struct Target : IComponentData
    {
       public Entity entity;
    }
    
    public class ChaserSystem : SystemBase
    {
       private EntityQuery query; // Initialized in Oncreate()
    
       [BurstCompile]
       private struct ChaserSystemJob : IJobChunk
       {
           // Read-write data in the current chunk
           public ComponentTypeHandle<Translation> PositionTypeHandle;
    
           // Read-only data in the current chunk
           [ReadOnly]
           public ComponentTypeHandle<Target> TargetTypeHandle;
    
           // Read-only data stored (potentially) in other chunks
           [ReadOnly]
           //[NativeDisableParallelForRestriction]
           public ComponentDataFromEntity<LocalToWorld> EntityPositions;
    
           // Non-entity data
           public float deltaTime;
    
           public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
           {
               NativeArray<Translation> positions = chunk.GetNativeArray<Translation>(PositionTypeHandle);
               NativeArray<Target> targets = chunk.GetNativeArray<Target>(TargetTypeHandle);
    
               for (int i = 0; i < positions.Length; i++)
               {
                   Entity targetEntity = targets[i].entity;
                   float3 targetPosition = EntityPositions[targetEntity].Position;
                   float3 chaserPosition = positions[i].Value;
    
                   float3 displacement = (targetPosition - chaserPosition);
                   positions[i] = new Translation { Value = chaserPosition + displacement * deltaTime };
               }
           }
       }
    
       protected override void OnCreate()
       {
           query = this.GetEntityQuery(typeof(Translation), ComponentType.ReadOnly<Target>());
       }
    
       protected override void OnUpdate()
       {
           var job = new ChaserSystemJob();
           job.PositionTypeHandle = this.GetComponentTypeHandle<Translation>(false);
           job.TargetTypeHandle = this.GetComponentTypeHandle<Target>(true);
    
           job.EntityPositions = this.GetComponentDataFromEntity<LocalToWorld>(true);
           job.deltaTime = this.Time.DeltaTime;
    
           this.Dependency = job.Schedule(query, this.Dependency);
       }
    }

    Methods

    Execute(ArchetypeChunk, Int32, Int32)

    Implement the Execute() function to perform a unit of work on an ArchetypeChunk.

    Declaration
    void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
    Parameters
    Type Name Description
    ArchetypeChunk chunk

    The current chunk.

    Int32 chunkIndex

    The index of the current chunk within the list of all chunks found by the Job's EntityQuery. Note that chunks are not processed in index order, except by chance.

    Int32 firstEntityIndex

    The index of the first entity in the current chunk within the list of all entities in all the chunks found by the Job's EntityQuery.

    Remarks

    The Job component system calls the Execute function once for each EntityArchetype found by the EntityQuery used to schedule the Job.

    In This Article
    • Methods
      • Execute(ArchetypeChunk, Int32, Int32)
    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