Skip to content

Instantly share code, notes, and snippets.

@helporme
Last active December 10, 2021 11:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helporme/fab4eb4f549d8569b543d801a34696ba to your computer and use it in GitHub Desktop.
Save helporme/fab4eb4f549d8569b543d801a34696ba to your computer and use it in GitHub Desktop.
Что бы сразу присечь возмущенния, да, IJobChunk тут не нужен, можно обойтись и .ForEach лямдой, но на то это и пример. На практике IJobChunk используется регулярно, и для него надо буквально каждый раз писать тонну лишнего кода. И если у вас возникло возмущение, вы это и без меня прекрасно понимаете.
public class ExampleSystem : SystemBase
{
private EntityQuery _query;
public void OnCreate()
{
_query = GetEntityQuery(typeof(A), typeof(B), typeof(C))
}
public void Update()
{
var job = ExampleJob {
AHandle = GetComponentHandle<A>(readonly: true),
BHandle = GetComponentHandle<B>(readonly: true),
CHandle = GetComponentHandle<C>(readonly: true),
}
job.Schedule(_query)
}
[BurstCompile]
public struct ExampleJob : IJobChunk
{
// К примеру, для работы нам надо 3 компонента
[ReadOnly] public ComponentTypeHandle<A> AHandle;
[ReadOnly] public ComponentTypeHandle<B> BHandle;
[ReadOnly] public ComponentTypeHandle<A> CHandle;
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
{
NativeArray<A> aComponents = chunk.GetNativeArray(AHandle);
NativeArray<B> bComponents = chunk.GetNativeArray(BHandle);
NativeArray<C> cComponents = chunk.GetNativeArray(CHandle);
for (int i = 0; i < chunk.Count; i++)
{
A aComponent = aComponents[i];
B bComponent = bComponents[i];
C cComponent = cComponents[i];
// Готово, теперь тут можно писать код
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment