public static void CreateChunks(Archetype *archetype, ArchetypeChunk *chunks, int entityCount,
                                        EntityComponentStore *entityComponentStore,
                                        ManagedComponentStore managedComponentStore,
                                        EntityGroupManager entityGroupManager)
        {
            int *sharedComponentValues = stackalloc int[archetype->NumSharedComponents];

            UnsafeUtility.MemClear(sharedComponentValues, archetype->NumSharedComponents * sizeof(int));

            Chunk *lastChunk  = null;
            int    chunkIndex = 0;

            while (entityCount != 0)
            {
                var chunk = GetCleanChunk(archetype, sharedComponentValues,
                                          entityComponentStore, managedComponentStore, entityGroupManager);
                int allocatedIndex;

                var allocatedCount = AllocateIntoChunk(chunk, entityCount, out allocatedIndex,
                                                       entityComponentStore, managedComponentStore, entityGroupManager);

                entityComponentStore->AllocateEntities(archetype, chunk, allocatedIndex, allocatedCount, null);
                ChunkDataUtility.InitializeComponents(chunk, allocatedIndex, allocatedCount);
                chunk->SetAllChangeVersions(entityComponentStore->GlobalSystemVersion);
                chunks[chunkIndex] = new ArchetypeChunk(chunk, entityComponentStore);
                lastChunk          = chunk;

                entityCount -= allocatedCount;
                chunkIndex++;
            }

            entityComponentStore->IncrementComponentTypeOrderVersion(archetype);
        }
        // ----------------------------------------------------------------------------------------------------------
        // 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...
        }
Exemplo n.º 3
0
 public bool CompareComponents(ComponentType[] componentTypes)
 {
     fixed(ComponentType *componentTypesPtr = componentTypes)
     {
         return(EntityGroupManager.CompareComponents(componentTypesPtr, componentTypes.Length, m_GroupData));
     }
 }
        // ----------------------------------------------------------------------------------------------------------
        // 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);
        }
        public static void SetChunkCount(Chunk *chunk, int newCount,
                                         EntityComponentStore *entityComponentStore,
                                         ManagedComponentStore managedComponentStore,
                                         EntityGroupManager entityGroupManager)
        {
            Assert.AreNotEqual(newCount, chunk->Count);
            Assert.IsFalse(chunk->Locked);
            Assert.IsTrue(!chunk->LockedEntityOrder || newCount == 0);

            // Chunk released to empty chunk pool
            if (newCount == 0)
            {
                ReleaseChunk(chunk, entityComponentStore, managedComponentStore, entityGroupManager);
                return;
            }

            var capacity = chunk->Capacity;

            // Chunk is now full
            if (newCount == capacity)
            {
                // this chunk no longer has empty slots, so it shouldn't be in the empty slot list.
                chunk->Archetype->EmptySlotTrackingRemoveChunk(chunk);
            }
            // Chunk is no longer full
            else if (chunk->Count == capacity)
            {
                Assert.IsTrue(newCount < chunk->Count);
                chunk->Archetype->EmptySlotTrackingAddChunk(chunk);
            }

            chunk->Count = newCount;
            chunk->Archetype->Chunks.SetChunkEntityCount(chunk->ListIndex, newCount);
        }
Exemplo n.º 6
0
        /// <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);
            }
        }
Exemplo n.º 7
0
        internal void DestroyInstance()
        {
            EndExclusiveEntityTransaction();

            m_ComponentJobSafetyManager->PreDisposeCheck();

            m_UniversalQuery.Dispose();
            m_UniversalQuery = null;

            m_ComponentJobSafetyManager->Dispose();
            UnsafeUtility.Free(m_ComponentJobSafetyManager, Allocator.Persistent);
            m_ComponentJobSafetyManager = null;

            Entities.EntityComponentStore.Destroy(m_EntityComponentStore);

            m_EntityComponentStore = null;
            m_EntityGroupManager.Dispose();
            m_EntityGroupManager = null;
            m_ExclusiveEntityTransaction.OnDestroy();

            m_ManagedComponentStore.Dispose();

            m_World = null;
            m_Debug = null;

            TypeManager.Shutdown();
        }
Exemplo n.º 8
0
        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);
            }
        }
        public static void CreateEntities(Archetype *archetype, Entity *entities, int count,
                                          EntityComponentStore *entityComponentStore,
                                          ManagedComponentStore managedComponentStore,
                                          EntityGroupManager entityGroupManager)
        {
            var sharedComponentValues = stackalloc int[archetype->NumSharedComponents];

            UnsafeUtility.MemClear(sharedComponentValues, archetype->NumSharedComponents * sizeof(int));

            while (count != 0)
            {
                var chunk = GetChunkWithEmptySlots(archetype, sharedComponentValues,
                                                   entityComponentStore, managedComponentStore, entityGroupManager);

                int allocatedIndex;
                var allocatedCount = AllocateIntoChunk(chunk, count, out allocatedIndex,
                                                       entityComponentStore, managedComponentStore, entityGroupManager);
                entityComponentStore->AllocateEntities(archetype, chunk, allocatedIndex, allocatedCount, entities);
                ChunkDataUtility.InitializeComponents(chunk, allocatedIndex, allocatedCount);
                chunk->SetAllChangeVersions(entityComponentStore->GlobalSystemVersion);
                entities += allocatedCount;
                count    -= allocatedCount;
            }

            entityComponentStore->IncrementComponentTypeOrderVersion(archetype);
        }
Exemplo n.º 10
0
        protected override void OnDestroyManager()
        {
            EndExclusiveEntityTransaction();

            ComponentJobSafetyManager.PreDisposeCheck();

            // Clean up all entities. This is needed to free all internal buffer allocations so memory is not leaked.
            using (var allEntities = GetAllEntities())
            {
                DestroyEntity(allEntities);
            }

            ComponentJobSafetyManager.Dispose();
            ComponentJobSafetyManager = null;

            Entities->OnDestroy();
            UnsafeUtility.Free(Entities, Allocator.Persistent);
            Entities = null;
            ArchetypeManager.Dispose();
            ArchetypeManager = null;
            m_GroupManager.Dispose();
            m_GroupManager = null;
            m_ExclusiveEntityTransaction.OnDestroyManager();

            m_SharedComponentManager.Dispose();

            UnsafeUtility.Free(m_CachedComponentTypeArray, Allocator.Persistent);
            m_CachedComponentTypeArray = null;

            UnsafeUtility.Free(m_CachedComponentTypeInArchetypeArray, Allocator.Persistent);
            m_CachedComponentTypeInArchetypeArray = null;
        }
Exemplo n.º 11
0
        public Archetype *GetOrCreateArchetype(ComponentTypeInArchetype *types, int count,
                                               EntityGroupManager groupManager)
        {
            var srcArchetype = GetOrCreateArchetypeInternal(types, count, groupManager);

            var removedTypes    = 0;
            var prefabTypeIndex = TypeManager.GetTypeIndex <Prefab>();

            for (var t = 0; t < srcArchetype->TypesCount; ++t)
            {
                var type = srcArchetype->Types[t];
                var skip = type.IsSystemStateComponent || type.IsSystemStateSharedComponent || (type.TypeIndex == prefabTypeIndex);
                if (skip)
                {
                    ++removedTypes;
                }
                else
                {
                    types[t - removedTypes] = srcArchetype->Types[t];
                }
            }

            srcArchetype->InstantiableArchetype = srcArchetype;
            if (removedTypes > 0)
            {
                var instantiableArchetype = GetOrCreateArchetypeInternal(types, count - removedTypes, groupManager);

                srcArchetype->InstantiableArchetype          = instantiableArchetype;
                instantiableArchetype->InstantiableArchetype = instantiableArchetype;
            }

            return(srcArchetype);
        }
Exemplo n.º 12
0
        private Archetype *GetOrCreateArchetypeInternal(ComponentTypeInArchetype *types, int count,
                                                        EntityGroupManager groupManager)
        {
            var type = GetExistingArchetype(types, count);

            return(type != null ? type : CreateArchetypeInternal(types, count, groupManager));
        }
Exemplo n.º 13
0
        internal EntityManager(World world)
        {
            TypeManager.Initialize();

            m_World = world;

            m_ComponentJobSafetyManager =
                (ComponentJobSafetyManager *)UnsafeUtility.Malloc(sizeof(ComponentJobSafetyManager), 64,
                                                                  Allocator.Persistent);
            m_ComponentJobSafetyManager->OnCreate();

            m_EntityComponentStore  = Entities.EntityComponentStore.Create(world.SequenceNumber << 32);
            m_ManagedComponentStore = new ManagedComponentStore();
            m_EntityGroupManager    = new EntityGroupManager(m_ComponentJobSafetyManager);

            m_ExclusiveEntityTransaction = new ExclusiveEntityTransaction(EntityGroupManager,
                                                                          m_ManagedComponentStore, EntityComponentStore);

            m_UniversalQuery = CreateEntityQuery(
                new EntityQueryDesc
            {
                Options = EntityQueryOptions.IncludePrefab | EntityQueryOptions.IncludeDisabled
            }
                );
        }
Exemplo n.º 14
0
        public unsafe void RemoveComponent(Entity entity, ComponentType type)
        {
            this.CheckAccess();
            EntityGroupManager target = (EntityGroupManager)this.m_EntityGroupManager.Target;

            this.m_Entities.AssertEntityHasComponent(entity, type);
            this.m_Entities.RemoveComponent(entity, type, this.ArchetypeManager, this.SharedComponentDataManager, target, this.m_CachedComponentTypeInArchetypeArray);
        }
Exemplo n.º 15
0
        private unsafe void DestroyEntityInternal(Entity *entities, int count)
        {
            this.CheckAccess();
            this.m_Entities.AssertEntitiesExist(entities, count);
            EntityGroupManager target = (EntityGroupManager)this.m_EntityGroupManager.Target;

            this.m_Entities.TryRemoveEntityId(entities, count, this.ArchetypeManager, this.SharedComponentDataManager, target, this.m_CachedComponentTypeInArchetypeArray);
        }
        static void ConstructChunk(Archetype *archetype, Chunk *chunk,
                                   SharedComponentValues sharedComponentValues,
                                   EntityComponentStore *entityComponentStore,
                                   ManagedComponentStore managedComponentStore,
                                   EntityGroupManager entityGroupManager)
        {
            chunk->Archetype       = archetype;
            chunk->Count           = 0;
            chunk->Capacity        = archetype->ChunkCapacity;
            chunk->SequenceNumber  = entityComponentStore->AssignSequenceNumber(chunk);
            chunk->metaChunkEntity = Entity.Null;

            var numSharedComponents = archetype->NumSharedComponents;

            if (numSharedComponents > 0)
            {
                for (var i = 0; i < archetype->NumSharedComponents; ++i)
                {
                    var sharedComponentIndex = sharedComponentValues[i];
                    managedComponentStore.AddReference(sharedComponentIndex);
                }
            }

            archetype->AddToChunkList(chunk, sharedComponentValues, entityComponentStore->GlobalSystemVersion);

            Assert.IsTrue(archetype->Chunks.Count != 0);

            // Chunk can't be locked at at construction time
            archetype->EmptySlotTrackingAddChunk(chunk);

            if (numSharedComponents == 0)
            {
                Assert.IsTrue(archetype->ChunksWithEmptySlots.Count != 0);
            }
            else
            {
                Assert.IsTrue(archetype->FreeChunksBySharedComponents.TryGet(chunk->SharedComponentValues,
                                                                             archetype->NumSharedComponents) != null);
            }

            if (archetype->NumManagedArrays > 0)
            {
                chunk->ManagedArrayIndex =
                    managedComponentStore.AllocateManagedArrayStorage(archetype->NumManagedArrays * chunk->Capacity);
            }
            else
            {
                chunk->ManagedArrayIndex = -1;
            }

            chunk->Flags = 0;

            if (archetype->MetaChunkArchetype != null)
            {
                CreateMetaEntityForChunk(chunk, entityComponentStore, managedComponentStore, entityGroupManager);
            }
        }
 public static void DestroyMetaChunkEntity(Entity entity,
                                           EntityComponentStore *entityComponentStore,
                                           ManagedComponentStore managedComponentStore,
                                           EntityGroupManager entityGroupManager)
 {
     EntityManagerChangeArchetypeUtility.RemoveComponent(entity, ComponentType.ReadWrite <ChunkHeader>(),
                                                         entityComponentStore, managedComponentStore, entityGroupManager);
     DestroyEntities(&entity, 1, entityComponentStore, managedComponentStore, entityGroupManager);
 }
        public static void InstantiateEntities(Entity srcEntity, Entity *outputEntities, int instanceCount,
                                               EntityComponentStore *entityComponentStore,
                                               ManagedComponentStore managedComponentStore,
                                               EntityGroupManager entityGroupManager)
        {
            var linkedType = TypeManager.GetTypeIndex <LinkedEntityGroup>();

            if (entityComponentStore->HasComponent(srcEntity, linkedType))
            {
                var header      = (BufferHeader *)entityComponentStore->GetComponentDataWithTypeRO(srcEntity, linkedType);
                var entityPtr   = (Entity *)BufferHeader.GetElementPointer(header);
                var entityCount = header->Length;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
                if (entityCount == 0 || entityPtr[0] != srcEntity)
                {
                    throw new ArgumentException("LinkedEntityGroup[0] must always be the Entity itself.");
                }
                for (int i = 0; i < entityCount; i++)
                {
                    if (!entityComponentStore->Exists(entityPtr[i]))
                    {
                        throw new ArgumentException(
                                  "The srcEntity's LinkedEntityGroup references an entity that is invalid. (Entity at index {i} on the LinkedEntityGroup.)");
                    }

                    var archetype = entityComponentStore->GetArchetype(entityPtr[i]);
                    if (archetype->InstantiableArchetype == null)
                    {
                        throw new ArgumentException(
                                  "The srcEntity's LinkedEntityGroup references an entity that has already been destroyed. (Entity at index {i} on the LinkedEntityGroup. Only system state components are left on the entity)");
                    }
                }
#endif
                InstantiateEntitiesGroup(entityPtr, entityCount, outputEntities, instanceCount,
                                         entityComponentStore, managedComponentStore, entityGroupManager);
            }
            else
            {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                if (!entityComponentStore->Exists(srcEntity))
                {
                    throw new ArgumentException("srcEntity is not a valid entity");
                }

                var srcArchetype = entityComponentStore->GetArchetype(srcEntity);
                if (srcArchetype->InstantiableArchetype == null)
                {
                    throw new ArgumentException(
                              "srcEntity is not instantiable because it has already been destroyed. (Only system state components are left on it)");
                }
#endif
                InstantiateEntitiesOne(srcEntity, outputEntities, instanceCount, null, 0,
                                       entityComponentStore, managedComponentStore, entityGroupManager);
            }
        }
Exemplo n.º 19
0
 private Archetype *GetEntityOnlyArchetype(ComponentTypeInArchetype *types, EntityGroupManager groupManager)
 {
     if (m_entityOnlyArchetype == null)
     {
         m_entityOnlyArchetype = GetOrCreateArchetypeInternal(types, 1, groupManager);
         m_entityOnlyArchetype->InstantiableArchetype       = m_entityOnlyArchetype;
         m_entityOnlyArchetype->SystemStateResidueArchetype = null;
     }
     return(m_entityOnlyArchetype);
 }
Exemplo n.º 20
0
        internal unsafe EntityArchetype CreateArchetype(ComponentType *types, int count)
        {
            EntityArchetype archetype;

            this.CheckAccess();
            EntityGroupManager target = (EntityGroupManager)this.m_EntityGroupManager.Target;

            archetype.Archetype = this.ArchetypeManager.GetOrCreateArchetype(this.m_CachedComponentTypeInArchetypeArray, this.PopulatedCachedTypeInArchetypeArray(types, count), target);
            return(archetype);
        }
Exemplo n.º 21
0
        internal ExclusiveEntityTransaction(EntityGroupManager entityGroupManager,
                                            ManagedComponentStore managedComponentStore, EntityComponentStore *componentStore)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            m_Safety = new AtomicSafetyHandle();
#endif
            m_EntityComponentStore  = componentStore;
            m_EntityGroupManager    = GCHandle.Alloc(entityGroupManager, GCHandleType.Weak);
            m_ManagedComponentStore = GCHandle.Alloc(managedComponentStore, GCHandleType.Weak);
        }
        public static void DestroyChunkForDiffing(Chunk *chunk,
                                                  EntityComponentStore *entityComponentStore,
                                                  ManagedComponentStore managedComponentStore,
                                                  EntityGroupManager entityGroupManager)
        {
            chunk->Archetype->EntityCount -= chunk->Count;
            entityComponentStore->FreeEntities(chunk);

            EntityManagerCreateDestroyEntitiesUtility.SetChunkCount(chunk, 0,
                                                                    entityComponentStore, managedComponentStore, entityGroupManager);
        }
        internal ExclusiveEntityTransaction(ArchetypeManager archetypes, EntityGroupManager entityGroupManager,
                                            SharedComponentDataManager sharedComponentDataManager, EntityDataManager *data)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            m_Safety = new AtomicSafetyHandle();
#endif
            m_Entities                   = data;
            m_ArchetypeManager           = GCHandle.Alloc(archetypes, GCHandleType.Weak);
            m_EntityGroupManager         = GCHandle.Alloc(entityGroupManager, GCHandleType.Weak);
            m_SharedComponentDataManager = GCHandle.Alloc(sharedComponentDataManager, GCHandleType.Weak);
        }
Exemplo n.º 24
0
        /// <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);
        }
Exemplo n.º 25
0
        private unsafe void InstantiateInternal(Entity srcEntity, Entity *outputEntities, int count)
        {
            this.CheckAccess();
            if (!this.m_Entities.Exists(srcEntity))
            {
                throw new ArgumentException("srcEntity is not a valid entity");
            }
            EntityGroupManager target = (EntityGroupManager)this.m_EntityGroupManager.Target;

            this.m_Entities.InstantiateEntities(this.ArchetypeManager, this.SharedComponentDataManager, target, srcEntity, outputEntities, count, this.m_CachedComponentTypeInArchetypeArray);
        }
        public static Chunk *GetCleanChunk(Archetype *archetype, SharedComponentValues sharedComponentValues,
                                           EntityComponentStore *entityComponentStore,
                                           ManagedComponentStore managedComponentStore,
                                           EntityGroupManager entityGroupManager)
        {
            Chunk *newChunk = entityComponentStore->AllocateChunk();

            ConstructChunk(archetype, newChunk, sharedComponentValues,
                           entityComponentStore, managedComponentStore, entityGroupManager);

            return(newChunk);
        }
        public static int AllocateIntoChunk(Chunk *chunk,
                                            EntityComponentStore *entityComponentStore,
                                            ManagedComponentStore managedComponentStore,
                                            EntityGroupManager entityGroupManager)
        {
            int outIndex;
            var res = AllocateIntoChunk(chunk, 1, out outIndex,
                                        entityComponentStore, managedComponentStore, entityGroupManager);

            Assert.AreEqual(1, res);
            return(outIndex);
        }
Exemplo n.º 28
0
 protected override unsafe void OnCreateManager()
 {
     TypeManager.Initialize();
     this.Entities = (EntityDataManager *)UnsafeUtility.Malloc((long)sizeof(EntityDataManager), 0x40, Allocator.Persistent);
     this.Entities.OnCreate();
     this.m_SharedComponentManager              = new SharedComponentDataManager();
     this.ArchetypeManager                      = new Unity.Entities.ArchetypeManager(this.m_SharedComponentManager);
     this.ComponentJobSafetyManager             = new Unity.Entities.ComponentJobSafetyManager();
     this.m_GroupManager                        = new EntityGroupManager(this.ComponentJobSafetyManager);
     this.m_ExclusiveEntityTransaction          = new ExclusiveEntityTransaction(this.ArchetypeManager, this.m_GroupManager, this.m_SharedComponentManager, this.Entities);
     this.m_CachedComponentTypeInArchetypeArray = (ComponentTypeInArchetype *)UnsafeUtility.Malloc((long)(sizeof(ComponentTypeInArchetype) * 0x400), 0x10, Allocator.Persistent);
 }
Exemplo n.º 29
0
        internal ExclusiveEntityTransaction(ArchetypeManager archetypes, EntityGroupManager entityGroupManager, SharedComponentDataManager sharedComponentDataManager, EntityDataManager *data)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            m_Safety = new AtomicSafetyHandle();
#endif
            m_Entities                   = data;
            m_ArchetypeManager           = GCHandle.Alloc(archetypes, GCHandleType.Weak);
            m_EntityGroupManager         = GCHandle.Alloc(entityGroupManager, GCHandleType.Weak);
            m_SharedComponentDataManager = GCHandle.Alloc(sharedComponentDataManager, GCHandleType.Weak);

            m_CachedComponentTypeInArchetypeArray = (ComponentTypeInArchetype *)UnsafeUtility.Malloc(sizeof(ComponentTypeInArchetype) * 32 * 1024, 16, Allocator.Persistent);
        }
        public static int AllocateIntoChunk(Chunk *chunk, int count, out int outIndex,
                                            EntityComponentStore *entityComponentStore,
                                            ManagedComponentStore managedComponentStore,
                                            EntityGroupManager entityGroupManager)
        {
            var allocatedCount = Math.Min(chunk->Capacity - chunk->Count, count);

            outIndex = chunk->Count;
            SetChunkCount(chunk, chunk->Count + allocatedCount,
                          entityComponentStore, managedComponentStore, entityGroupManager);
            chunk->Archetype->EntityCount += allocatedCount;
            return(allocatedCount);
        }