/IJobChunkExample.cs Secret
Last active
December 10, 2021 11:09
Что бы сразу присечь возмущенния, да, IJobChunk тут не нужен, можно обойтись и .ForEach лямдой, но на то это и пример. На практике IJobChunk используется регулярно, и для него надо буквально каждый раз писать тонну лишнего кода. И если у вас возникло возмущение, вы это и без меня прекрасно понимаете.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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