기본 패턴
csharp
NativeArray<RaycastCommand> commands = new(count, Allocator.TempJob);
NativeArray<RaycastHit> hits = new(count, Allocator.TempJob);
JobHandle handle = RaycastCommand.ScheduleBatch(commands, hits, 1);설명
- Unity 공식 가이드는 수많은 레이캐스트를 한 프레임에 처리할 때
RaycastCommand로 배치하라고 권장합니다. - 일반
Physics.Raycast를 수천 번 반복하면 메인 스레드 CPU 비용이 커질 수 있습니다. RaycastCommand는 C# Job System과 함께 작동해 레이캐스트를 병렬 처리하게 돕습니다.- 시야 판정, 대규모 센서, 에이전트 LOS 계산 같은 상황에서 도입 가치가 큽니다.
짧은 예제
csharp
// 개념 예시
for (int i = 0; i < count; i++)
{
commands[i] = new RaycastCommand(origins[i], directions[i], distance);
}
JobHandle handle = RaycastCommand.ScheduleBatch(commands, hits, 1);
handle.Complete();빠른 정리
| 항목 | 설명 |
|---|---|
| 대량 레이캐스트 | 메인 스레드 부담이 큰 대표 사례 |
RaycastCommand | 배치형 레이캐스트 작업 |
| Job System | 병렬 처리와 궁합이 좋음 |
| 결과 배열 | 완료 후 일괄 확인 |
| 적용 대상 | 시야 판정, 센서, 군집 AI |
주의할 점
레이캐스트 수가 적은데도 무조건 Job 기반 구조로 옮기면 코드 복잡도만 커질 수 있습니다. 실제 병목 규모가 큰 경우에 우선 적용하는 편이 좋습니다.
참고 링크
2 sources