Skip to content

Instantly share code, notes, and snippets.

@jeffvella
Created August 21, 2020 23:52
Show Gist options
  • Save jeffvella/16033b6182ee7758ccf609b67f3f6ea0 to your computer and use it in GitHub Desktop.
Save jeffvella/16033b6182ee7758ccf609b67f3f6ea0 to your computer and use it in GitHub Desktop.
DynamicBufferTestSystem
public class DynamicBufferTestSystem : SystemBase
{
private EntityQuery _query;
private Entity _entity;
protected override void OnCreate()
{
_query = GetEntityQuery(ComponentType.ReadWrite<MyBufferData>());
_entity = EntityManager.CreateEntity(ComponentType.ReadWrite<MyBufferData>());
EntityManager.GetBuffer<MyBufferData>(_entity).Add(default);
}
protected override void OnUpdate()
{
Debug.Log($"{EntityManager.GetBuffer<MyBufferData>(_entity)[0].Value}");
Dependency = new ChunkJob
{
MyBufferDataType = GetArchetypeChunkBufferType<MyBufferData>(),
}.Schedule(_query);
}
public struct MyBufferData : IBufferElementData
{
public int Value;
}
public struct ChunkJob : IJobChunk
{
public ArchetypeChunkBufferType<MyBufferData> MyBufferDataType;
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
{
var bufferAccessor = chunk.GetBufferAccessor(MyBufferDataType);
for (int i = 0; i < bufferAccessor.Length; i++)
{
var buffer = bufferAccessor[i];
for (int j = 0; j < buffer.Length; j++)
{
var element = buffer[j];
element.Value += 1;
buffer[j] = element;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment