Job system이란?

Job이란?

사용방법


using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
public class Test : MonoBehaviour
{
    public GameObject go;
    private NativeArray<ulong> result = new NativeArray<ulong>(1, Allocator.TempJob);
    private MyJob JobData = new MyJob();
    private JobHandle handle;
    private ulong r;
    private void Start()
    {
        JobData.result = result; 
        handle = JobData.Schedule();
        handle.Complete();
        r = result[0];
    }
    private void Update()
    {
        if(r != 20)
        {
            Debug.Log("wait for seconds");
        }
        if (r != 0)
        {
            Debug.Log($"jobData result : {r}");
            r = 0;
            JobData.result = result;
            handle = JobData.Schedule();
            handle.Complete();
            r = result[0];
        }
        go.transform.position += Vector3.forward * Time.deltaTime;
    }
    public struct MyJob : IJob
    {
        public NativeArray<ulong> result;
        public void Execute()
        {
            result[0] = 1;
            for(int i = 0; i < 20; i++)
            {
                result[0]++;
            }
        }
    }
}
// 실행결과로는 wait for seconds와 jobData result : ?가 번갈아 출력되고, 게임 오브젝트가 움직이는 것을 확인해 볼 수 있다.