void ExtendedExample()
    {
        NativeArray <float> result = new NativeArray <float>(1, Allocator.Persistent);

        SimpleJob job = new SimpleJob()
        {
            result = result
        };

        JobExecution execution = JobHelper.AddScheduledJob(job, job.Schedule(), (jobExecutor) => {
            /// OnJobComplete delegate returns 'jobExecutor' with usefull data.
            if (jobExecutor.FramesTaken == -1)
            {
                Debug.Log("Job has completed immediatelly!");
            }
            else
            {
                Debug.LogFormat("Job has completed in {0}s and {1} frames!", jobExecutor.Duration, jobExecutor.FramesTaken);
            }

            // Result is available. LateUpdate() context.
            Debug.Log(result[0]);

            // You can dispose NativeContainers container within the jobexecution immediately after completion.
            // This is required if your set your container allocator as Temp or TempJob, dont Dispose if you use Persistent allocation.
            // Otherwise no need to call Dispose, the JobHelper will execute Dispose() on all completed jobs on OnDestroy()
            jobExecutor.Dispose();
        },
                                                           // If you want to complete this job immediately in the LateUpdate() check this to true.
                                                           // But make sure to schedule the job before, to leave some room for workers to do its work.
                                                           completeImmediatelly: false);

        // There is also an option to complete the job whenever you want.
        Debug.Log(execution.Complete());
    }
Esempio n. 2
0
    void LateUpdate()
    {
        for (int i = this._scheduledJobs.Count - 1; i >= 0; i--)
        {
            JobExecution execution = this._scheduledJobs[i];
            if (execution.Handle.IsCompleted && execution.JobWorking || execution.CompleteInLateUpdate)
            {
                execution.Complete();

                if (DebugLog)
                {
                    Debug.LogFormat("Job {0} has been completed. Removing it from scheduled jobs.", execution.JobTag);
                }

                this._completedJobs.Add(execution);
                this._scheduledJobs.RemoveAt(i);
            }
        }
    }