Search Unity

Code to run immediately before/after screen vertical blank?

Discussion in 'Editor & General Support' started by marcusrwatson, Sep 25, 2016.

  1. marcusrwatson

    marcusrwatson

    Joined:
    Sep 24, 2015
    Posts:
    10
    Hi all,

    I'm not sure this is easy/possible to do from within Unity, but what I am looking for is the ability to have a certain line of code run immediately upon the onset (or offset, doesn't really matter) of the monitor's vertical blank period.

    I'm trying to use Unity for a neuroscience experiment, and we require absolutely rock-solid timing of the events on screen. So I need to send an event marker out to our experimental equipment that indicates exactly when a stimulus first appears on screen.

    The timing issue here is really important. Jitter on the order of a millisecond or two is unfortunately unacceptable for our purposes. If there is any delay between the vertical blank onset and the message being sent, this might be ok, provided that the delay is the same on each frame.

    So far as I can see, the best options are WaitForEndOfFrame() or something like the code discussed in the first two responses here http://answers.unity3d.com/questions/614343/how-to-implement-preupdate-function.html. But it's hard to determine exactly how solid the timing is with these options.

    I suppose the problem, from my perspective, with a "preupdate" function as described in the link is that the physics processing will be run before update, if I understand this diagram correctly: https://docs.unity3d.com/Manual/ExecutionOrder.html. And potentially the problem with WaitForEndOfFrame is that it seems to just happen after everything else has finished, rather than waiting for the vsync signal? So on frames where not much is happening, it is presumably much earlier than on frames with a lot of processing?

    I guess what I'm hoping for is that someone can tell me whether some version of one of these two options, or some other alternative I haven't considered, might be sufficient for our purposes.

    Thanks!
     
    Last edited: Sep 25, 2016