Is it possible to draw inside OnDrawGizmosSelected?

I want to use ALINE drawing methods in the OnDrawGizmosSelected() but I can’t inherit from the gizmo MonoBehaviour as the documentation recommends. I am in Unity 6000.3.2f1 and it actually draws fine but the logs are filled with this error which check the stack:

Exception: You are trying to use Draw.* functions from within Unity’s OnDrawGizmos function. Use this package’s gizmo callbacks instead (see the documentation).

Can these errors be ignored and removed? If not how can I draw from inside the regular gizmo method without inheriting from the custom MonoBehaviour?

Hi

You use a different method for drawing only selected items in ALINE.

See “Selection Based Drawing” in the get started guide: Get Started With ALINE - ALINE

If you don’t want to inherit from MonoBehaviorGizmos, you can copy-paste its implementation into whatever base class you use. It will work as long as it implements the right interface, and registers itself.

Thanks, I had had a look in the manual before asking but having to use inheritance to enable this just does not sit right with me. Especially since I would only use this for editor (debugging). I could add a debug object for that (editor only tagged) but that’s also not very convenient.

For now I have reverted back to regular gizmo code but would be interested if that could be supported in a future version without inheritance.

The gizmo code is only executed in the editor, so you don’t have to worry about performance there. Not even the initialization code runs.

You can always draw during void Update() for example. That doesn’t use inheritance.

Drawing during Unity gizmo callbacks is unfortunately tricky, since Unity doesn’t call it at predictable times (especially when render pipelines are involved).

Yes, I thought there has to be a reason.

My worry is more about composition vs inheritance. If I inherit of something with many scripts then for me there has to be a very very good reason. Just drawing som debug lines isn’t enough for me. Also that object does not use Update() nor do I want to make it ExecuteInEditor just to draw some gizmos. I am a picky b***rd, I know :smiley:

Would be nice if I could just disable that error though as that really is the only thing that makes it “not work” for me. The drawing works as far as I have seen in Unity 6.3 + URP.

Here is to Unity finally merging all the RPs in Unity 7 fingers-crossed.

Sure, you can do that if you want. But be warned that it will likely cause some edge case bugs.

1 Like