internal void AddComponent(MatchingArchetypeList archetypeList, EntityQueryFilter filter, ComponentType componentType) { var jobHandle = new JobHandle(); using (var chunks = ComponentChunkIterator.CreateArchetypeChunkArray(archetypeList, Allocator.TempJob, out jobHandle, ref filter)) { jobHandle.Complete(); if (chunks.Length == 0) { return; } BeforeStructuralChange(); var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking(); EntityComponentStore->AssertCanAddComponent(chunks, componentType); using (var entityBatchList = m_EntityComponentStore->CreateEntityBatchList(chunks)) { EntityManagerChangeArchetypeUtility.AddComponentFromMainThread(entityBatchList, componentType, 0, EntityComponentStore, ManagedComponentStore); } var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges); EntityGroupManager.AddAdditionalArchetypes(changedArchetypes); } }
/// <summary> /// Adds a component to each of the chunks identified by a EntityQuery and set the component values. /// </summary> /// <remarks> /// This function finds all chunks whose archetype satisfies the EntityQuery and adds the specified /// component to them. /// /// A chunk component is common to all entities in a chunk. You can access a chunk <see cref="IComponentData"/> /// instance through either the chunk itself or through an entity stored in that chunk. /// /// **Important:** This function creates a sync point, which means that the EntityManager waits for all /// currently running Jobs to complete before adding the component and no additional Jobs can start before /// the function is finished. A sync point can cause a drop in performance because the ECS framework may not /// be able to make use of the processing power of all available cores. /// </remarks> /// <param name="entityQuery">The EntityQuery identifying the chunks to modify.</param> /// <param name="componentData">The data to set.</param> /// <typeparam name="T">The type of component, which must implement IComponentData.</typeparam> public void AddChunkComponentData <T>(EntityQuery entityQuery, T componentData) where T : struct, IComponentData { using (var chunks = entityQuery.CreateArchetypeChunkArray(Allocator.TempJob)) { if (chunks.Length == 0) { return; } BeforeStructuralChange(); var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking(); EntityComponentStore->AssertCanAddChunkComponent(chunks, ComponentType.ChunkComponent <T>()); var type = ComponentType.ReadWrite <T>(); var chunkType = ComponentType.FromTypeIndex(TypeManager.MakeChunkComponentTypeIndex(type.TypeIndex)); using (var entityBatchList = m_EntityComponentStore->CreateEntityBatchList(chunks)) { EntityManagerChangeArchetypeUtility.AddComponentFromMainThread(entityBatchList, chunkType, 0, EntityComponentStore, ManagedComponentStore); m_EntityComponentStore->SetChunkComponent <T>(entityBatchList, componentData); } var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges); EntityGroupManager.AddAdditionalArchetypes(changedArchetypes); } }