/// <summary>
        /// Adds a set of component to an entity.
        /// </summary>
        /// <remarks>
        /// Adding components changes the entity's archetype and results in the entity being moved to a different
        /// chunk.
        ///
        /// The added components have the default values for the type.
        ///
        /// If the <see cref="Entity"/> object refers to an entity that has been destroyed, this function throws an ArgumentError
        /// exception.
        ///
        /// **Important:** This function creates a sync point, which means that the EntityManager waits for all
        /// currently running Jobs to complete before adding these components 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="types">The types of components to add.</param>
        public void AddComponents(Entity entity, ComponentTypes types)
        {
            BeforeStructuralChange();
            var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking();

            EntityComponentStore->AssertCanAddComponents(entity, types);
            EntityComponentStore->AddComponents(entity, types);

            var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges);

            EntityQueryManager.AddAdditionalArchetypes(changedArchetypes);
            ManagedComponentStore.Playback(ref EntityComponentStore->ManagedChangesTracker);
        }
Example #2
0
        public EntityArchetype GetEntityOnlyArchetype()
        {
            if (!m_EntityOnlyArchetype.Valid)
            {
                var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking();
                ComponentTypeInArchetype entityType = new ComponentTypeInArchetype(ComponentType.ReadWrite <Entity>());
                var archetype = EntityComponentStore->GetOrCreateArchetype(&entityType, 1);
                m_EntityOnlyArchetype = new EntityArchetype {
                    Archetype = archetype
                };
                var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges);
                EntityQueryManager.AddAdditionalArchetypes(changedArchetypes);
            }

            return(m_EntityOnlyArchetype);
        }
        public void SetArchetype(Entity entity, EntityArchetype archetype)
        {
            BeforeStructuralChange();

            EntityComponentStore->AssertEntitiesExist(&entity, 1);
            var oldArchetype = EntityComponentStore->GetArchetype(entity);
            var newArchetype = archetype.Archetype;

            Unity.Entities.EntityComponentStore.AssertArchetypeDoesNotRemoveSystemStateComponents(oldArchetype, newArchetype);

            var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking();

            EntityComponentStore->SetArchetype(entity, archetype);
            var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges);

            EntityQueryManager.AddAdditionalArchetypes(changedArchetypes);
            ManagedComponentStore.Playback(ref EntityComponentStore->ManagedChangesTracker);
        }
        // ----------------------------------------------------------------------------------------------------------
        // 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);
            EntityComponentStore->AddComponent(entity, componentType);

            var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges);

            EntityQueryManager.AddAdditionalArchetypes(changedArchetypes);
            ManagedComponentStore.Playback(ref EntityComponentStore->ManagedChangesTracker);
        }
        internal void AddSharedComponentData(NativeArray <ArchetypeChunk> chunks, int sharedComponentIndex, ComponentType componentType)
        {
            if (chunks.Length == 0)
            {
                return;
            }

            BeforeStructuralChange();
            var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking();

            EntityComponentStore->AssertCanAddComponent(chunks, componentType);
            EntityComponentStore->AddSharedComponent(chunks, componentType, sharedComponentIndex);

            var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges);

            EntityQueryManager.AddAdditionalArchetypes(changedArchetypes);
            ManagedComponentStore.Playback(ref EntityComponentStore->ManagedChangesTracker);
            ManagedComponentStore.RemoveReference(sharedComponentIndex);
        }
        /// <summary>
        /// Remove a component from a set of entities.
        /// </summary>
        /// <remarks>
        /// Removing a component changes an entity's archetype and results in the entity being moved to a different
        /// chunk.
        ///
        /// If an <see cref="Entity"/> object in the `entities` array refers to an entity that has been destroyed, this function
        /// throws an ArgumentError exception.
        ///
        /// **Important:** This function creates a sync point, which means that the EntityManager waits for all
        /// currently running Jobs to complete before creating these chunks 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="entities">An array of Entity objects.</param>
        /// <param name="componentType">The type of component to remove.</param>
        public void RemoveComponent(NativeArray <Entity> entities, ComponentType componentType)
        {
            if (entities.Length == 0)
            {
                return;
            }

            BeforeStructuralChange();
            var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking();

            using (var entityBatchList = m_EntityComponentStore->CreateEntityBatchList(entities))
            {
                EntityComponentStore->RemoveComponentFromMainThread(entityBatchList, componentType, 0);
            }

            var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges);

            EntityQueryManager.AddAdditionalArchetypes(changedArchetypes);
            ManagedComponentStore.Playback(ref EntityComponentStore->ManagedChangesTracker);
        }
        // ----------------------------------------------------------------------------------------------------------
        // INTERNAL
        // ----------------------------------------------------------------------------------------------------------

        void MoveEntitiesFromInternalQuery(EntityManager srcEntities, EntityQuery filter, NativeArray <EntityRemapUtility.EntityRemapInfo> entityRemapping)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            if (filter._EntityComponentStore != srcEntities.EntityComponentStore)
            {
                throw new ArgumentException(
                          "EntityManager.MoveEntitiesFrom failed - srcEntities and filter must belong to the same World)");
            }

            if (srcEntities == this)
            {
                throw new ArgumentException("srcEntities must not be the same as this EntityManager.");
            }
#endif
            BeforeStructuralChange();
            srcEntities.BeforeStructuralChange();

            using (var chunks = filter.CreateArchetypeChunkArray(Allocator.TempJob))
            {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                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

                var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking();

                MoveChunksFromFiltered(chunks, entityRemapping, srcEntities.EntityComponentStore, srcEntities.ManagedComponentStore);

                var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges);
                EntityQueryManager.AddAdditionalArchetypes(changedArchetypes);
            }
        }