| 
 类型 
 | 
 说明 
 | 
| 
 IComponentData 
 | 
 常用组件,或者chunk components 
 | 
| 
 IBufferElementData 
 | 
 用于将dynamic buffer和entity关联 
DynamicBuffer<T> 
 | 
| 
 ISharedComponentData 
 | 
 共享组件,用于按archetype中的值来对entity分类或分组 
 | 
| 
 ISystemStateComponentData 
 | 
 用于将system-specific state与entity关联 
用于检测单个entity的创建或销毁 
 | 
| 
 ISharedSystemStateComponentData 
 | 
shared和system state的组合 | 
| 
 Blob assets 
 | 
 不算是component,但可以用来存储data。Blob assets可以被一个或多个component通过BlobAssetReference使用,并且是immutable的。 
Blob assets允许你在assets之间共享data,并在c# job中访问这个数据。 
 | 
| 
 c# blttable types 
 | 
| 
 bool 
 | 
| 
 char 
 | 
| 
 a fixed-sized character buffer 
 | 
| 
 BlobAssetReference<T>(a reference to a Blob data structure) 
 | 
| 
 fixed arrays(in an unsafe context) 
 | 
| 
 包含这些unmanaged、blitable fields的struct 
 | 
var transform = group.transform[index]; // Read transform.heading = playerInput.move; // Modify transform.position += deltaTime * playerInput.move * settings.playerMoveSpeed; group.transform[index] = transform; // Write
struct FooStateComponent : ISystemStateComponentData { } struct FooStateSharedComponent : ISystemStateSharedComponentData { public int Value; }
[InternalBufferCapacity(8)] public struct FloatBufferElement : IBufferElementData { // Actual value each buffer element will store. public float Value; // The following implicit conversions are optional, but can be convenient. public static implicit operator float(FloatBufferElement e) { return e.Value; } public static implicit operator FloatBufferElement(float e) { return new FloatBufferElement {Value = e}; } } public class DynamicBufferExample : ComponentSystem { protected override void OnUpdate() { float sum = 0; Entities.ForEach((DynamicBuffer<FloatBufferElement> buffer) => { foreach (var element in buffer.Reinterpret<float>()) { sum += element; } }); Debug.Log("Sum of all buffers: " + sum); } }
EntityManager.AddBuffer<MyBufferElement>(entity);
    Entity e = EntityManager.CreateEntity(typeof(MyBufferElement));
struct DataSpawnJob : IJobForEachWithEntity<DataToSpawn> { public EntityCommandBuffer.Concurrent CommandBuffer; public void Execute(Entity spawnEntity, int index, [ReadOnly] ref DataToSpawn data) { for (int e = 0; e < data.EntityCount; e++) { Entity newEntity = CommandBuffer.CreateEntity(index); DynamicBuffer<MyBufferElement> buffer = CommandBuffer.AddBuffer<MyBufferElement>(index, newEntity); DynamicBuffer<int> intBuffer = buffer.Reinterpret<int>(); for (int j = 0; j < data.ElementCount; j++) { intBuffer.Add(j); } } CommandBuffer.DestroyEntity(index, spawnEntity); } }
DynamicBuffer<MyBufferElement> dynamicBuffer = EntityManager.GetBuffer<MyBufferElement>(entity);
public class DynamicBufferSystem : ComponentSystem { protected override void OnUpdate() { var sum = 0; Entities.ForEach((DynamicBuffer<MyBufferElement> buffer) => { foreach (var integer in buffer.Reinterpret<int>()) { sum += integer; } }); Debug.Log("Sum of all buffers: " + sum); } }
BufferFromEntity<MyBufferElement> lookup = GetBufferFromEntity<MyBufferElement>(); var buffer = lookup[entity]; buffer.Add(17); buffer.RemoveAt(0);
public struct BuffersByEntity : IJobForEachWithEntity_EB<MyBufferElement>
public void Execute(Entity entity, int index, DynamicBuffer<MyBufferElement> buffer)
public struct BuffersInChunks : IJobChunk { //The data type and safety object public ArchetypeChunkBufferType<MyBufferElement> BufferType; //An array to hold the output, intermediate sums public NativeArray<int> sums; public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) { //A buffer accessor is a list of all the buffers in the chunk BufferAccessor<MyBufferElement> buffers = chunk.GetBufferAccessor(BufferType); for (int c = 0; c < chunk.Count; c++) { //An individual dynamic buffer for a specific entity DynamicBuffer<MyBufferElement> buffer = buffers[c]; foreach (MyBufferElement element in buffer) { sums[chunkIndex] += element.Value; } } } }
DynamicBuffer<int> intBuffer = EntityManager.GetBuffer<MyBufferElement>(entity).Reinterpret<int>();
// entity直接add EntityManager.AddChunkComponentData<ChunkComponentA>(entity);
// 使用EntityQuery EntityQueryDesc ChunksWithoutComponentADesc = new EntityQueryDesc() { None = new ComponentType[] {ComponentType.ChunkComponent<ChunkComponentA>()} }; ChunksWithoutChunkComponentA = GetEntityQuery(ChunksWithoutComponentADesc); EntityManager.AddChunkComponentData<ChunkComponentA>(ChunksWithoutChunkComponentA,ew ChunkComponentA() {Value = 4}); // 使用EntityArchetype ArchetypeWithChunkComponent = EntityManager.CreateArchetype( ComponentType.ChunkComponent(typeof(ChunkComponentA)), ComponentType.ReadWrite<GeneralPurposeComponentA>()); var entity = EntityManager.CreateEntity(ArchetypeWithChunkComponent);
// 使用components列表 ComponentType[] compTypes = {ComponentType.ChunkComponent<ChunkComponentA>(), ComponentType.ReadOnly<GeneralPurposeComponentA>()}; var entity = EntityManager.CreateEntity(compTypes);
// With the ArchetypeChunk instance var chunks = ChunksWithChunkComponentA.CreateArchetypeChunkArray(Allocator.TempJob); foreach (var chunk in chunks) { var compValue = EntityManager.GetChunkComponentData<ChunkComponentA>(chunk); //.. } chunks.Dispose(); // 直接使用entity if(EntityManager.HasChunkComponent<ChunkComponentA>(entity)) chunkComponentValue = EntityManager.GetChunkComponentData<ChunkComponentA>(entity);
// fluent query Entities.WithAll(ComponentType.ChunkComponent<ChunkComponentA>()).ForEach( (Entity entity) => { var compValue = EntityManager.GetChunkComponentData<ChunkComponentA>(entity); //... });
// With the ArchetypeChunk instance EntityManager.SetChunkComponentData<ChunkComponentA>(chunk, new ChunkComponentA(){Value = 7}); // 直接使用entity var entityChunk = EntityManager.GetChunk(entity); EntityManager.SetChunkComponentData<ChunkComponentA>(entityChunk, new ChunkComponentA(){Value = 8});
[BurstCompile] struct ChunkComponentCheckerJob : IJobChunk { public ArchetypeChunkComponentType<ChunkComponentA> ChunkComponentATypeInfo; public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) { var compValue = chunk.GetChunkComponentData(ChunkComponentATypeInfo); //... var squared = compValue.Value * compValue.Value; chunk.SetChunkComponentData(ChunkComponentATypeInfo, new ChunkComponentA(){Value= squared}); } }
ComponentType.ChunkComponent<T> ComponentType.ChunkComponentReadOnly<T> // In an EntityQueryDesc EntityQueryDesc ChunksWithChunkComponentADesc = new EntityQueryDesc() { All = new ComponentType[]{ComponentType.ChunkComponent<ChunkComponentA>()} }; // In an EntityQueryBuilder lambda function Entities.WithAll(ComponentType.ChunkComponentReadOnly<ChunkCompA>()) .ForEach((Entity ent) => { var chunkComponentA = EntityManager.GetChunkComponentData<ChunkCompA>(ent); });
原文:https://www.cnblogs.com/sifenkesi/p/12315547.html