public void AddComponent(UnsafeMatchingArchetypePtrList archetypeList, EntityQueryFilter filter, ComponentType componentType) { if (!m_IsMainThread) { throw new InvalidOperationException("Must be called from the main thread"); } EntityComponentStore->AssertCanAddComponent(archetypeList, componentType); using (var chunks = ChunkIterationUtility.CreateArchetypeChunkArray(archetypeList, Allocator.TempJob, ref filter, EntityManager.DependencyManager)) { if (chunks.Length == 0) { return; } EntityComponentStore->AssertCanAddComponent(chunks, componentType); EntityManager.BeforeStructuralChange(); var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking(); //@TODO the fast path for a chunk that contains a single entity is only possible if the chunk doesn't have a Locked Entity Order //but we should still be allowed to add zero sized components to chunks with a Locked Entity Order, even ones that only contain a single entity /* * if ((chunks.Length == 1) && (chunks[0].Count == 1)) * { * var entityPtr = (Entity*) chunks[0].m_Chunk->Buffer; * StructuralChange.AddComponentEntity(EntityComponentStore, entityPtr, componentType.TypeIndex); * } * else * { */ StructuralChange.AddComponentChunks(EntityComponentStore, (ArchetypeChunk *)NativeArrayUnsafeUtility.GetUnsafePtr(chunks), chunks.Length, componentType.TypeIndex); /* * } */ var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges); EntityQueryManager.AddAdditionalArchetypes(changedArchetypes); ManagedComponentStore.Playback(ref EntityComponentStore->ManagedChangesTracker); } }
/// <summary> /// EntityManager.BeforeStructuralChange must be called before invoking this. /// ManagedComponentStore.Playback must be called after invoking this. /// EntityQueryManager.AddAdditionalArchetypes must be called after invoking this. /// Invoking this must be wrapped in ArchetypeChangeTracking. /// </summary> /// <param name="archetypeList"></param> /// <param name="filter"></param> /// <param name="componentType"></param> /// <exception cref="InvalidOperationException"></exception> public void AddComponentDuringStructuralChange(UnsafeMatchingArchetypePtrList archetypeList, EntityQueryFilter filter, ComponentType componentType) { if (!m_IsMainThread) { throw new InvalidOperationException("Must be called from the main thread"); } using (var chunks = ChunkIterationUtility.CreateArchetypeChunkArray(archetypeList, Allocator.TempJob, ref filter, DependencyManager)) { if (chunks.Length == 0) { return; } StructuralChange.AddComponentChunks(EntityComponentStore, (ArchetypeChunk *)NativeArrayUnsafeUtility.GetUnsafePtr(chunks), chunks.Length, componentType.TypeIndex); } }