/// <summary> /// /// </summary> /// <param name="componentType"></param> /// <param name="manager"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public ArchetypeChunkComponentObjects <T> GetComponentObjects <T>(ArchetypeChunkComponentType <T> componentType, EntityManager manager) where T : class { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(componentType.m_Safety); #endif var archetype = m_Chunk->Archetype; var typeIndexInArchetype = ChunkDataUtility.GetIndexInTypeArray(m_Chunk->Archetype, componentType.m_TypeIndex); NativeArray <int> indexArray; if (typeIndexInArchetype == -1) { indexArray = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <int>(null, 0, 0); } else { var buffer = m_Chunk->Buffer; var length = m_Chunk->Count; var startOffset = archetype->Offsets[typeIndexInArchetype]; indexArray = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <int>(buffer + startOffset, length, Allocator.None); } #if ENABLE_UNITY_COLLECTIONS_CHECKS NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref indexArray, componentType.m_Safety); #endif m_Chunk->SetChangeVersion(typeIndexInArchetype, componentType.GlobalSystemVersion); return(new ArchetypeChunkComponentObjects <T>(indexArray, manager)); }
public NativeArray <T> GetNativeArray <T>(ArchetypeChunkComponentType <T> chunkComponentType) where T : struct, IComponentData { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(chunkComponentType.m_Safety); #endif var archetype = m_Chunk->Archetype; var typeIndex = chunkComponentType.m_TypeIndex; var typeIndexInArchetype = GetIndexInArchetype(typeIndex); if (typeIndexInArchetype == -1) { var emptyResult = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <T>(null, 0, Allocator.Invalid); #if ENABLE_UNITY_COLLECTIONS_CHECKS NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref emptyResult, chunkComponentType.m_Safety); #endif return(emptyResult); } var buffer = m_Chunk->Buffer; var length = m_Chunk->Count; var startOffset = archetype->Offsets[typeIndexInArchetype]; var result = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <T>(buffer + startOffset, length, Allocator.Invalid); #if ENABLE_UNITY_COLLECTIONS_CHECKS NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref result, chunkComponentType.m_Safety); #endif if (!chunkComponentType.IsReadOnly) { m_Chunk->ChangeVersion[typeIndex] = chunkComponentType.GlobalSystemVersion; } return(result); }
public bool Has <T>(ArchetypeChunkComponentType <T> chunkComponentType) where T : struct, IComponentData { var typeIndexInArchetype = ChunkDataUtility.GetIndexInTypeArray(m_Chunk->Archetype, chunkComponentType.m_TypeIndex); return(typeIndexInArchetype != -1); }
public unsafe NativeArray <T> GetNativeArray <T>(ArchetypeChunkComponentType <T> chunkComponentType) where T : struct, IComponentData { NativeArray <T> array3; if (chunkComponentType.m_IsZeroSized) { throw new ArgumentException($"ArchetypeChunk.GetNativeArray<{typeof(T)}> cannot be called on zero-sized IComponentData"); } AtomicSafetyHandle.CheckReadAndThrow(chunkComponentType.m_Safety); Unity.Entities.Archetype *archetype = this.m_Chunk.Archetype; int indexInTypeArray = ChunkDataUtility.GetIndexInTypeArray(this.m_Chunk.Archetype, chunkComponentType.m_TypeIndex); if (indexInTypeArray == -1) { NativeArray <T> array = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <T>(null, 0, Allocator.Invalid); NativeArrayUnsafeUtility.SetAtomicSafetyHandle <T>(ref array, chunkComponentType.m_Safety); array3 = array; } else { int count = this.m_Chunk.Count; NativeArray <T> array = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <T>((void *)(&this.m_Chunk.Buffer.FixedElementField + archetype->Offsets[indexInTypeArray]), count, Allocator.None); NativeArrayUnsafeUtility.SetAtomicSafetyHandle <T>(ref array, chunkComponentType.m_Safety); if (!chunkComponentType.IsReadOnly) { this.m_Chunk.ChangeVersion[indexInTypeArray] = chunkComponentType.GlobalSystemVersion; } array3 = array; } return(array3); }
/// <summary> /// Reports whether any of IComponentData components in the chunk, of the type identified by /// <paramref name="chunkComponentType"/>, could have changed. /// </summary> /// <remarks> /// Note that for efficiency, the change version applies to whole chunks not individual entities. The change /// version is incremented even when another job or system that has declared write access to a component does /// not actually change the component value.</remarks> /// <param name="chunkComponentType">An object containing type and job safety information. Create this /// object by calling <see cref="Unity.Entities.JobComponentSystem.GetArchetypeChunkComponentType{T}"/> immediately /// before scheduling a job. Pass the object to a job using a public field you define as part of the job struct. /// </param> /// <param name="version">The version to compare. In a system, this parameter should be set to the /// current <see cref="Unity.Entities.ComponentSystemBase.LastSystemVersion"/> at the time the job is run or /// scheduled.</param> /// <typeparam name="T">The component type.</typeparam> /// <returns>True, if the version number stored in the chunk for this component is more recent than the version /// passed to the <paramref name="version"/> parameter.</returns> public bool DidChange <T>(ArchetypeChunkComponentType <T> chunkComponentType, uint version) where T : #if UNITY_DISABLE_MANAGED_COMPONENTS struct, #endif IComponentData { return(ChangeVersionUtility.DidChange(GetComponentVersion(chunkComponentType), version)); }
public void SetChunkComponentData <T>(ArchetypeChunkComponentType <T> chunkComponentType, T value) where T : struct { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckWriteAndThrow(chunkComponentType.m_Safety); #endif var ptr = entityComponentStore->GetComponentDataWithTypeRW(m_Chunk->metaChunkEntity, chunkComponentType.m_TypeIndex, entityComponentStore->GlobalSystemVersion); UnsafeUtility.CopyStructureToPtr(ref value, ptr); }
public NativeArray <T> ToComponentDataArray <T>(Allocator allocator, out JobHandle jobhandle) where T : struct, IComponentData { #if ENABLE_UNITY_COLLECTIONS_CHECKS var componentType = new ArchetypeChunkComponentType <T>(m_SafetyManager.GetSafetyHandle(TypeManager.GetTypeIndex <T>(), true), true, EntityDataManager->GlobalSystemVersion); #else var componentType = new ArchetypeChunkComponentType <T>(true, EntityDataManager->GlobalSystemVersion); #endif return(ComponentChunkIterator.CreateComponentDataArray(m_GroupData->MatchingArchetypes, allocator, componentType, this, ref m_Filter, out jobhandle, GetDependency())); }
public uint GetComponentVersion <T>(ArchetypeChunkComponentType <T> chunkComponentType) where T : struct, IComponentData { var typeIndexInArchetype = ChunkDataUtility.GetIndexInTypeArray(m_Chunk->Archetype, chunkComponentType.m_TypeIndex); if (typeIndexInArchetype == -1) { return(0); } return(m_Chunk->GetChangeVersion(typeIndexInArchetype)); }
/// <summary> /// Reports whether this chunk contains the specified component type. /// </summary> /// <remarks>When an <see cref="Unity.Entities.EntityQuery"/> includes optional components (using /// <see cref="EntityQueryDesc.Any"/>), some chunks returned by the query may contain such components and some /// may not. Use this function to determine whether or not the current chunk contains one of these optional /// component types.</remarks> /// <param name="chunkComponentType">An object containing type and job safety information. Create this /// object by calling <see cref="Unity.Entities.JobComponentSystem.GetArchetypeChunkComponentType{T}"/> immediately /// before scheduling a job. Pass the object to a job using a public field you define as part of the job struct. /// </param> /// <typeparam name="T">The data type of the component.</typeparam> /// <returns>True, if this chunk contains an array of the specified component type.</returns> public bool Has <T>(ArchetypeChunkComponentType <T> chunkComponentType) where T : #if UNITY_DISABLE_MANAGED_COMPONENTS struct, #endif IComponentData { var typeIndexInArchetype = ChunkDataUtility.GetIndexInTypeArray(m_Chunk->Archetype, chunkComponentType.m_TypeIndex); return(typeIndexInArchetype != -1); }
public T GetChunkComponentData <T>(ArchetypeChunkComponentType <T> chunkComponentType) where T : struct { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(chunkComponentType.m_Safety); #endif var ptr = entityComponentStore->GetComponentDataWithTypeRO(m_Chunk->metaChunkEntity, chunkComponentType.m_TypeIndex); T value; UnsafeUtility.CopyPtrToStructure(ptr, out value); return(value); }
public uint GetComponentVersion <T>(ArchetypeChunkComponentType <T> chunkComponentType) where T : struct, IComponentData { var typeIndex = chunkComponentType.m_TypeIndex; var typeIndexInArchetype = GetIndexInArchetype(typeIndex); if (typeIndexInArchetype == -1) { return(0); } return(m_Chunk->ChangeVersion[typeIndexInArchetype]); }
public bool HasChunkComponent <T>(ArchetypeChunkComponentType <T> chunkComponentType) where T : struct, IComponentData { var metaChunkArchetype = m_Chunk->Archetype->MetaChunkArchetype; if (metaChunkArchetype == null) { return(false); } var typeIndexInArchetype = ChunkDataUtility.GetIndexInTypeArray(m_Chunk->Archetype->MetaChunkArchetype, chunkComponentType.m_TypeIndex); return(typeIndexInArchetype != -1); }
public unsafe uint GetComponentVersion <T>(ArchetypeChunkComponentType <T> chunkComponentType) where T : struct, IComponentData { uint num2; int indexInTypeArray = ChunkDataUtility.GetIndexInTypeArray(this.m_Chunk.Archetype, chunkComponentType.m_TypeIndex); if (indexInTypeArray == -1) { num2 = 0; } else { num2 = this.m_Chunk.ChangeVersion[indexInTypeArray]; } return(num2); }
public ArchetypeChunkComponentObjects <T> GetComponentObjects <T>(ArchetypeChunkComponentType <T> componentType, EntityManager manager) where T : class { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckReadAndThrow(componentType.m_Safety); #endif var archetype = m_Chunk->Archetype; var typeIndexInArchetype = ChunkDataUtility.GetIndexInTypeArray(archetype, componentType.m_TypeIndex); int offset, length; var array = manager.ArchetypeManager.GetManagedObjectRange(m_Chunk, typeIndexInArchetype, out offset, out length); var componentArray = new ArchetypeChunkComponentObjects <T>(offset, length, array); return(componentArray); }
public static void CopyFromComponentDataArray <T>(MatchingArchetypeList matchingArchetypes, NativeArray <T> componentDataArray, ArchetypeChunkComponentType <T> type, EntityQuery entityQuery, ref EntityQueryFilter filter, out JobHandle jobHandle, JobHandle dependsOn) where T : struct, IComponentData { var job = new CopyComponentArrayToChunks <T> { ComponentData = componentDataArray, ComponentType = type }; jobHandle = job.Schedule(entityQuery, dependsOn); }
public void CopyFromComponentDataArray <T>(NativeArray <T> componentDataArray) where T : struct, IComponentData { // throw if non equal size #if ENABLE_UNITY_COLLECTIONS_CHECKS var entityCount = CalculateEntityCount(); if (entityCount != componentDataArray.Length) { throw new ArgumentException($"Length of input array ({componentDataArray.Length}) does not match length of EntityQuery ({entityCount})"); } var componentType = new ArchetypeChunkComponentType <T>(m_SafetyManager->GetSafetyHandle(TypeManager.GetTypeIndex <T>(), false), false, EntityComponentStore->GlobalSystemVersion); #else var componentType = new ArchetypeChunkComponentType <T>(false, EntityComponentStore->GlobalSystemVersion); #endif ComponentChunkIterator.CopyFromComponentDataArray(m_QueryData->MatchingArchetypes, componentDataArray, componentType, this, ref m_Filter, out var job, GetDependency()); job.Complete(); }
public void CopyFromComponentDataArray <T>(NativeArray <T> componentDataArray, out JobHandle jobhandle) where T : struct, IComponentData { // throw if non equal size #if ENABLE_UNITY_COLLECTIONS_CHECKS var groupLength = CalculateLength(); if (groupLength != componentDataArray.Length) { throw new ArgumentException($"Length of input array ({componentDataArray.Length}) does not match length of ComponentGroup ({groupLength})"); } #endif #if ENABLE_UNITY_COLLECTIONS_CHECKS var componentType = new ArchetypeChunkComponentType <T>(m_SafetyManager.GetSafetyHandle(TypeManager.GetTypeIndex <T>(), false), false, EntityDataManager->GlobalSystemVersion); #else var componentType = new ArchetypeChunkComponentType <T>(false, EntityDataManager->GlobalSystemVersion); #endif ComponentChunkIterator.CopyFromComponentDataArray(m_GroupData->MatchingArchetypes, componentDataArray, componentType, this, ref m_Filter, out jobhandle, GetDependency()); }
public static NativeArray <T> CreateComponentDataArray <T>(MatchingArchetypes *firstMatchingArchetype, Allocator allocator, ArchetypeChunkComponentType <T> type, ComponentGroup componentGroup, ref ComponentGroupFilter filter, out JobHandle jobHandle, JobHandle dependsOn) where T : struct, IComponentData { var entityCount = CalculateLength(firstMatchingArchetype, ref filter); var job = new GatherComponentDataJob <T> { ComponentData = new NativeArray <T>(entityCount, allocator), ComponentType = type }; jobHandle = job.Schedule(componentGroup, dependsOn); return(job.ComponentData); }
public static NativeArray <T> CreateComponentDataArray <T>(UnsafeMatchingArchetypePtrList matchingArchetypes, Allocator allocator, ArchetypeChunkComponentType <T> type, EntityQuery entityQuery, ref EntityQueryFilter filter, out JobHandle jobHandle, JobHandle dependsOn) where T : struct, IComponentData { var entityCount = CalculateEntityCount(matchingArchetypes, ref filter); var job = new GatherComponentDataJob <T> { ComponentData = new NativeArray <T>(entityCount, allocator), ComponentType = type }; jobHandle = job.Schedule(entityQuery, dependsOn); return(job.ComponentData); }
/// <summary> /// Creates a NativeArray containing the components of type T for the selected entities. /// </summary> /// <param name="allocator">The type of memory to allocate.</param> /// <typeparam name="T">The component type.</typeparam> /// <returns>An array containing the specified component for all the entities selected /// by the EntityQuery.</returns> /// <exception cref="InvalidOperationException">Thrown if you ask for a component that is not part of /// the group.</exception> public NativeArray <T> ToComponentDataArray <T>(Allocator allocator) where T : struct, IComponentData { #if ENABLE_UNITY_COLLECTIONS_CHECKS var componentType = new ArchetypeChunkComponentType <T>(m_SafetyManager->GetSafetyHandle(TypeManager.GetTypeIndex <T>(), true), true, EntityComponentStore->GlobalSystemVersion); #else var componentType = new ArchetypeChunkComponentType <T>(true, EntityComponentStore->GlobalSystemVersion); #endif #if ENABLE_UNITY_COLLECTIONS_CHECKS int typeIndex = TypeManager.GetTypeIndex <T>(); int indexInEntityQuery = GetIndexInEntityQuery(typeIndex); if (indexInEntityQuery == -1) { throw new InvalidOperationException($"Trying ToComponentDataArray of {TypeManager.GetType(typeIndex)} but the required component type was not declared in the EntityGroup."); } #endif JobHandle job; var res = ComponentChunkIterator.CreateComponentDataArray(m_QueryData->MatchingArchetypes, allocator, componentType, this, ref m_Filter, out job, GetDependency()); job.Complete(); return(res); }
public NativeArray <T> GetNativeArray <T>(ArchetypeChunkComponentType <T> chunkComponentType) where T : struct, IComponentData { #if ENABLE_UNITY_COLLECTIONS_CHECKS if (chunkComponentType.m_IsZeroSized) { throw new ArgumentException($"ArchetypeChunk.GetNativeArray<{typeof(T)}> cannot be called on zero-sized IComponentData"); } AtomicSafetyHandle.CheckReadAndThrow(chunkComponentType.m_Safety); #endif var archetype = m_Chunk->Archetype; var typeIndexInArchetype = ChunkDataUtility.GetIndexInTypeArray(m_Chunk->Archetype, chunkComponentType.m_TypeIndex); if (typeIndexInArchetype == -1) { var emptyResult = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <T>(null, 0, 0); #if ENABLE_UNITY_COLLECTIONS_CHECKS NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref emptyResult, chunkComponentType.m_Safety); #endif return(emptyResult); } var buffer = m_Chunk->Buffer; var length = m_Chunk->Count; var startOffset = archetype->Offsets[typeIndexInArchetype]; var result = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <T>(buffer + startOffset, length, Allocator.None); #if ENABLE_UNITY_COLLECTIONS_CHECKS NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref result, chunkComponentType.m_Safety); #endif if (!chunkComponentType.IsReadOnly) { m_Chunk->SetChangeVersion(typeIndexInArchetype, chunkComponentType.GlobalSystemVersion); } return(result); }
public bool DidChange <T>(ArchetypeChunkComponentType <T> chunkComponentType) where T : struct, IComponentData { return(ChangeVersionUtility.DidChange(GetComponentVersion(chunkComponentType), chunkComponentType.GlobalSystemVersion)); }
public unsafe bool Has <T>(ArchetypeChunkComponentType <T> chunkComponentType) where T : struct, IComponentData => (ChunkDataUtility.GetIndexInTypeArray(this.m_Chunk.Archetype, chunkComponentType.m_TypeIndex) != -1);
public bool DidChange <T>(ArchetypeChunkComponentType <T> chunkComponentType) where T : struct, IComponentData => ChangeVersionUtility.DidChange(this.GetComponentVersion <T>(chunkComponentType), chunkComponentType.GlobalSystemVersion);
public bool DidChange <T>(ArchetypeChunkComponentType <T> chunkComponentType, uint version) where T : struct, IComponentData { return(ChangeVersionUtility.DidChange(GetComponentVersion(chunkComponentType), version)); }
public bool DidAddOrChange <T>(ArchetypeChunkComponentType <T> chunkComponentType, uint version) where T : struct, IComponentData => ChangeVersionUtility.DidAddOrChange(this.GetComponentVersion <T>(chunkComponentType), version);