Hey!
So I’d like to draw a gizmo in the scene view. I want to draw a cube.
My issue is that I have an editor window, and I’d like to put some code there that draws the gizmos whenever I want it to be drawn.
I tried using ‘OnDrawGizmos’ but no luck.
I tried these two pieces of code, but the functions don’t seem to ever get called.
void OnDrawGizmosSelected() {
Gizmos.color = new Color(1, 0, 0, 0.5F);
Gizmos.DrawCube(Vector3.zero, new Vector3(1, 1, 1));
}
void OnDrawGizmos() {
Gizmos.color = new Color(1, 0, 0, 0.5F);
Gizmos.DrawCube(Vector3.zero, new Vector3(1, 1, 1));
}
Any help would be greatly appreciated. I also tried using Handles but it’s a hassle to draw a volume type of visual that isn’t a perfect cube.
Alex
I managed to get something working using this.
[DrawGizmo(GizmoType.Selected | GizmoType.NonSelected)]
static void drawGizmo2(Transform gizmoLocation, GizmoType gizmoType)
{
Gizmos.color = new Color(1, 0, 0, 0.5F);
Gizmos.DrawSphere(Vector3.zero , 1);
//Gizmos.DrawCube(rockstudio.mouseHitPoint.point + new Vector3(0,rockstudio.RockYOffset,0), new Vector3(rockstudio.EdgeWidth, rockstudio.EdgeHeight, rockstudio.EdgeDepth));
}
1 Like
Hmm it doesn’t quite work as expected. The DrawGizmo attribute is meant to be used with components. I just have an Editorwindow and nothing more. I think I might just stick to using handles.
Edit: Sorry for spamming this thread. So I’d like to draw gizmos independently of any gameobjects or components.
You’re looking for the Handles class, coupled with SceneView.onSceneGUIDelegate.
Yeah I tried handles, but they don’t have an option to draw a ‘box volume’. I’ve only seen like wire cubes, or lines
A box volume is made of lines.
Yeah but the sides are colored, and I don’t know how to do that with handles…
And also, when using handles, they stay as an overlay on the screen. So if I put the handles lower, and they go through like a plane or a 3d model or whatever, it doesn’t show.
Do your due diligence please 
Handles does have fewer convenience methods but you are by no means unable to draw whatever you wish. Not to mention all of Graphics and GL are there for you to use.
Okay. Still wondering if there is a way to do this with gizmos. Thank you for the links, I’ll make a cube that way for now.
I know it’s doable with handles, I just find gizmos to be a bit easier and was wondering if I could use them in this case.
It’s not. Gizmos are restricted specifically to a gizmo drawing context, which only exists in the context of MonoBehaviours or other scene objects.
But its also not unreasonable for someone to ask if they can be used. Unity’s documentation on advanced editor functionality is woeful, and its not really popular enough to get a lot of answers on answer sites. A lot of these type of questions get no answer or “it can’t be done” type answers even when in fact it can be done.
Not saying its the case here, but it is common in editor land.
1 Like
Yeah I thought maybe there was a sneaky workaround. But alright, that does answer my question. Thank you.