public void InstantiateEntities(ArchetypeManager archetypeManager, SharedComponentDataManager sharedComponentDataManager, EntityGroupManager groupManager, Entity srcEntity, Entity *outputEntities, int count, ComponentTypeInArchetype *componentTypeInArchetypeArray) { var srcIndex = m_Entities[srcEntity.Index].IndexInChunk; var srcChunk = m_Entities[srcEntity.Index].Chunk; var srcArchetype = GetArchetype(srcEntity); var dstArchetype = GetInstantiableArchetype(srcEntity, archetypeManager, groupManager, componentTypeInArchetypeArray); var srcSharedComponentDataIndices = GetComponentChunk(srcEntity)->SharedComponentValueArray; while (count != 0) { var chunk = archetypeManager.GetChunkWithEmptySlots(dstArchetype, srcSharedComponentDataIndices); int indexInChunk; var allocatedCount = archetypeManager.AllocateIntoChunk(chunk, count, out indexInChunk); ChunkDataUtility.ReplicateComponents(srcChunk, srcIndex, chunk, indexInChunk, allocatedCount); AllocateEntities(dstArchetype, chunk, indexInChunk, allocatedCount, outputEntities); outputEntities += allocatedCount; count -= allocatedCount; } IncrementComponentOrderVersion(dstArchetype, srcChunk, sharedComponentDataManager); }
int InstantiateEntitiesOne(Entity srcEntity, Entity *outputEntities, int instanceCount, InstantiateRemapChunk *remapChunks, int remapChunksCount) { var src = GetEntityInChunk(srcEntity); var srcArchetype = src.Chunk->Archetype; var dstArchetype = srcArchetype->InstantiableArchetype; var archetypeChunkFilter = new ArchetypeChunkFilter(); archetypeChunkFilter.Archetype = dstArchetype; if (RequiresBuildingResidueSharedComponentIndices(srcArchetype, dstArchetype)) { BuildResidueSharedComponentIndices(srcArchetype, dstArchetype, src.Chunk->SharedComponentValues, archetypeChunkFilter.SharedComponentValues); } else { // Always copy shared component indices since GetChunkWithEmptySlots might reallocate the storage of SharedComponentValues src.Chunk->SharedComponentValues.CopyTo(archetypeChunkFilter.SharedComponentValues, 0, dstArchetype->NumSharedComponents); } Chunk *chunk = null; int instanceBeginIndex = 0; while (instanceBeginIndex != instanceCount) { chunk = GetChunkWithEmptySlots(ref archetypeChunkFilter); int indexInChunk; var allocatedCount = AllocateIntoChunk(chunk, instanceCount - instanceBeginIndex, out indexInChunk); ChunkDataUtility.ReplicateComponents(src.Chunk, src.IndexInChunk, chunk, indexInChunk, allocatedCount); AllocateEntities(dstArchetype, chunk, indexInChunk, allocatedCount, outputEntities + instanceBeginIndex); if (srcArchetype->NumManagedArrays > 0) { ManagedChangesTracker.ReplicateManagedObjects(src.Chunk, src.IndexInChunk, chunk, indexInChunk, allocatedCount, srcEntity, outputEntities + instanceBeginIndex); } chunk->SetAllChangeVersions(GlobalSystemVersion); #if UNITY_EDITOR for (var i = 0; i < allocatedCount; ++i) { CopyName(outputEntities[i + instanceBeginIndex], srcEntity); } #endif if (remapChunks != null) { remapChunks[remapChunksCount].Chunk = chunk; remapChunks[remapChunksCount].IndexInChunk = indexInChunk; remapChunks[remapChunksCount].AllocatedCount = allocatedCount; remapChunks[remapChunksCount].InstanceBeginIndex = instanceBeginIndex; remapChunksCount++; } instanceBeginIndex += allocatedCount; } if (chunk != null) { ManagedChangesTracker.IncrementComponentOrderVersion(dstArchetype, chunk->SharedComponentValues); IncrementComponentTypeOrderVersion(dstArchetype); } return(remapChunksCount); }
static int InstantiateEntitiesOne(Entity srcEntity, Entity *outputEntities, int instanceCount, InstantiateRemapChunk *remapChunks, int remapChunksCount, EntityComponentStore *entityComponentStore, ManagedComponentStore managedComponentStore) { var src = entityComponentStore->GetEntityInChunk(srcEntity); var srcArchetype = src.Chunk->Archetype; var dstArchetype = srcArchetype->InstantiableArchetype; var temp = stackalloc int[dstArchetype->NumSharedComponents]; if (RequiresBuildingResidueSharedComponentIndices(srcArchetype, dstArchetype)) { BuildResidueSharedComponentIndices(srcArchetype, dstArchetype, src.Chunk->SharedComponentValues, temp); } else { // Always copy shared component indices since GetChunkWithEmptySlots might reallocate the storage of SharedComponentValues src.Chunk->SharedComponentValues.CopyTo(temp, 0, dstArchetype->NumSharedComponents); } SharedComponentValues sharedComponentValues = temp; Chunk *chunk = null; int instanceBeginIndex = 0; while (instanceBeginIndex != instanceCount) { chunk = GetChunkWithEmptySlots(dstArchetype, sharedComponentValues, entityComponentStore, managedComponentStore); int indexInChunk; var allocatedCount = AllocateIntoChunk(chunk, instanceCount - instanceBeginIndex, out indexInChunk, entityComponentStore, managedComponentStore); ChunkDataUtility.ReplicateComponents(src.Chunk, src.IndexInChunk, chunk, indexInChunk, allocatedCount); entityComponentStore->AllocateEntities(dstArchetype, chunk, indexInChunk, allocatedCount, outputEntities + instanceBeginIndex); chunk->SetAllChangeVersions(entityComponentStore->GlobalSystemVersion); #if UNITY_EDITOR for (var i = 0; i < allocatedCount; ++i) { entityComponentStore->SetName(outputEntities[i + instanceBeginIndex], entityComponentStore->GetName(srcEntity)); } #endif if (remapChunks != null) { remapChunks[remapChunksCount].Chunk = chunk; remapChunks[remapChunksCount].IndexInChunk = indexInChunk; remapChunks[remapChunksCount].AllocatedCount = allocatedCount; remapChunks[remapChunksCount].InstanceBeginIndex = instanceBeginIndex; remapChunksCount++; } instanceBeginIndex += allocatedCount; } if (chunk != null) { managedComponentStore.IncrementComponentOrderVersion(dstArchetype, chunk->SharedComponentValues); entityComponentStore->IncrementComponentTypeOrderVersion(dstArchetype); } return(remapChunksCount); }