I have an editor-only static debugging class (not a custom editor) with methods that draw things like meshes and handles in the scene view. I cache the draw calls in a queue, and then draw everything once during the UnityGUI repaint event, emptying the queue. If these methods are called by MonoBehaviour.FixedUpdate, there is often a dead zone where things are not drawn, as FixedUpdate can occur less frequently than Update, which results in a blinking effect because the queue will be empty for a few render frames. To prevent this, I switched to a list, and clear out the existing calls only when new calls are made.
This gets me 99% of the way towards my desired result, but I would still like to clear the list if no calls were made during the most recent FixedUpdate, though I can’t figure out how to do this. If I knew when FixedUpdate began, I could clear out the current list and await new calls.
One thing I considered is setting a short timer that will automatically clear out old calls if no new calls have been made within a certain amount of time, but that feels clunky. I could also make a dummy MonoBehaviour with a static event telling me when FixedUpdate begins (and lowering the script execution order), but that is not ideal either. Or perhaps there is a better way to do all of this.
Basically, I am trying to mimic the behavior of OnDrawGizmos or Unity’s Debug.Draw methods (which seem to persist through FixedUpdate), but with my own debugging tools.
Record a timestamp for every thing that you want to draw only for a short time.
Say you have this:
MyDebug.DrawRay(ray, duration: 0.05f)
Internally, this adds a ray to draw to the list as well as the Time this debug draw state expires:
public struct DebugRay
{
public Ray ray;
public float timeout;
}
You could set the default duration to Time.fixedDeltaTime, this would ensure the drawing is repeated until the next FixedUpdate.
And then every time you draw, either before or after drawing is performed, you go through the list of draw commands and check if their timeout has expired and if so, you remove that element. Or if many expire all the time, it may be more efficient to create a new list with just the ones that haven’t yet expired.
You may also want to record where the debug draw is coming from to not draw the ray from the same source multiple times, unless you like to see a change over time (will look like a ghost or motion blur effect if duration is > fixedDeltaTime).