I am using OnDrawGizmo to draw the trigger of an object in the scene view. I have several of these and so would like to see the bounds of the triggers without needing to select them. The problem is that using OnDrawGizmo basically hides the trigger, since it is drawn over the top of it. Therefore I would like for the gizmo to only draw when the object is NOT selected. I have tried setting the gizmo color to clear in OnDrawGizmoSelected but this does not seem to do anything. What do I need to change?
So this is totally un-tested code - but if this doesn’t give you the solution, hopefully it will at least point you in the right direction.
On line 2, put this:
if (UnityEditor.Selection.activeGameObject != this.gameObject) return;
The UnityEditor namespace has a handy GameObject array of all of the selected GameObjects. For instance, if you select ALL of the GameObjects in your hierarchy, the UnityEditor.Selection.gameObjects array will contain each one of those objects.
What the code line above is doing is ‘breaking out of’ the method if the gameObject that said script is attached to is a selected gameObject. Thus, not allowing the rest of the code to run and effectively disabling the gizmo by not letting the method get to the code that renders said gizmo.
If you have any questions about any part of this post, please feel free to comment back.
The simplest way to do this is in a custom gizmo drawer class using the DrawGizmo attribute. In your case it would look something like this:
using UnityEditor;
public class MyObjectGizmoDrawer
{
[DrawGizmo(GizmoType.NotInSelectionHierarchy)]
private static void DrawGizmo(MyObject source, GizmoType type)
{
// Do your gizmo drawing here
}
}