기술 자료

Unity 엔진

Unity 엔진

작업 종속성

잡 종속성에 대해 알아봅니다.
읽는 시간 2분최근 업데이트: 10일 전

종종 한 잡은 다른 잡의 결과에 따라 달라집니다. 예를 들어, 작업 A는 작업 B가 입력으로 사용하는
NativeArray
에 작성할 수 있습니다. 종속 작업을 예약할 때 이러한 종속성에 대해 잡 시스템에 전달해야 합니다. 잡 시스템은 종속된 작업이 완료될 때까지 종속된 작업을 실행하지 않습니다. 한 잡은 둘 이상의 잡에 종속될 수 있습니다.
각 잡이 이전 잡에 종속되는 잡 체인을 만들 수도 있습니다. 그러나 종속성은 잡이 실행되기 전에 잡의 종속성이 완료될 때까지 기다려야 하므로 잡 실행이 지연됩니다. 종속된 잡을 완료하려면 먼저 종속된 잡과 해당 잡이 종속된 잡을 완료해야 합니다.
작업의
Schedule
메서드를 호출하면
JobHandle
가 반환됩니다. 다른 잡에 대한 종속성으로
JobHandle
를 사용할 수 있습니다. 잡이 다른 잡의 결과에 의존하는 경우 다음과 같이 첫 번째 잡의
JobHandle
를 두 번째 잡의
Schedule
메서드에 파라미터로 전달할 수 있습니다.
JobHandle firstJobHandle = firstJob.Schedule();secondJob.Schedule(firstJobHandle);

종속성 결합

잡에 종속성이 많은 경우 메서드
JobHandle.CombineDependencies
를 사용하여 병합할 수 있습니다.
CombineDependencies
를 사용하면 종속성을
Schedule
메서드로 전달할 수 있습니다.
NativeArray<JobHandle> handles = new NativeArray<JobHandle>(numJobs, Allocator.TempJob);여러 일자리에서 `JobHandles`가 있는 // Populate `handles`...JobHandle jh = JobHandle.CombineDependencies(handles);

여러 잡과 종속성 예시

다음은 여러 종속성이 있는 여러 잡의 예제입니다. 작업 코드(
MyJob
및
AddOneJob
)를 별도의 파일에
Update
및
LateUpdate
코드에 넣는 것이 가장 좋지만, 명확성을 위해 다음 예제는 파일 하나입니다.
using UnityEngine;using Unity.Collections;using Unity.Jobs;public class MyDependentJob : MonoBehaviour{ // Create a native array of a single float to store the result. 이 예시는 작업이 완료될 때까지 대기합니다. NativeArray<float> 결과; // 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. 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(); }}

추가 리소스