Search Unity

C# Job System example

Discussion in 'Data Oriented Technology Stack' started by mathias234, Feb 21, 2018.

  1. mathias234

    mathias234

    Joined:
    Sep 9, 2012
    Posts:
    239
    I had a hard time finding some examples of the job system, but finally I am starting to understand it, Here is an example script I made. It is probably not the most efficient use of the job system but atleast it should get you started


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using Unity.Collections;
    4. using UnityEngine;
    5. using UnityEngine.Jobs;
    6.  
    7. namespace Scripts {
    8.     public struct MoveToTargetJob : IJobParallelForTransform {
    9.         public float DeltaTime;
    10.         public NativeArray<Vector3> Targets;
    11.         public float Speed;
    12.  
    13.         // Called once per element in the TransformAccessArray
    14.         public void Execute(int index, TransformAccess transform) {
    15.             transform.position = Vector3.Lerp(transform.position, Targets[index], DeltaTime / Speed);
    16.         }
    17.     }
    18.  
    19.     public class TransformJob : MonoBehaviour {
    20.         public int Count = 1000;        // How many spheres you want to spawn
    21.         public float Speed = 20;        // The speed which the spheres moves
    22.         public int SpawnRange = 50;     // The range from -SpawnRange to SpawnRange
    23.  
    24.         private Transform[] _transforms;
    25.         private Vector3[] _targets;
    26.  
    27.         public void Start() {
    28.             // Create (count) amount of spheres
    29.             _transforms = new Transform[Count];
    30.             for (int i = 0; i < Count; i++) {
    31.                 GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    32.                 obj.transform.position = new Vector3(Random.Range(-SpawnRange, SpawnRange), Random.Range(-SpawnRange, SpawnRange), Random.Range(-SpawnRange, SpawnRange));
    33.                 obj.GetComponent<MeshRenderer>().material.color = Color.red;
    34.                 _transforms[i] = obj.transform;
    35.             }
    36.  
    37.             // initialize the targets array
    38.             _targets = new Vector3[_transforms.Length];
    39.  
    40.             StartCoroutine(GenerateTargets());
    41.         }
    42.  
    43.         // Generate targets, I was not able to use Random.Range inside the job system, so generate the outside
    44.         public IEnumerator GenerateTargets() {
    45.             while (true) {
    46.  
    47.                 for (int i = 0; i < _targets.Length; i++) {
    48.                     _targets[i] = new Vector3(Random.Range(-SpawnRange, SpawnRange), Random.Range(-SpawnRange, SpawnRange), Random.Range(-SpawnRange, SpawnRange));
    49.                 }
    50.  
    51.                 yield return new WaitForSeconds(2);
    52.             }
    53.         }
    54.  
    55.         public void Update() {
    56.             // Put the spawned transforms into a TransfromAccessArray
    57.             TransformAccessArray transAccArr = new TransformAccessArray(_transforms);
    58.            
    59.             // Create a native array for _targets
    60.             NativeArray<Vector3> nativeTargets = new NativeArray<Vector3>(_targets, Allocator.Temp);
    61.  
    62.  
    63.             // Setup the job
    64.             var job = new MoveToTargetJob();
    65.             job.DeltaTime = Time.deltaTime;
    66.             job.Targets = nativeTargets;
    67.             job.Speed = Speed;
    68.  
    69.             // Schedule the job
    70.             job.Schedule(transAccArr);
    71.  
    72.             // Dispose of the arrays
    73.             transAccArr.Dispose();
    74.             nativeTargets.Dispose();
    75.         }
    76.     }
    77. }
    78.  
     
    Last edited: Feb 21, 2018
    davidfrk and NathanJSmith like this.
  2. MartinGram

    MartinGram

    Unity Technologies

    Joined:
    Feb 24, 2017
    Posts:
    40
    You can find some job system samples here.
     
  3. mathias234

    mathias234

    Joined:
    Sep 9, 2012
    Posts:
    239
    I will definitly look at those, thanks
     
  4. mkawick

    mkawick

    Joined:
    Feb 11, 2014
    Posts:
    4
    You should add a job handle and then call complete on it. Otherwise, you get a lot of errors at runtime.

    Code (CSharp):
    1. var jobHandle = job.Schedule(transAccArr);
    2.  
    3. jobHandle.Complete();
     
  5. Murray_Aurecon

    Murray_Aurecon

    Joined:
    Jun 1, 2017
    Posts:
    3
    Do you have to schedule jobs in Update? If so why?
     
  6. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    902
    You can schedule a job anywhere on the main thread (you cannot schedule a job within a job - it’s somewhere explained in the docs)
     
  7. korzen303

    korzen303

    Joined:
    Oct 2, 2012
    Posts:
    170
  8. xman7c7

    xman7c7

    Joined:
    Oct 28, 2016
    Posts:
    28
  9. starikcetin

    starikcetin

    Joined:
    Dec 7, 2017
    Posts:
    225
    mathias234, xman7c7 and Antypodish like this.
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,222
    starikcetin
    yep, these once are indeed good too. I looked a them previously.
     
    mathias234 and starikcetin like this.
  11. crescinicharmaine

    crescinicharmaine

    Joined:
    Nov 16, 2018
    Posts:
    3
    Hello. Is it possible to use job system for navmesh with finite state machine?
     
  12. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    1,564