public void AssertCanRemoveComponents(Entity entity, ComponentTypes types) { for (int i = 0; i < types.Length; ++i) { AssertCanRemoveComponent(entity, ComponentType.FromTypeIndex(types.GetTypeIndex(i))); } }
// Calculates the intersection of "All" queriesDesc private ComponentType *CalculateRequiredComponentsFromQuery(ref UnsafeScratchAllocator allocator, ArchetypeQuery *queries, int queryCount, out int outRequiredComponentsCount) { var maxIntersectionCount = 0; for (int queryIndex = 0; queryIndex < queryCount; ++queryIndex) { maxIntersectionCount = math.max(maxIntersectionCount, queries[queryIndex].AllCount); } var intersection = (int *)allocator.Allocate <int>(maxIntersectionCount); UnsafeUtility.MemCpy(intersection, queries[0].All, sizeof(int) * queries[0].AllCount); var intersectionCount = maxIntersectionCount; for (int i = 1; i < queryCount; ++i) { intersectionCount = IntersectSortedComponentIndexArrays(intersection, intersectionCount, queries[i].All, queries[i].AllCount, intersection); } var outRequiredComponents = (ComponentType *)allocator.Allocate <ComponentType>(intersectionCount + 1); outRequiredComponents[0] = ComponentType.ReadWrite <Entity>(); for (int i = 0; i < intersectionCount; ++i) { outRequiredComponents[i + 1] = ComponentType.FromTypeIndex(intersection[i]); } outRequiredComponentsCount = intersectionCount + 1; return(outRequiredComponents); }
static void AssignHybridComponentsToCompanionGameObjectsDelegate(EntityManager entityManager, NativeArray <Entity> entities) { for (int i = 0; i < entities.Length; ++i) { var entity = entities[i]; var companionGameObject = entityManager.GetComponentData <CompanionLink>(entity).Companion; var archetypeChunk = entityManager.GetChunk(entities[i]); var archetype = archetypeChunk.Archetype.Archetype; var types = archetype->Types; var firstIndex = archetype->FirstManagedComponent; var lastIndex = archetype->ManagedComponentsEnd; for (int t = firstIndex; t < lastIndex; ++t) { var type = TypeManager.GetTypeInfo(types[t].TypeIndex); if (type.Category != TypeManager.TypeCategory.Class) { continue; } var hybridComponent = companionGameObject.GetComponent(type.Type); entityManager.SetComponentObject(entity, ComponentType.FromTypeIndex(type.TypeIndex), hybridComponent); } } entityManager.RemoveComponent <CompanionGameObjectActiveSystemState>(entities); entityManager.RemoveComponent <EditorCompanionInPreviewSceneTag>(entities); }
/// <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); } }
public ComponentDataArray <T> GetComponentDataArray <T>() where T : struct, IComponentData { Profiler.BeginSample("GetComponentDataArray"); var typeIndex = TypeManager.GetTypeIndex <T>(); #if ENABLE_UNITY_COLLECTIONS_CHECKS var componentType = ComponentType.FromTypeIndex(typeIndex); if (componentType.IsZeroSized) { throw new ArgumentException($"GetComponentDataArray<{typeof(T)}> cannot be called on zero-sized IComponentData"); } #endif int length; ComponentChunkIterator iterator; GetComponentChunkIterator(out length, out iterator); var indexInComponentGroup = GetIndexInComponentGroup(typeIndex); ComponentDataArray <T> res; GetComponentDataArray(ref iterator, indexInComponentGroup, length, out res); Profiler.EndSample(); return(res); }
internal ComponentDataFromEntity(int typeIndex, EntityDataManager *entityData) { m_TypeIndex = typeIndex; m_Entities = entityData; m_TypeLookupCache = 0; m_GlobalSystemVersion = entityData->GlobalSystemVersion; m_IsZeroSized = ComponentType.FromTypeIndex(typeIndex).IsZeroSized; }
internal unsafe ComponentDataFromEntity(int typeIndex, EntityDataManager *entityData, AtomicSafetyHandle safety) { this.m_Safety = safety; this.m_TypeIndex = typeIndex; this.m_Entities = entityData; this.m_TypeLookupCache = 0; this.m_GlobalSystemVersion = entityData.GlobalSystemVersion; this.m_IsZeroSized = ComponentType.FromTypeIndex(typeIndex).IsZeroSized; }
internal ComponentDataFromEntity(int typeIndex, EntityComponentStore *entityComponentStoreComponentStore, AtomicSafetyHandle safety) { m_Safety = safety; m_TypeIndex = typeIndex; m_EntityComponentStore = entityComponentStoreComponentStore; m_TypeLookupCache = 0; m_GlobalSystemVersion = entityComponentStoreComponentStore->GlobalSystemVersion; m_IsZeroSized = ComponentType.FromTypeIndex(typeIndex).IsZeroSized; }
internal override void UpdateSerializedData(EntityManager manager, Entity entity) { var typeIndex = TypeManager.GetTypeIndex <T>(); var componentType = ComponentType.FromTypeIndex(typeIndex); if (componentType.IsZeroSized) { return; } m_SerializedData = manager.GetComponentData <T>(entity); }
public unsafe void SetComponentData <T>(Entity entity, T componentData) where T : struct, IComponentData { int typeIndex = TypeManager.GetTypeIndex <T>(); this.Entities.AssertEntityHasComponent(entity, typeIndex); if (ComponentType.FromTypeIndex(typeIndex).IsZeroSized) { throw new ArgumentException($"GetComponentData<{typeof(T)}> can not be called with a zero sized component."); } this.ComponentJobSafetyManager.CompleteReadAndWriteDependency(typeIndex); byte *numPtr = ref this.Entities.GetComponentDataWithTypeRW(entity, typeIndex, this.Entities.GlobalSystemVersion); UnsafeUtility.CopyStructureToPtr <T>(ref componentData, (void *)numPtr); }
public unsafe T GetComponentData <T>(Entity entity) where T : struct, IComponentData { T local; int typeIndex = TypeManager.GetTypeIndex <T>(); this.Entities.AssertEntityHasComponent(entity, typeIndex); if (ComponentType.FromTypeIndex(typeIndex).IsZeroSized) { throw new ArgumentException($"GetComponentData<{typeof(T)}> can not be called with a zero sized component."); } this.ComponentJobSafetyManager.CompleteWriteDependency(typeIndex); UnsafeUtility.CopyPtrToStructure <T>((void *)this.Entities.GetComponentDataWithTypeRO(entity, typeIndex), out local); return(local); }
public object GetComponentBoxed(Entity entity, Type type) { m_Manager.EntityComponentStore->AssertEntitiesExist(&entity, 1); var archetype = m_Manager.m_EntityComponentStore->GetArchetype(entity); var typeIndex = ChunkDataUtility.GetTypeIndexFromType(archetype, type); #if ENABLE_UNITY_COLLECTIONS_CHECKS if (typeIndex == -1) { throw new ArgumentException($"A component with type:{type} has not been added to the entity."); } #endif return(GetComponentBoxed(entity, ComponentType.FromTypeIndex(typeIndex))); }
unsafe EntityQuery ResolveEntityQuery(int *delegateTypeIndices, int delegateTypeCount) { SanitizeTypes(delegateTypeIndices, ref delegateTypeCount); var hash = (uint)m_Any.GetHashCode() * 0xEA928FF9 ^ (uint)m_None.GetHashCode() * 0x4B772F25 ^ (uint)m_All.GetHashCode() * 0xBAEE8991 ^ (uint)m_AnyWritableBitField.GetHashCode() * 0x8F8BF1C7 ^ (uint)m_AllWritableBitField.GetHashCode() * 0xB6D633F7 ^ (uint)m_Options.GetHashCode() * 0xE0B7379B ^ math.hash(delegateTypeIndices, sizeof(int) * delegateTypeCount); var cache = m_System.GetOrCreateEntityQueryCache(); var found = cache.FindQueryInCache(hash); if (found < 0) { // base query from builder spec, but reserve some extra room for the types detected from the delegate var eaq = ToEntityQueryDesc(delegateTypeCount); // now fill out the extra types for (var i = 0; i < delegateTypeCount; ++i) { eaq.All[i + m_All.Length] = ComponentType.FromTypeIndex(delegateTypeIndices[i]); } var query = m_System.GetEntityQuery(eaq); #if ENABLE_UNITY_COLLECTIONS_CHECKS found = cache.CreateCachedQuery(hash, query, ref this, delegateTypeIndices, delegateTypeCount); #else found = cache.CreateCachedQuery(hash, query); #endif } #if ENABLE_UNITY_COLLECTIONS_CHECKS else { cache.ValidateMatchesCache(found, ref this, delegateTypeIndices, delegateTypeCount); // TODO: also validate that m_Query spec matches m_Any/All/None and delegateTypeIndices } #endif return(cache.GetCachedQuery(found)); }
public void SetComponentData <T>(Entity entity, T componentData) where T : struct, IComponentData { var typeIndex = TypeManager.GetTypeIndex <T>(); Entities->AssertEntityHasComponent(entity, typeIndex); #if ENABLE_UNITY_COLLECTIONS_CHECKS if (ComponentType.FromTypeIndex(typeIndex).IsZeroSized) { throw new System.ArgumentException($"GetComponentData<{typeof(T)}> can not be called with a zero sized component."); } #endif ComponentJobSafetyManager.CompleteReadAndWriteDependency(typeIndex); var ptr = Entities->GetComponentDataWithTypeRW(entity, typeIndex, Entities->GlobalSystemVersion); UnsafeUtility.CopyStructureToPtr(ref componentData, ptr); }
internal void GetComponentDataArray <T>(ref ComponentChunkIterator iterator, int indexInComponentGroup, int length, out ComponentDataArray <T> output) where T : struct, IComponentData { #if ENABLE_UNITY_COLLECTIONS_CHECKS var typeIndex = TypeManager.GetTypeIndex <T>(); var componentType = ComponentType.FromTypeIndex(typeIndex); if (componentType.IsZeroSized) { throw new ArgumentException($"GetComponentDataArray<{typeof(T)}> cannot be called on zero-sized IComponentData"); } #endif iterator.IndexInComponentGroup = indexInComponentGroup; #if ENABLE_UNITY_COLLECTIONS_CHECKS output = new ComponentDataArray <T>(iterator, length, GetSafetyHandle(indexInComponentGroup)); #else output = new ComponentDataArray <T>(iterator, length); #endif }
unsafe ComponentGroup ResolveComponentGroup(int *delegateTypeIndices, int delegateTypeCount) { var hash = (uint)m_Any.GetHashCode() * 0xEA928FF9 ^ (uint)m_None.GetHashCode() * 0x4B772F25 ^ (uint)m_All.GetHashCode() * 0xBAEE8991 ^ math.hash(delegateTypeIndices, sizeof(int) * delegateTypeCount); var cache = m_System.GetOrCreateEntityQueryCache(); var found = cache.FindQueryInCache(hash); if (found < 0) { // base query from builder spec, but reserve some extra room for the types detected from the delegate var eaq = ToEntityArchetypeQuery(delegateTypeCount); // now fill out the extra types for (var i = 0; i < delegateTypeCount; ++i) { eaq.All[i + m_All.Length] = ComponentType.FromTypeIndex(delegateTypeIndices[i]); } var group = m_System.GetComponentGroup(eaq); #if ENABLE_UNITY_COLLECTIONS_CHECKS found = cache.CreateCachedQuery(hash, group, ref this, delegateTypeIndices, delegateTypeCount); #else found = cache.CreateCachedQuery(hash, group); #endif } #if ENABLE_UNITY_COLLECTIONS_CHECKS else { cache.ValidateMatchesCache(found, ref this, delegateTypeIndices, delegateTypeCount); // TODO: also validate that m_Group spec matches m_Any/All/None and delegateTypeIndices } #endif return(cache.GetCachedQuery(found)); }
public void SetComponentData <T>(Entity entity, T componentData) where T : struct, IComponentData { var typeIndex = TypeManager.GetTypeIndex <T>(); Entities->AssertEntityHasComponent(entity, typeIndex); // If the user attempts to set a zero-sized type, we do nothing instead. // This is to prevent requiring users from checking for zero-size before calling this API. var componentType = ComponentType.FromTypeIndex(typeIndex); if (componentType.IsZeroSized) { return; } ComponentJobSafetyManager.CompleteReadAndWriteDependency(typeIndex); var ptr = Entities->GetComponentDataWithTypeRW(entity, typeIndex, Entities->GlobalSystemVersion); UnsafeUtility.CopyStructureToPtr(ref componentData, ptr); }
public T GetComponentData <T>(Entity entity) where T : struct, IComponentData { var typeIndex = TypeManager.GetTypeIndex <T>(); Entities->AssertEntityHasComponent(entity, typeIndex); #if ENABLE_UNITY_COLLECTIONS_CHECKS if (ComponentType.FromTypeIndex(typeIndex).IsZeroSized) { throw new System.ArgumentException($"GetComponentData<{typeof(T)}> can not be called with a zero sized component."); } #endif ComponentJobSafetyManager.CompleteWriteDependency(typeIndex); var ptr = Entities->GetComponentDataWithTypeRO(entity, typeIndex); T value; UnsafeUtility.CopyPtrToStructure(ptr, out value); return(value); }
public T GetComponentData <T>(Entity entity) where T : struct, IComponentData { var typeIndex = TypeManager.GetTypeIndex <T>(); Entities->AssertEntityHasComponent(entity, typeIndex); // If the user attempts to get a zero-sized type, we return a default-initialized value instead. // This is to prevent requiring users from checking for zero-size before calling this API. var componentType = ComponentType.FromTypeIndex(typeIndex); if (componentType.IsZeroSized) { return(default(T)); } ComponentJobSafetyManager.CompleteWriteDependency(typeIndex); var ptr = Entities->GetComponentDataWithTypeRO(entity, typeIndex); T value; UnsafeUtility.CopyPtrToStructure(ptr, out value); return(value); }
// ---------------------------------------------------------------------------------------------------------- // PUBLIC // ---------------------------------------------------------------------------------------------------------- public Archetype *GetOrCreateArchetype(ComponentTypeInArchetype *inTypesSorted, int count) { var srcArchetype = GetExistingArchetype(inTypesSorted, count); if (srcArchetype != null) { return(srcArchetype); } srcArchetype = CreateArchetype(inTypesSorted, count); var types = stackalloc ComponentTypeInArchetype[count + 1]; srcArchetype->InstantiateArchetype = CreateInstanceArchetype(inTypesSorted, count, types, srcArchetype, true); srcArchetype->CopyArchetype = CreateInstanceArchetype(inTypesSorted, count, types, srcArchetype, false); if (srcArchetype->InstantiateArchetype != null) { Assert.IsTrue(srcArchetype->InstantiateArchetype->InstantiateArchetype == srcArchetype->InstantiateArchetype); Assert.IsTrue(srcArchetype->InstantiateArchetype->SystemStateResidueArchetype == null); } if (srcArchetype->CopyArchetype != null) { Assert.IsTrue(srcArchetype->CopyArchetype->CopyArchetype == srcArchetype->CopyArchetype); Assert.IsTrue(srcArchetype->CopyArchetype->SystemStateResidueArchetype == null); } // Setup System state cleanup archetype if (srcArchetype->SystemStateCleanupNeeded) { var cleanupEntityType = new ComponentTypeInArchetype(ComponentType.FromTypeIndex(m_CleanupEntityType)); bool cleanupAdded = false; types[0] = inTypesSorted[0]; var newTypeCount = 1; for (var t = 1; t < srcArchetype->TypesCount; ++t) { var type = srcArchetype->Types[t]; if (type.IsSystemStateComponent) { if (!cleanupAdded && (cleanupEntityType < srcArchetype->Types[t])) { types[newTypeCount++] = cleanupEntityType; cleanupAdded = true; } types[newTypeCount++] = srcArchetype->Types[t]; } } if (!cleanupAdded) { types[newTypeCount++] = cleanupEntityType; } var systemStateResidueArchetype = GetOrCreateArchetype(types, newTypeCount); srcArchetype->SystemStateResidueArchetype = systemStateResidueArchetype; Assert.IsTrue(systemStateResidueArchetype->SystemStateResidueArchetype == systemStateResidueArchetype); Assert.IsTrue(systemStateResidueArchetype->InstantiateArchetype == null); Assert.IsTrue(systemStateResidueArchetype->CopyArchetype == null); } // Setup meta chunk archetype if (count > 1) { types[0] = new ComponentTypeInArchetype(m_EntityComponentType); int metaArchetypeTypeCount = 1; for (int i = 1; i < count; ++i) { var t = inTypesSorted[i]; ComponentType typeToInsert; if (inTypesSorted[i].IsChunkComponent) { typeToInsert = new ComponentType { TypeIndex = ChunkComponentToNormalTypeIndex(t.TypeIndex) }; SortingUtilities.InsertSorted(types, metaArchetypeTypeCount++, typeToInsert); } } if (metaArchetypeTypeCount > 1) { SortingUtilities.InsertSorted(types, metaArchetypeTypeCount++, m_ChunkHeaderComponentType); srcArchetype->MetaChunkArchetype = GetOrCreateArchetype(types, metaArchetypeTypeCount); } } return(srcArchetype); }
internal void AddSharedComponentDataBoxed(MatchingArchetypeList archetypeList, EntityQueryFilter filter, int typeIndex, int hashCode, object componentData) { //TODO: optimize this (no need to move the entity to a new chunk twice) var newSharedComponentDataIndex = 0; if (componentData != null) // null means default { newSharedComponentDataIndex = ManagedComponentStore.InsertSharedComponentAssumeNonDefault(typeIndex, hashCode, componentData); } AddSharedComponentData(archetypeList, filter, newSharedComponentDataIndex, ComponentType.FromTypeIndex(typeIndex)); }
public void AssertCanAddComponent(Entity entity, int componentType) { AssertCanAddComponent(entity, ComponentType.FromTypeIndex(componentType)); }
internal void AddComponentRaw(Entity entity, int typeIndex) { AddComponent(entity, ComponentType.FromTypeIndex(typeIndex)); }
// ---------------------------------------------------------------------------------------------------------- // PUBLIC // ---------------------------------------------------------------------------------------------------------- public Archetype *GetOrCreateArchetype(ComponentTypeInArchetype *inTypesSorted, int count) { var srcArchetype = GetExistingArchetype(inTypesSorted, count); if (srcArchetype != null) { return(srcArchetype); } srcArchetype = CreateArchetype(inTypesSorted, count); var types = stackalloc ComponentTypeInArchetype[count + 1]; // Setup Instantiable archetype { UnsafeUtility.MemCpy(types, inTypesSorted, sizeof(ComponentTypeInArchetype) * count); var hasCleanup = false; var removedTypes = 0; for (var t = 0; t < srcArchetype->TypesCount; ++t) { var type = srcArchetype->Types[t]; hasCleanup |= type.TypeIndex == m_CleanupEntityType; var skip = type.IsSystemStateComponent || type.TypeIndex == m_PrefabType; if (skip) { ++removedTypes; } else { types[t - removedTypes] = srcArchetype->Types[t]; } } // Entity has already been destroyed, so it shouldn't be instantiated anymore if (hasCleanup) { srcArchetype->InstantiableArchetype = null; } else if (removedTypes > 0) { var instantiableArchetype = GetOrCreateArchetype(types, count - removedTypes); srcArchetype->InstantiableArchetype = instantiableArchetype; Assert.IsTrue(instantiableArchetype->InstantiableArchetype == instantiableArchetype); Assert.IsTrue(instantiableArchetype->SystemStateResidueArchetype == null); } else { srcArchetype->InstantiableArchetype = srcArchetype; } } // Setup System state cleanup archetype if (srcArchetype->SystemStateCleanupNeeded) { var cleanupEntityType = new ComponentTypeInArchetype(ComponentType.FromTypeIndex(m_CleanupEntityType)); bool cleanupAdded = false; types[0] = inTypesSorted[0]; var newTypeCount = 1; for (var t = 1; t < srcArchetype->TypesCount; ++t) { var type = srcArchetype->Types[t]; if (type.IsSystemStateComponent) { if (!cleanupAdded && (cleanupEntityType < srcArchetype->Types[t])) { types[newTypeCount++] = cleanupEntityType; cleanupAdded = true; } types[newTypeCount++] = srcArchetype->Types[t]; } } if (!cleanupAdded) { types[newTypeCount++] = cleanupEntityType; } var systemStateResidueArchetype = GetOrCreateArchetype(types, newTypeCount); srcArchetype->SystemStateResidueArchetype = systemStateResidueArchetype; Assert.IsTrue(systemStateResidueArchetype->SystemStateResidueArchetype == systemStateResidueArchetype); Assert.IsTrue(systemStateResidueArchetype->InstantiableArchetype == null); } // Setup meta chunk archetype if (count > 1) { types[0] = new ComponentTypeInArchetype(m_EntityComponentType); int metaArchetypeTypeCount = 1; for (int i = 1; i < count; ++i) { var t = inTypesSorted[i]; ComponentType typeToInsert; if (inTypesSorted[i].IsChunkComponent) { typeToInsert = new ComponentType { TypeIndex = ChunkComponentToNormalTypeIndex(t.TypeIndex) }; SortingUtilities.InsertSorted(types, metaArchetypeTypeCount++, typeToInsert); } } if (metaArchetypeTypeCount > 1) { SortingUtilities.InsertSorted(types, metaArchetypeTypeCount++, m_ChunkHeaderComponentType); srcArchetype->MetaChunkArchetype = GetOrCreateArchetype(types, metaArchetypeTypeCount); } } return(srcArchetype); }
internal void AddSharedComponentDataBoxed(Entity entity, int typeIndex, int hashCode, object componentData) { //TODO: optimize this (no need to move the entity to a new chunk twice) AddComponent(entity, ComponentType.FromTypeIndex(typeIndex)); SetSharedComponentDataBoxed(entity, typeIndex, hashCode, componentData); }
internal void RemoveComponentRaw(Entity entity, int typeIndex) { RemoveComponent(entity, ComponentType.FromTypeIndex(typeIndex)); }
internal void AddSharedComponentDataBoxed(Entity entity, int typeIndex, int hashCode, object componentData) { this.AddComponent(entity, ComponentType.FromTypeIndex(typeIndex)); this.SetSharedComponentDataBoxed(entity, typeIndex, hashCode, componentData); }
internal void AddSharedComponentDataBoxedDefaultMustBeNull(UnsafeMatchingArchetypePtrList archetypeList, EntityQueryFilter filter, int typeIndex, int hashCode, object componentData) { var newSharedComponentDataIndex = 0; if (componentData != null) // null means default { newSharedComponentDataIndex = ManagedComponentStore.InsertSharedComponentAssumeNonDefault(typeIndex, hashCode, componentData); } AddSharedComponentData(archetypeList, filter, newSharedComponentDataIndex, ComponentType.FromTypeIndex(typeIndex)); }