// ---------------------------------------------------------------------------------------------------------- // PUBLIC // ---------------------------------------------------------------------------------------------------------- /// <summary> /// Moves all entities managed by the specified EntityManager to the <see cref="World"/> of this EntityManager and fills /// an array with their <see cref="Entity"/> objects. /// </summary> /// <remarks> /// After the move, the entities are managed by this EntityManager. Use the `output` array to make post-move /// changes to the transferred entities. /// /// Each world has one EntityManager, which manages all the entities in that world. This function /// allows you to transfer entities from one World to another. /// /// **Important:** This function creates a sync point, which means that the EntityManager waits for all /// currently running Jobs to complete before moving the entities 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="output">An array to receive the Entity objects of the transferred entities.</param> /// <param name="srcEntities">The EntityManager whose entities are appropriated.</param> /// <param name="entityRemapping">A set of entity transformations to make during the transfer.</param> /// <exception cref="ArgumentException"></exception> public void MoveEntitiesFrom(out NativeArray <Entity> output, EntityManager srcEntities, NativeArray <EntityRemapUtility.EntityRemapInfo> entityRemapping) { #if ENABLE_UNITY_COLLECTIONS_CHECKS if (srcEntities == this) { throw new ArgumentException("srcEntities must not be the same as this EntityManager."); } if (!srcEntities.m_ManagedComponentStore.AllSharedComponentReferencesAreFromChunks(srcEntities .EntityComponentStore)) { throw new ArgumentException( "EntityManager.MoveEntitiesFrom failed - All ISharedComponentData references must be from EntityManager. (For example EntityQuery.SetFilter with a shared component type is not allowed during EntityManager.MoveEntitiesFrom)"); } #endif BeforeStructuralChange(); srcEntities.BeforeStructuralChange(); var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking(); EntityManagerMoveEntitiesUtility.MoveChunks(entityRemapping, srcEntities.EntityComponentStore, srcEntities.ManagedComponentStore, EntityComponentStore, ManagedComponentStore); EntityRemapUtility.GetTargets(out output, entityRemapping); var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges); EntityGroupManager.AddAdditionalArchetypes(changedArchetypes); //@TODO: Need to increment the component versions based the moved chunks... }
internal void RemoveComponent(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->AssertCanRemoveComponent(chunks, componentType); EntityManagerChangeArchetypeUtility.RemoveComponent(chunks, componentType, EntityComponentStore, ManagedComponentStore); var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges); EntityGroupManager.AddAdditionalArchetypes(changedArchetypes); } }
// ---------------------------------------------------------------------------------------------------------- // INTERNAL // ---------------------------------------------------------------------------------------------------------- void MoveEntitiesFrom(out NativeArray <Entity> output, EntityManager srcEntities, NativeArray <ArchetypeChunk> chunks, NativeArray <EntityRemapUtility.EntityRemapInfo> entityRemapping) { #if ENABLE_UNITY_COLLECTIONS_CHECKS if (srcEntities == this) { throw new ArgumentException("srcEntities must not be the same as this EntityManager."); } for (int i = 0; i < chunks.Length; ++i) { if (chunks[i].m_Chunk->Archetype->HasChunkHeader) { throw new ArgumentException( "MoveEntitiesFrom can not move chunks that contain ChunkHeader components."); } } #endif BeforeStructuralChange(); srcEntities.BeforeStructuralChange(); var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking(); EntityManagerMoveEntitiesUtility.MoveChunks(chunks, entityRemapping, srcEntities.EntityComponentStore, srcEntities.ManagedComponentStore, EntityComponentStore, ManagedComponentStore); var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges); EntityGroupManager.AddAdditionalArchetypes(changedArchetypes); EntityRemapUtility.GetTargets(out output, entityRemapping); }
/// <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); } }
/// <summary> /// Removes a component from an entity. /// </summary> /// <remarks> /// Removing a component changes an entity's archetype and results in the entity being moved to a different /// chunk. /// /// **Important:** This function creates a sync point, which means that the EntityManager waits for all /// currently running Jobs to complete before removing 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="entity">The entity to modify.</param> /// <param name="type">The type of component to remove.</param> public void RemoveComponent(Entity entity, ComponentType type) { BeforeStructuralChange(); var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking(); EntityManagerChangeArchetypeUtility.RemoveComponent(entity, type, EntityComponentStore, ManagedComponentStore); var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges); EntityGroupManager.AddAdditionalArchetypes(changedArchetypes); }
public EntityArchetype GetEntityOnlyArchetype() { if (!m_EntityOnlyArchetype.Valid) { var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking(); ComponentTypeInArchetype entityType = new ComponentTypeInArchetype(ComponentType.ReadWrite <Entity>()); var archetype = EntityManagerCreateArchetypeUtility.GetOrCreateArchetype(&entityType, 1, EntityComponentStore); m_EntityOnlyArchetype = new EntityArchetype { Archetype = archetype }; var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges); EntityGroupManager.AddAdditionalArchetypes(changedArchetypes); } return(m_EntityOnlyArchetype); }
// ---------------------------------------------------------------------------------------------------------- // INTERNAL // ---------------------------------------------------------------------------------------------------------- internal void AddComponent(Entity entity, ComponentType componentType, bool ignoreDuplicateAdd) { if (ignoreDuplicateAdd && HasComponent(entity, componentType)) { return; } BeforeStructuralChange(); var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking(); EntityComponentStore->AssertCanAddComponent(entity, componentType); EntityManagerChangeArchetypeUtility.AddComponent(entity, componentType, EntityComponentStore, ManagedComponentStore); var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges); EntityGroupManager.AddAdditionalArchetypes(changedArchetypes); }
/// <summary> /// Removes a component from the chunks identified by a EntityQuery. /// </summary> /// <remarks> /// 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 removing 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> /// <typeparam name="T">The type of component to remove.</typeparam> public void RemoveChunkComponentData <T>(EntityQuery entityQuery) { using (var chunks = entityQuery.CreateArchetypeChunkArray(Allocator.TempJob)) { if (chunks.Length == 0) { return; } BeforeStructuralChange(); var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking(); EntityManagerChangeArchetypeUtility.RemoveComponent(chunks, ComponentType.ChunkComponent <T>(), EntityComponentStore, ManagedComponentStore); var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges); EntityGroupManager.AddAdditionalArchetypes(changedArchetypes); } }
/// <summary> /// Adds a shared component to a set of entities defined by a EntityQuery. /// </summary> /// <remarks> /// The fields of the `componentData` parameter are assigned to all of the added shared components. /// /// Adding a component to an entity changes its archetype and results in the entity being moved to a /// different chunk. The entity moves to a chunk with other entities that have the same shared component values. /// A new chunk is created if no chunk with the same archetype and shared component values currently exists. /// /// **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 defining a set of entities to modify.</param> /// <param name="componentData">The data to set.</param> /// <typeparam name="T">The data type of the shared component.</typeparam> public void AddSharedComponentData <T>(EntityQuery entityQuery, T componentData) where T : struct, ISharedComponentData { var componentType = ComponentType.ReadWrite <T>(); using (var chunks = entityQuery.CreateArchetypeChunkArray(Allocator.TempJob)) { if (chunks.Length == 0) { return; } BeforeStructuralChange(); var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking(); var newSharedComponentDataIndex = m_ManagedComponentStore.InsertSharedComponent(componentData); EntityComponentStore->AssertCanAddComponent(chunks, componentType); EntityManagerChangeArchetypeUtility.AddSharedComponent(chunks, componentType, newSharedComponentDataIndex, EntityComponentStore, ManagedComponentStore); m_ManagedComponentStore.RemoveReference(newSharedComponentDataIndex); var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges); EntityGroupManager.AddAdditionalArchetypes(changedArchetypes); } }