Ejemplo n.º 1
0
    // Notes about scheduling:
    // - calling Complete() right after Schedule() will essentially block the main thread and void any benefits of using jobs
    // - it is important where you call Schedule() (S) and Complete() (C), here are some good examples:
    //    S: Awake/Start/OnEnable / C: Coroutine after some frames/seconds => spread some heavy init code over the first couple updates/frames
    //    S: Update/FixedUpdate / C: LateUpdate/OnPreRender => spread job's work over the course of the current frame
    //    S: LateUpdate / Update/FixedUpdate => spread job's work over to the next frame, ie perform work during rendering the current frame
    private void Update()
    {
        // create the native array needed to get the output value(s) from a job
        // Note: even though we only need a single value as output we still have to use a native collection!
        outputValueA = new NativeArray <int>(1, Allocator.TempJob);
        // schedule the job here, it will begin its work immediately on a background thread
        backgroundJobHandleA = new BackgroundJobWithInputOutputParams
        {
            InputValue  = JobInputValue,
            OutputValue = outputValueA
        }.Schedule();


        // create the native array needed to get the output value(s) from a job
        // Note: even though we only need a single value as output we still have to use a native collection!
        outputValueB = new NativeArray <int>(1, Allocator.TempJob);
        // this is the same as above but without struct initializer syntax - use whichever style suits you better
        var job = new BackgroundJobWithInputOutputParams();

        job.InputValue       = JobInputValue;
        job.OutputValue      = outputValueB;
        backgroundJobHandleB = job.Schedule();

        // there are now two independent jobs running in the background in parallel ...
    }
Ejemplo n.º 2
0
    // Notes about scheduling:
    // - calling Complete() right after Schedule() will essentially block the main thread and void any benefits of using jobs
    // - it is important where you call Schedule() (S) and Complete() (C), here are some good examples:
    //    S: Awake/Start/OnEnable / C: Coroutine after some frames/seconds => spread some heavy init code over the first couple updates/frames
    //    S: Update/FixedUpdate / C: LateUpdate/OnPreRender => spread job's work over the course of the current frame
    //    S: LateUpdate / Update/FixedUpdate => spread job's work over to the next frame, ie perform work during rendering the current frame
    private void Update()
    {
        jobHandles   = new NativeArray <JobHandle>(jobCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
        outputValues = new List <NativeArray <int> >();

        for (var i = 0; i < jobCount; i++)
        {
            // create the native array needed to get the output value(s) from a job
            // Note: even though we only need a single value as output we still have to use a native collection!
            var outputValue = new NativeArray <int>(1, Allocator.TempJob);
            outputValues.Add(outputValue);

            jobHandles[i] = new BackgroundJobWithInputOutputParams
            {
                InputValue  = JobInputValue,
                OutputValue = outputValue
            }.Schedule();
        }

        // make sure the jobs are going to start now
        JobHandle.ScheduleBatchedJobs();

        // there are now several independent jobs running in the background in parallel ...
    }