How to draw a gizmo only when an object is NOT selected?
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?
void OnDrawGizmos() {
Gizmos.color = Color.blue;
Gizmos.DrawWireCube(transform.position, new Vector3(transform.lossyScale.x, transform.lossyScale.y, transform.lossyScale.z));
}
void OnDrawGizmosSelected() {
Gizmos.color = Color.clear;
}
Answer by Addyarb · Jun 22, 2016 at 04:44 AM
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.
Resource:
That didn't work. I have tried using Selection.activeGameObject but that didn't work either.
Sorry for the confusion, I forgot to add 'gameObject' into the mix.
The revised code looks like this. Tested this time!
Good luck.
if (UnityEditor.Selection.activeGameObject != gameObject) return;
Gizmos.DrawWireCube(transform.position, new Vector3(transform.lossyScale.x, transform.lossyScale.y, transform.lossyScale.z));
Thanks for that, but I think that you meant "==" since "!=" draws the gizmo for only the selected gameobject whereas "==" draws only the non selected ones.
Answer by TSWessel · Feb 14, 2020 at 12:25 PM
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
}
}
Answer by jonc_io · Oct 30, 2018 at 06:08 PM
For me I had an editor-only bool, "isSelected", and toggled that on/off to change behaviour. Like so:
#if UNITY_EDITOR
private bool isSelected;
void OnDrawGizmos()
{
// Draw always-visible gizmos here
if (isSelected)
{
isSelected = false; // Will change back to true if still selected
return;
}
// Draw unselected-only gizmos here
}
void OnDrawGizmosSelected()
{
isSelected=true;
// Draw selected gizmos
}
#endif