This is one of those issues where I have a solution in place that is less elegant than I’d like. Here’s my issue:
I have an editor tool that relies on being able to pick out an “invisible” object, of which there are many. There are far too many to hunt for the object in the heirarchy, but they all have different locations, though no renderers. I’ve added a sphere gizmo to the monobehaviour attached to the object so that you can actually click on it in the editor, but these objects also exist in the game, and if they’re drawing their gizmos (and even if they don’t, just the existence of the DrawGizmos function takes some time) the game is really slow.
So far I’ve just been commenting/uncommenting the OnDrawGizmos call to switch between edit and play mode, but I want to have other people on my team do this, and some of them are afraid of code. Ideally, I’d like to have a settable flag in the inspector, even better, only invoke the OnDrawGizmos function in editor mode.
The IS_EDITOR compiler directive is SO CLOSE to what I need, but it’s still true when you run the game from inside the editor, which makes it slow.
Yeah, that works in runtime, but like I mentioned I’m trying to remove the OnDrawGizmos call altogether. In other words, even if I put the DrawSphere call inside an EditorApplication.isPlaying block, the function call itself takes some time. Not much time, no, but I have tens of thousands of these objects, so it stacks up.
Have you tried measuring you OnDrawGizmos call to figure out how much time it takes against an OnDrawGizmos test method that draws the same amount of gizmos but with static/cached positions and such. Maybe there is some performance to gain there.
Ah! that’s actually given me a great idea! I don’t need to make the call from within the individual monobehaviors, I can do it in another class that would only be one call to draw all of them. Then that could be turned on/off with minimal performance hit.
That doesn’t work. The whole point is to be able to pick a specific vertex, but if I call OnDrawGizmos from another object, clicking a vert will select that object, not the vertex. Oh well.
Can’t you use raycast and find the nearest vertex from the hit point?
Maybe you should add in some billboard vertex highlighters in the scene? It might be more optimal to avoid the OnDrawGizmos call compared to having some extra mesh data in the scene.