Search Unity

Unity-Multithreaded-Job-System demo not work(Resolved)

Discussion in 'Entity Component System' started by dreamerflyer, Jan 15, 2018.

  1. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    job.png
    NO MOVING?
     

    Attached Files:

  2. Krajca

    Krajca

    Joined:
    May 6, 2014
    Posts:
    347
    You are not using new Unity.Job system, so I can't help you.
     
    MadeFromPolygons likes this.
  3. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    how to enable it?
     
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Unity Technologies will let us know during 2018.1 beta.

    Here is their statement regarding C# Job System in 2018, copied from their recent 2018.1 beta blog-post:
    https://blogs.unity3d.com/2018/01/10/get-early-access-to-unity-2018-1-the-beta-is-out/

    Once they make this officially available, they are most likely provide (more) information how to use it.
     
  5. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,980
    Job system is already available in the 2018.1 beta :)

    just use the relevant using statement!
     
  6. Krajca

    Krajca

    Joined:
    May 6, 2014
    Posts:
    347
    dreamerflyer likes this.
  7. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
  8. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,980

    I really think you should be trying basic tutorials first, the job system is an advanced threaded solution and you should probably get a grip with unity first before getting comfortable with these.

    the code posted on the docs is more than enough to work out what to do, so probably if you cant work it out from that you should try looking at:

    - Threading in .Net (threadpool, task, taskfactory etc etc)
    - Asynchronous code
    - Compute shaders (not fully needed but the way they work on data is very similar and will get your head around how to parrellize your operations)
     
    dreamerflyer likes this.
  9. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    OK,My mistake,this example is not the c# job system....and ijob is very easy to use...
    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Collections;
    3. using Unity.Jobs;
    4.  
    5. class ApplyVelocitySample : MonoBehaviour
    6. {
    7.     struct VelocityJob : IJob
    8.     {
    9.         // Jobs declare all data that will be accessed in the job
    10.         // By declaring it as read only, multiple jobs are allowed to access the data in parallel
    11.         [ReadOnly]
    12.         public NativeArray<Vector3> velocity;
    13.  
    14.         // By default containers are assumed to be read & write
    15.         public NativeArray<Vector3> position;
    16.  
    17.         // Delta time must be copied to the job since jobs generally don't have concept of a frame.
    18.         // The main thread waits for the job on the same frame or the next frame, but the job should
    19.         // perform work in a deterministic and independent way when running on worker threads.
    20.         public float deltaTime;
    21.  
    22.         // The code actually running on the job
    23.         public void Execute()
    24.         {
    25.             // Move the positions based on delta time and velocity
    26.             for (var i = 0; i < position.Length; i++)
    27.                 position[i] = position[i] + velocity[i] * deltaTime;
    28.         }
    29.     }
    30.  
    31.     public void Update()
    32.     {
    33.         var position = new NativeArray<Vector3>(500, Allocator.Persistent);
    34.  
    35.         var velocity = new NativeArray<Vector3>(500, Allocator.Persistent);
    36.         for (var i = 0; i < velocity.Length; i++)
    37.             velocity[i] = new Vector3(0, 10, 0);
    38.  
    39.  
    40.         // Initialize the job data
    41.         var job = new VelocityJob()
    42.         {
    43.             deltaTime = Time.deltaTime,
    44.             position = position,
    45.             velocity = velocity
    46.         };
    47.  
    48.         // Schedule the job, returns the JobHandle which can be waited upon later on
    49.         JobHandle jobHandle = job.Schedule();
    50.  
    51.         // Ensure the job has completed
    52.         // It is not recommended to Complete a job immediately,
    53.         // since that gives you no actual parallelism.
    54.         // You optimally want to schedule a job early in a frame and then wait for it later in the frame.
    55.         jobHandle.Complete();
    56.  
    57.     //    Debug.Log(job.position[0]);
    58.         transform.position = job.position[0];//main thread to use worker thread data need after complet.
    59.  
    60.         // Native arrays must be disposed manually
    61.         position.Dispose();
    62.         velocity.Dispose();
    63.     }
    64. }
    65.  
     
    Krajca and MadeFromPolygons like this.
  10. korvyr

    korvyr

    Joined:
    May 20, 2018
    Posts:
    1
    I actually really like this execution of thread handling. It's quite elegant. Kudos, Unity, you've just won a new user.