Custom Editors in Unity3D – Part 4: ‘Simple Customization’ #3 – Gizmos

The third and final piece of ‘Simple Customization’ that I’m going to talk about in this post are Gizmos.  If you’ve ever noticed the Camera or Light icons that appear in the scene view, you’ve already interacted with them!  Essentially, Gizmos are just something drawn in the Scene View (but not in the actual game) to represent an object.

There are two functions that you can use to draw Gizmos for your object, and you just add them to your MonoBehavior the same way you would Start() or Update(): OnDrawGizmos() and OnDrawGizmosSelected().  The only difference between these two is that OnDrawGizmos() is called whenever the object is active in the scene view, while OnDrawGizmosSelected() is only called when the object is active in the scene view AND currently selected.

Inside of these functions, you’ll be using the Gizmos class (from UnityEngine) to actually draw things in the scene view.  Gizmos provides a number of static functions to draw various shapes and images in the scene view, each of which generally takes a position (in world space, often transform.position) and information to define the size/shape of the object (radius for a sphere, Vector3 of width/height/depth for a cube, etc), as well as using the Gizmos.color property to determine what color to draw in (in the case of shapes).

Let’s look at some example code:


The code above is for an example of an ‘Invisible Trigger’, essentially a GameObject with no visual representation, but merely a collider of some kind, which acts on the player when they move through it.  For laying these out however, it’s useful to have some representation of their location and size in the editor, hence – Gizmos!  In OnDrawGizmos, I use DrawWireSphere and Draw WireCube to draw the bounds of an attached BoxCollider2D or CircleCollider2D (you could just as well do this with BoxCollider or SphereCollider), and in OnDrawGizmosSelected, I do the same but with DrawSphere and DrawCube (the solid versions), so that unselected triggers are outlined, while the currently selected trigger is filled in.  Each of these also uses the color property on the component to determine what color to draw the Gizmos in.  Here’s a screenshot of this code in action:


You can see that there are 3 Invisible Triggers in the above scene, 2 with box colliders, and one with a circle collider.  You can also see that the object with a circle collider is currently selected (and as a result drawn as a filled in sphere), while the other two are merely drawn as outlines.

One other thing to note in the example code provided are the Color oldColor = Gizmos.color; and Gizmos.color = oldColor; lines.  This is just good housekeeping to make sure that color changes you make don’t affect other code that may also be working with Gizmos.

If you want to try this out for yourself, you can grab the full source for InvisibleTrigger here, then just toss it on an object with a BoxCollider2D or CircleCollider2D.  I’ll eventually post a full Unity project with all the examples from this series, complete with demo scenes, but that’s not quite done yet.

This is the last post for now on ‘simple’ customization – next time we’ll start to take a look at the UnityEditor namespace and some of the tools it provides to help us make more awesome, complex custom editors.

If you have any questions, comments, or suggestions about anything in this post or other things you’d like to see covered, feel free to leave them in the comments below!

Leave a Reply

Your email address will not be published. Required fields are marked *