잡 종속성
Understand job dependencies.
읽는 시간 2분최근 업데이트: 한 달 전
한 잡은 다른 잡의 결과에 따라 달라지는 경우가 많습니다. 예를 들어, 잡 A는 작업 B가 입력으로 사용하는 에 작성할 수 있습니다. 종속 잡을 예약할 때 이러한 종속성을 잡 시스템에 전달해야 합니다. 잡 시스템은 종속 대상이 되는 잡이 완료될 때까지 종속된 잡을 실행하지 않습니다. 한 잡은 둘 이상의 잡에 종속될 수 있습니다.
NativeArray각 잡이 이전 잡에 종속되는 잡 체인을 만들 수도 있습니다. 그러나 잡 실행 전에 종속된 잡이 완료될 때까지 기다려야 하므로 종속성으로 인해 잡 실행이 지연될 수 있습니다. 종속된 잡을 완료하려면 먼저 종속 대상이 되는 잡과 해당 잡이 종속된 잡을 완료해야 합니다.
잡의 메서드를 호출하면 이 반환됩니다. 다른 잡에 대한 종속성으로 을 사용할 수 있습니다. 잡이 다른 잡의 결과에 종속되는 경우 다음과 같이 첫 번째 잡의 을 두 번째 잡의 메서드에 파라미터로 전달할 수 있습니다.
ScheduleJobHandleJobHandleJobHandleScheduleJobHandle firstJobHandle = firstJob.Schedule();secondJob.Schedule(firstJobHandle);
종속성 결합
잡에 종속성이 많은 경우 메서드 를 사용하여 병합할 수 있습니다. 를 사용하면 종속성을 메서드로 전달할 수 있습니다.
JobHandle.CombineDependenciesCombineDependenciesScheduleNativeArray<JobHandle> handles = new NativeArray<JobHandle>(numJobs, Allocator.TempJob);// Populate `handles` with `JobHandles` from multiple scheduled jobs...JobHandle jh = JobHandle.CombineDependencies(handles);
여러 개의 잡과 종속성 예시
다음은 여러 종속성이 있는 여러 잡의 예시입니다. 잡 코드( 및 )를 별도의 파일에서 및 코드에 넣는 것이 가장 좋지만, 명확성을 위해 다음 예시는 파일 하나만 보여 줍니다.
MyJobAddOneJobUpdateLateUpdateusing UnityEngine;using Unity.Collections;using Unity.Jobs;public class MyDependentJob : MonoBehaviour{ // Create a native array of a single float to store the result. This example waits for the job to complete. NativeArray<float> result; // Create a JobHandle to access the results JobHandle secondHandle; // Set up the first job public struct MyJob : IJob { public float a; public float b; public NativeArray<float> result; public void Execute() { result[0] = a + b; } } // Set up the second job, which adds one to a value public struct AddOneJob : IJob { public NativeArray<float> result; public void Execute() { result[0] = result[0] + 1; } } // Update is called once per frame void Update() { // Set up the job data for the first job result = new NativeArray<float>(1, Allocator.TempJob); MyJob jobData = new MyJob { a = 10, b = 10, result = result }; // Schedule the first job JobHandle firstHandle = jobData.Schedule(); // Setup the data for the second job AddOneJob incJobData = new AddOneJob { result = result }; // Schedule the second job secondHandle = incJobData.Schedule(firstHandle); } private void LateUpdate() { // Sometime later in the frame, wait for the job to complete before accessing the results. secondHandle.Complete(); // All copies of the NativeArray point to the same memory, you can access the result in "your" copy of the NativeArray // float aPlusBPlusOne = result[0]; // Free the memory allocated by the result array result.Dispose(); }}