Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Job Schedule in LateUpdate: weird behavior (2021.2a.21)

Discussion in 'Entity Component System' started by runner78, Jun 17, 2021.

  1. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    760
    I have a MonoBehavior with 2 jobs for mesh generation, one for creating the mesh and one for baking the mesh collider.
    I use the script for a stress test and create a new mesh as soon as the previous one has been completed.
    The chronological order:

    Update
    - Complete bake job from the previous frame
    - Schedule mesh generator job when bake mesh is done
    LateUpdate
    - Complete mesh generator
    - Schedule bake mesh

    At first it looks as if it would work fine, but I saw in the Profiler that it always skips a frame, even if both jobs should be finished within one frame.

    I noticed that the bake job only starts in the next frame after I call handle.isCompeted.

    That’s the result

    Frame 1
    - Schedule mesh generation
    - Complete mesh generation
    - Schedule BakeMesh

    Frame 2
    - Bake job started, skips the rest

    Frame 3
    - Complete bake
    - ... repeat from frame 1

    I can force the job start if i check handle.Is completed right after scheduling the job.

    Is this by design or a bug?
     
  2. elcionap

    elcionap

    Joined:
    Jan 11, 2016
    Posts:
    138
    By desgin.
    Scheduling a job doesn't automatically start itself but put them on a queue.
    You need to call JobHandle.ScheduleBatchedJobs for that, which is expensive and the reason why it's not automatic. So if you have a lot of jobs, it's better calling JobHandle.ScheduleBatchedJobs only after them all have been scheduled.

    Calling Complete will start the job if wasn't started already so that's why calling IsComplete (IIRC does the same) start the job.

    In ECS, the dependency system takes care of that for you so that's why you don't see that being talked a lot.

    []'s
     
  3. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    760
    I've tried something and have seen that this behavior is shown, if I only have a single MonoBehavior with jobs, as soon as I clone it, then it seems to start the jobs randomly.
    I also tried to start a single job and always complete it after 4 frames. The job is always executed at the time of the LateUpdate cycle. This then leads to the fact that if I am schedule in LateUpdate, then the job will be executing during the LateUpdate cycle in the next frame.