Search Unity

LateFixedUpdate?

Discussion in 'Entity Component System' started by any_user, May 11, 2018.

  1. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    I'm using jobs (this time without ECS) to do some physics calculations in FixedUpdate. In the docs it is mentioned that it's good practice to schedule jobs in Update and then complete them in LateUpdate so they have time to run. I'd like to do that in a MonoBehaviour, but with FixedUpdate. Is there something like LateFixedUpdate?
     
  2. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    I don't think it exists.
    you can:
    a) have 2 related MonoBehaviour and use script execution order to schedule them (one early, the other late)
    b) keep the job handle around, in your FixedUpdate() you .Complete() the previous frame job before scheduling the job for the current frame (you get 1 frame lag).

    remember to call JobHandle.ScheduleBatchedJobs(), else the jobs only gets scheduled when you .Complete() them
     
  3. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    b) sounds like exactly the solution I need, thanks!
     
  4. GabrieleUnity

    GabrieleUnity

    Unity Technologies

    Joined:
    Sep 4, 2012
    Posts:
    116
    @any_user What @M_R is suggesting makes total sense, and it's likely the best approach in your situation.

    That said, you should be able to implement your own
    LateFixedUpdate
    logic via the new experimental API that we exposed, that allow you to customize the player loop from managed code (https://docs.unity3d.com/2018.1/Documentation/ScriptReference/Experimental.LowLevel.PlayerLoop.html).

    Depending on your needs, you can inject a new
    PlayerLoopSystem
    at the end of the fixed update sub group, and drive your logic from there.

    The documentation of the feature is not amazing (yet) but you can look at the ECS source code to get an idea of how to use the API.
     
    senkal_ likes this.