Unity3D :作业依赖关系

推荐:将NSDT场景编辑器加入你的3D工具链
3D工具集:NSDT简石数字孪生

作业依赖关系

通常,一个作业依赖于另一个作业的结果。例如,作业 A 可能会写入作业 B 用作输入的 A。在计划依赖作业时,必须告知作业系统此类依赖项。作业系统在它所依赖的作业完成之前不会运行依赖作业。一个作业可以依赖于多个作业。NativeArray

您还可以有一个作业链,其中每个作业都依赖于前一个作业。但是,依赖项会延迟作业执行,因为您必须等待作业的任何依赖项完成才能运行。完成依赖作业必须首先完成它所依赖的任何作业,以及这些作业所依赖的任何作业。

调用作业的 Schedule 方法时,它将返回 JobHandle。可以使用 作为其他作业的依赖项。如果一个作业依赖于另一个作业的结果,则可以将第一个作业作为参数传递给第二个作业的方法,如下所示:JobHandleJobHandleSchedule

JobHandle firstJobHandle = firstJob.Schedule();
secondJob.Schedule(firstJobHandle);

组合依赖关系

如果作业有很多依赖项,则可以使用 JobHandle.CombineDependencies 方法合并它们。 允许您将依赖项传递给方法。CombineDependenciesSchedule

NativeArray<JobHandle> handles = new NativeArray<JobHandle>(numJobs, Allocator.TempJob);

// Populate `handles` with `JobHandles` from multiple scheduled jobs...

JobHandle jh = JobHandle.CombineDependencies(handles);

多个作业和依赖项的示例

下面是具有多个依赖项的多个作业的示例。最佳做法是将作业代码 ( 和 ) 放在 and 代码的单独文件中,但为了清楚起见,此示例是一个文件:MyJobAddOneJobUpdateLateUpdate

using 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();
    }

}


3D建模学习工作室整理翻译,转载请注明出处!

上一篇:Unity3D :创建并运行作业 (mvrlink.com)

下一篇:Unity3D :并行作业 (mvrlink.com)

NSDT场景编辑器 | NSDT 数字孪生 | GLTF在线编辑器 | 3D模型在线转换 | UnrealSynth虚幻合成数据生成器 | 3D模型自动纹理化工具
2023 power by nsdt©鄂ICP备2023000829号