/// <summary>
 /// Creates a set of entities of the specified archetype.
 /// </summary>
 /// <remarks>Fills the [NativeArray](https://docs.unity3d.com/ScriptReference/Unity.Collections.NativeArray_1.html)
 /// object assigned to the `entities` parameter with the Entity objects of the created entities. Each entity
 /// has the components specified by the <see cref="EntityArchetype"/> object assigned
 /// to the `archetype` parameter. The EntityManager adds these entities to the <see cref="World"/> entity list. Use the
 /// Entity objects in the array for further processing, such as setting the component values.</remarks>
 /// <param name="archetype">The archetype defining the structure for the new entities.</param>
 /// <param name="entities">An array to hold the Entity objects needed to access the new entities.
 /// The length of the array determines how many entities are created.</param>
 public void CreateEntity(EntityArchetype archetype, NativeArray <Entity> entities)
 {
     BeforeStructuralChange();
     EntityManagerCreateDestroyEntitiesUtility.CreateEntities(archetype.Archetype,
                                                              (Entity *)entities.GetUnsafePtr(), entities.Length,
                                                              EntityComponentStore, ManagedComponentStore);
 }
        // ----------------------------------------------------------------------------------------------------------
        // PUBLIC
        // ----------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Creates an entity having the specified archetype.
        /// </summary>
        /// <remarks>
        /// The EntityManager creates the entity in the first available chunk with the matching archetype that has
        /// enough space.
        ///
        /// **Important:** This function creates a sync point, which means that the EntityManager waits for all
        /// currently running Jobs to complete before creating the entity 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="archetype">The archetype for the new entity.</param>
        /// <returns>The Entity object that you can use to access the entity.</returns>
        public Entity CreateEntity(EntityArchetype archetype)
        {
            Entity entity;

            BeforeStructuralChange();
            EntityManagerCreateDestroyEntitiesUtility.CreateEntities(archetype.Archetype, &entity, 1, EntityComponentStore, ManagedComponentStore);
            return(entity);
        }
Пример #3
0
        public Entity CreateEntity(EntityArchetype archetype)
        {
            CheckAccess();

            Entity entity;

            EntityManagerCreateDestroyEntitiesUtility.CreateEntities(archetype.Archetype, &entity, 1, EntityComponentStore, ManagedComponentStore);
            return(entity);
        }
        public Entity CreateEntity()
        {
            BeforeStructuralChange();
            Entity entity;

            EntityManagerCreateDestroyEntitiesUtility.CreateEntities(
                GetEntityOnlyArchetype().Archetype, &entity, 1,
                EntityComponentStore, ManagedComponentStore);
            return(entity);
        }
Пример #5
0
        public static void MoveChunks(
            NativeArray <ArchetypeChunk> chunks,
            NativeArray <EntityRemapUtility.EntityRemapInfo> entityRemapping,
            EntityComponentStore *srcEntityComponentStore,
            ManagedComponentStore srcManagedComponentStore,
            EntityComponentStore *dstEntityComponentStore,
            ManagedComponentStore dstManagedComponentStore)
        {
            new MoveChunksJob
            {
                srcEntityComponentStore = srcEntityComponentStore,
                dstEntityComponentStore = dstEntityComponentStore,
                entityRemapping         = entityRemapping,
                chunks = chunks
            }.Run();

            int chunkCount  = chunks.Length;
            var remapChunks = new NativeArray <RemapChunk>(chunkCount, Allocator.TempJob);

            for (int i = 0; i < chunkCount; ++i)
            {
                var chunk     = chunks[i].m_Chunk;
                var archetype = chunk->Archetype;

                //TODO: this should not be done more than once for each archetype
                var dstArchetype =
                    EntityManagerCreateArchetypeUtility.GetOrCreateArchetype(archetype->Types, archetype->TypesCount,
                                                                             dstEntityComponentStore);

                remapChunks[i] = new RemapChunk {
                    chunk = chunk, dstArchetype = dstArchetype
                };
                chunk->SequenceNumber = dstEntityComponentStore->AssignSequenceNumber(chunk);

                if (archetype->MetaChunkArchetype != null)
                {
                    Entity srcEntity = chunk->metaChunkEntity;
                    Entity dstEntity;

                    EntityManagerCreateDestroyEntitiesUtility.CreateEntities(dstArchetype->MetaChunkArchetype,
                                                                             &dstEntity, 1,
                                                                             dstEntityComponentStore, dstManagedComponentStore);

                    srcEntityComponentStore->GetChunk(srcEntity, out var srcChunk, out var srcIndex);
                    dstEntityComponentStore->GetChunk(dstEntity, out var dstChunk, out var dstIndex);

                    ChunkDataUtility.SwapComponents(srcChunk, srcIndex, dstChunk, dstIndex, 1,
                                                    srcEntityComponentStore->GlobalSystemVersion, dstEntityComponentStore->GlobalSystemVersion);
                    EntityRemapUtility.AddEntityRemapping(ref entityRemapping, srcEntity, dstEntity);

                    EntityManagerCreateDestroyEntitiesUtility.DestroyEntities(&srcEntity, 1,
                                                                              srcEntityComponentStore, srcManagedComponentStore);
                }
            }

            k_ProfileMoveSharedComponents.Begin();
            var remapShared =
                dstManagedComponentStore.MoveSharedComponents(srcManagedComponentStore, chunks,
                                                              entityRemapping,
                                                              Allocator.TempJob);

            k_ProfileMoveSharedComponents.End();

            var remapChunksJob = new RemapChunksJob
            {
                dstEntityComponentStore = dstEntityComponentStore,
                remapChunks             = remapChunks,
                entityRemapping         = entityRemapping
            }.Schedule(remapChunks.Length, 1);

            var moveChunksBetweenArchetypeJob = new MoveChunksBetweenArchetypeJob
            {
                remapChunks         = remapChunks,
                remapShared         = remapShared,
                globalSystemVersion = dstEntityComponentStore->GlobalSystemVersion
            }.Schedule(remapChunksJob);

            moveChunksBetweenArchetypeJob.Complete();

            remapShared.Dispose();
            remapChunks.Dispose();
        }
 public void CreateEntity(EntityArchetype archetype, NativeArray <Entity> entities)
 {
     CheckAccess();
     EntityManagerCreateDestroyEntitiesUtility.CreateEntities(archetype.Archetype, (Entity *)entities.GetUnsafePtr(), entities.Length, EntityComponentStore, ManagedComponentStore, EntityGroupManager);
 }