1
\$\begingroup\$

I have a list of gizmos in a simple gizmo drawing manager. This list is updated from different parts of the codebase, usually from Update() methods. It goes something like this:

public class RenderGizmos : Singleton<RenderGizmos>
{
    private List<GizmoData> gizmos = new List<GizmoData>();        

    public void AddNewGizmo(GizmoData[] gizmos)
    {
        this.gizmos.AddRange(gizmos);
    }

    public void AddNewGizmo(GizmoData gizmo)
    {
        this.gizmos.Add(gizmo);
    }

    void OnDrawGizmos()
    {
        if (gizmos == null) return;
        for (var i = 0; i < gizmos.Count; i++)
        {
            //draw gizmos
        }
    }
}

Can you advise me on when to clear the gizmo list? Ideally I would like to clear it every frame. However:

  • Clearing it at the end of the OnDrawGizmos() method will render no gizmos (as I suspect this method might be called several times a frame).
  • Clearing it in an LateUpdate()/Update() method is dependent on execution order, sometimes it draws gizmos, sometimes it doesn't
  • Clearing it in a Coroutine has rendered no gizmos (but it might just be dependent on the execution order as well)

I would like not to be dependent on the execution order so I prefer not to use the Script Execution Order system.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Using LateUpdate instead of Update can be helpful - LateUpdate functions are run after all of the other Update functions have been called.

I use something similar to maintain a 'blacklist' of spots on the screen that shouldn't respond to clicks or other mouse-interactions.

https://docs.unity3d.com/ScriptReference/MonoBehaviour.LateUpdate.html

\$\endgroup\$
1
  • \$\begingroup\$ Thank you but that doesn't help since OnDrawGizmos() is executed after LateUpdate() and thus suffers from the same problem as regular Update() method \$\endgroup\$
    – Kraken
    Commented Jun 5, 2017 at 8:25
1
\$\begingroup\$

Figured it out. I can extend the AddNewGizmo method to check if we are in the same frame, this works fine for my case.

public void AddNewGizmo(GizmoData gizmo)
{
    ClearIfReady();
    //add
}

private void ClearIfReady()
{
    if (lastClearedFrame != Time.frameCount)
    {
        lastClearedFrame = Time.frameCount;
        gizmos.Clear();
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .