Mastering UnityEditor Handles
Updated 3 years ago
8.7 K
Learn to use builtin handles, improve them and create completely custom handles

Handles overview

Handles in unity are a very powerful tool to accelerate the development of your game and enhance your editor scripts, they are widely used by unity's default components to give fast and accurate control over different things like colliders bounds or the radius of a point light. Even the transform tools provided by the editor to move/rotate/scale objects are handles, it gives you an idea of how useful they are !
And now, with this article you'll learn to create your own handle, change the control you have over an object in the scene view and add a lot of useful debug tools to you projects. It'll git you the possibility to develop the perfect tool that fits to your needs whether it is to create an "attack pattern editor" or a "3D shape editor', we'll also see two examples of custom handles i created for this article (available here): the curve handle and the snap move tool. With that said, let's get started with some theory and docs !

How to use Handles ?

You can use handles in every unity scripts unless you have UnityEditor included. It will be easier on custom editor which inherit from Editor and so have the OnSceneGUI message where you can put all your Handles calls. But it's possible to draw to implement our own OnSceneGUI by subscribing to the SceneView.onSceneGUIDelegate (not documented) event like this:
//OnSceneGUI for EditorWindow void OnEnable() { SceneView.onSceneGUIDelegate += OnSceneGUI; } void OnDisable() { SceneView.onSceneGUIDelegate -= OnSceneGUI; } void OnSceneGUI(SceneView sv) { //Draw your handles here }
This method have another advantage: it gives you access to the current SceneView but unfortunately this class is totally undocumented but it contains really powerful functions to control the current scene view camera (LookAt, Move to, 2d mode, align view, ....) if you're interested you can check the source code of this class on the official Unity C# reference repository.
Once you are in the good context to call your handles, you just have to choose which handle you want to use in the list of Unity Handles. There are some additional settings that cannot be passed as parameter like the handle colors, or if it's lit, these parameters can be set with properties in the Handle class: Handle color, Handle lighting. Some of the Handles functions will take a CapFunction in parameter, the purpose of these functions is to manage the visual and "grab" parts of the Handle, so you can have one handle control with multiple visuals (sphere, dot, capsule, ...). By default there is a couple of Cap functions (they all ends with "HandleCap") that you can use in the Handles class.

Cap and Handles functions

Inside the Handles class, you have three group of functions (plus some utils): Draw, Cap and Handle functions.
  • About Draw function, as their name says, these functions draw something to the current Handle camera, you can use them to debug or improve your visualization tools but they can't interact with user input.
  • Cap functions are used to manage both visual and control part of the handle. They act during two event passes: Repaint and Layout, during the repaint pass the cap function draw what it have to draw and in the layout it call the AddControl function to record the distance from the mouse to the visual.
  • And finally, Handle functions provide a control over the cap function given in parameter (it generates a controlId, catch event relative to the handle and then call the cap function with it's control id). For example the FreeMoveHandle will allow to his cap function to be dragged in 3D inside the scene view.
The size parameter of HandleCap functions can be dynamic or static (like when you switch off 3D icons in the scene view). A dynamic size in comparison with a static size (which is a simple value), is calculated HandleUtility.GetHandleSize which take a 3D position and return a size based on how far is the camera from the position so your handle will appear to be the same size if you're near or far from it.

Better uses of builtin Handles

I didn't found a better way to introduce this section so it will just be a list of cool things to know when you want to use Handles :)
Some of the Draw and Cap function in the Handle class does not let you change the scale of the visual but there is a simple way to change it: DrawingScope, this function will push a matrix and a color in parameter and use them in Handles.matrix and Handles.color so you just have to put a rotation matrix in the DrawingScope and then draw every Handle you need.
In Unity, there is some Handles that are not in the Handles class but in the IMGUI.ControlsOther class, these are powerful Handles and can be easily implemented (clickable images beside).
You can write text to the current camera with Label, this function support styling so you can customize the font, color or size of your text but it does not support 3D text.
HandleUtility.Repaint can be used to Repaint the current camera if you have a dynamic Handle which move without users's event.
If you need to raycast from your mouse, HandleUtility.GUIPointToWorldRay can be used to get the current ray from the mouse position.

Creating custom Handles

Here we are, the core of the article and the certainly the most important part for you if you want to create good editor tools :) In this section i'll support my explanation with some code, i recommend you to take a look at the project i create for this article, it is fully available here: MasteringUnityHandles. It contains a simple Free2DMoveHandle, KeyframeHandle, and a CurveHandle (thumbnail of the article), moreover there is an editor window (in Window/Handles Examples tab) which list some Handles draw functions, all IMGUI handles and my custom handles.
In this section, i'll take in example the Free2DMoveHandle i created, it's a rather simple handle so i wont be too complex to be a start point. The purpose of this Handle is to allow a point to move freely on a plane in 2D and display a texture or a square facing the camera at the position of the point.

Custom controls

First, for our Handle we need a controlId. The controlId will be used to tell which Handle we are controlling so it's essential if we don't want to break everything to make good use of the controlId. We use GetControlId to generate a new controlId, it needs to be called every frame so at the beginning of our Handle function we put this:
int controlId = EditorGUIUtility.GetControlID(controlIdHash, FocusType.Keyboard);
The first parameter is a hint to help the id generation, it's a good practice to put a hash of a string containing the name of your handle in this parameter "Free2DMoveHandle".GetHashCode(). The second parameter is a FocusType which specifies if the control can catch the keyboard focus (by pressing tab for example) or not.
Now that we have our controlId let's do a switch to handle our events.
switch (Event.current.type) { case EventType.MouseDown: //check nearest control and set hotControl/keyboardControl break ; case EventType.MouseUp: //check if i'm controlled and set hotControl/keyboardControl to 0 break ; case EventType.MouseDrag: //if i'm controlled, move the point break ; case EventType.Repaint: //draw point visual break ; case EventType.Layout: //register distance from mouse to my point break ; }
On MouseDown we use HandleUtility.nearestControl to check the nearest controlId in the scene. This field was updated during the last Layout pass using the lowest value recorded in AddControl, this guarantee that only one Handle will be controller at same time. It also let you know when the mouse is near your handle which is cool for a over effect. So we check if the nearest control is our control and if we pressed the left mouse button. Then i assigned both hotControl and keyboardControl to my controlId to tell to others Handles that i have the focus of the user.
if (HandleUtility.nearestControl == controlId && e.button == 0) { GUIUtility.hotControl = controlId; GUIUtility.keyboardControl = controlId; e.Use(); }
As written in the doc, hotControl ensure that no other objects will take the control until you reset it to 0. I choose to assign keyboardControl too because i wanted the focus to persist after we release the mouse so the keyboardControl field can keep track of my controlId and i can check if my object is selected if hotControl or keyboardControl are equal to my controlId.
Then on MouseUp we have to reset hotControl if we are controlled:
if (GUIUtility.hotControl == controlId && e.button == 0) { GUIUtility.hotControl = 0; e.Use(); }
The last event to manage for controls is Layout i'll talk about the Repaint event in the next part. During this event we need to call AddControl to record the distance between the mouse and our handle. To get this distance there are some utils in the HandleUtility class to help us, here i used the DistanceToRectangle function, it just take the world position of the rectangle, it's rotation and it's size to calculate the closest distance between the mouse and this rect for us. For the rotation of the rectangle, i used the rotation of the current camera rotation so the distance will fit to the visual. To get my world position i have to take the position i got on my 2D plane (position) and multiply it by a TRS matrix that i calculate before.
Vector3 pointWorldPos = matrix.MultiplyPoint3x4(position); float distance = HandleUtility.DistanceToRectangle(pointWorldPos, Camera.current.transform.rotation, size); HandleUtility.AddControl(controlId, distance);

Custom visual

During the Repaint pass, you have to draw the visual of your handle, to do that you have multiple choices, it depends of what you want to draw. If you want a geometry that changes when the user interact with the handle you must use legacy Opengl instant mode (it's an old deprecated way to draw objects during the render loop but unity still use it inside the editor maybe because it's simpler to write ?), an example of procedural geometry with an existing Handle is the Arc handle, at runtime it generate the vertices of the arc depending on his angle and then draw it. On the other side if you you have static geometry you can simply use a mesh loaded with Resources.Load or EditorGUIUtility.Load and draw it with Graphics.DrawMeshNow. The mesh technique is obviously faster than with legacy Opengl (by almost a 10x factor) even by recreating the mesh from scratch each repaint frames (i made a little bench with 30k triangles in my project) so you can also use it for procedural geometry but it can be really boring to write procedural mesh generation with the current mesh api so i'll show you the legacy way for procedural geometry.
For our quad, we are going to use legacy Opengl to draw a quad since, even if it's slower than drawing a mesh it remains instant so we don't have to care about that. The first thing we need to do is to generate the top right corner position of our quad, this is pretty simple since our quad is facing the camera:
Vector3 worldPos = matrix.MultiplyPoint3x4(position); Vector3 camRight = Camera.current.transform.right * size; Vector3 camUp = Camera.current.transform.up * size;
We also got the world position of our handle like in the Layout pass. Then with these datas we can draw out quad:
GL.Begin(GL.QUADS); { GL.Vertex(worldPos + camRight + camUp); GL.Vertex(worldPos + camRight - camUp); GL.Vertex(worldPos - camRight - camUp); GL.Vertex(worldPos - camRight + camUp); } GL.End();
Now there is just a last step before you can see you handle: set it a material. In fact if you just run your code like this you'll see nothing because the material assigned to this geometry comes from the last object draw by the editor, so you have to assign your material before the GL.End. To do that you need a Material (it can be loaded from an asset or it can be created at runtime) once you have you handle material you need call the SetPass function to bind the material. We generally put 0 in the pass count parameter but if you're using a more complex material you must want to use another number. In my project i've created a static class that store all my materials for Handles so i can access them like this:
If you want to add a texture like me, you'll need UVs in your geometry, this can be done by adding GL.TexCoord2(uv.x, uv.y) before each GL.vertex to set uv per vertices. Then you'll have to specify your texture at your material before calling SetPass, and it's obvious but your material must have a Texture2D field in it's proterties. In some other cases you only want to draw colors and don't care about texture, in my curve Handle it's the case, to do it you have to support vertex coloring in your material which allow your shader to get the per-vertex colors specified with GL.Color(color) and then blend between them. I've written a shader that does it available here if anyone is interested.
procedural geometry / mesh / material / vertex coloring

Free2DMoveHandle Finish up

There are few things left to do to get our handle to work, indeed we didn't manage the MouseDrag event in out switch yet. During this pass, We have to cast a ray from the mouse, intersect it with our 2D plane and move the position to this new intersection. Getting the mouse ray:
Then we create a plane with our current matrix as rotation, and intersect with our ray:
float dist; Plane plane = new Plane(matrix * Vector3.forward, matrix.MultiplyPoint(; plane.Raycast(ray, out dist); if (dist > 0) position = ray.GetPoint(dist);
We also have to check if the hit distance is positive before assigning our position so the position will not move if the ray does not hit the plane.
Another last thing you have to do during the MouseDrag pass is to tell to the GUI system that something have been changed because the user interacted with your handle this is very important if you want your handle to support Undo/Redo. Here is my simple implementation of this:
if ( != GUI.changed = true;
With this little piece of code, when the mouse move dragging our handle, the GUI state is set to changed.
And here we are, it's finished ! You have a fully functional 2D move handle. Now i'll show you how to use my implementation of this handle (you can also find it in the HandlesExtended.cs file):
static Free2DMoveHandle free2DMoveHandle = new Free2DMoveHandle(); public static void Free2DMoveHandle(ref Vector2 position, float size, Quaternion rotation) { free2DMoveHandle.matrix = Matrix4x4.TRS(, rotation,; free2DMoveHandle.faceCamera = true; free2DMoveHandle.DrawHandle(ref position, size); }
First, we create an instance of our Handle, then we set the rotation of the plane, by default without rotation this plane is the XY plane so you must take this in account when you rotate the plane. Then we set faceCamera to true, i've implemented another mode in my handle which face the current plane, i thought that can be useful in some cases. And finally we call our handle draw function which will directly go into the switch we created before.

Custom Cap Handle function

Now that you know how to recreate a basic handle you can improve existing handles visuals, maybe you just want to create a strange form with existing controls (for example a Cylinder you can move around ?) then you might want to use FreeMoveHandle but with a custom Cap function that draw a cylinder. Let's give it a try !
First, looking at the CapFunction delegate in the Handles class we need our function to have the same parameters and then we need to manage Repaint and Layout events:
static void CylinderHandleCap(int controlId, Vector3 position, Quaternion rotation, float size, EventType eventType) { if (eventType == EventType.Repaint) { HandlesMaterials.vertexColor.SetPass(0); Graphics.DrawMeshNow(cylinderMesh, position, Quaternion.identity); } else if (eventType == EventType.Layout) { float cylinderHeight = 3.5f; Vector3 startPosition = position + Vector3.up * (cylinderHeight / 2); float distance = 1e20f; for (int i = 0; i < 9; i++) { distance = Mathf.Min(distance, HandleUtility.DistanceToCircle(startPosition, size / 2)); startPosition -= Vector3.up * cylinderHeight / 8; } HandleUtility.AddControl(controlId, distance); } }

During the Repaint pass, we set the material for our mesh and draw our cylinder mesh at the Handle position, unfortunately the rotation in parameter is not the rotation passed to the FreeMoveHandle, it's the current camera rotation so if you use this rotation to draw the mesh, it will always face the camera that why i used Quaternion.identity instead.
The Layout part may seem a bit strange but it's because evaluating a distance between a point and a rotated cylinder is very complex and annoying so i used multiple distances from circle and combined them with Mathf.Min to create a distance to the cylinder and it works pretty well.
And here we are, a functional cylinder custom cap, you just have to put this function in parameter of FreeMoveHandle or any other Handle functions and it will display a movable cylinder.

Undo and Redo

This is a part i wanted to talk about, undo/redo is very important when creating a new tool in Unity (or any other software) and can ruin the user experience if not/bad implemented. Unfortunately, in unity there is nearly no way to implement a custom Undo/Redo system and we have to use the unity implementation of this system. The problem with this is that the unity undo only support unity objects so our curve handle or free 2D move handle can't benefit of it (because AnimationCurve or Vector2 does not inherit from Object). So as standalone our handles can't benefit from the editor system, we have to link them to an existing object to be saved. Indeed handles never implements an undo/redo system but the script that draw the handle does and so when you change the object datas with an handle, the script detect if the handle have been changed with GUI.changed and record the object modification with Undo.RecordObject. You can see an implementation of this in the CurveScriptableObjectEditor.cs file in my project, and here is a simple implementation of this with my curveEditor:
AnimationCurve modifiedCurve = curveHandle.DrawHandle(curve); if (GUI.changed) { Undo.RecordObject(target, "Changed curve"); curve = modifiedCurve; }
Furthermore you'll notice that i assigned my curve to the modified curve after recording the object, this is very important to do that in this order, if you don't you'll get inconsistencies in the undo/redo behavior.
If you go to my project in the MasterUnityHandles/ExamplesScenes/curves folder you'll find some scriptable objects with curves within and a handle in the scene view for modify them. As you can see, the Undo/Redo works perfectly on the curve thanks to this little piece of code placed in the editor script of the scriptable objects.

Example: curve handle

To complement this article, i decided to add two different examples of custom handles implementation (wit full sources available in my project). I'll try to explain every part of code that i find interesting without being redundant with the explanations provided above. The first example we're reviewing is the Curve Handle:
The Curve Handle work with two others Handles i created for this curve Handle: the free 2D move handle and the keyframe handle. We already seen the first in detail so i'll skip to the keyframe Handle.
Before to start a bit of context: the pointHandle and tangentHandle fields are Free2DMoveHandles and are here to draw the center of the keyframe and his tangents, then the e field is Event.current coming from the CustomHandle base class. In the DrawHandle function, the first thing you can notice is that the right mouse down event call a function to display a context menu, in the condition you can also see that i check the nearestControl with my center point control id (that's why they are public) to know when my mouse is over the center point.
if (e.type == EventType.MouseDown) { //we add the context menu when right clicking on the point Handle: if (HandleUtility.nearestControl == pointHandle.controlId && e.button == 1) KeyframeContextMenu(keyframe); }
If this condition is true, then KeyframeContextMenu is called. This function is pretty simple and just create a GenericMenu, add items if there is a curve linked to the keyframe and show it (pic beside).
void KeyframeContextMenu(Keyframe keyframe) { GenericMenu menu = new GenericMenu(); if (curve != null) { int keyframeIndex = curve.keys.ToList().FindIndex(k => k.Equal(keyframe)) - 1; //add keyframes actions as MenuItems menu.ShowAsContext(); } }
The rest of the handle logic is managed in the DrawKeyframeHandle function which draw and assign the position and tangents of our keyframe. The hardest part in this function was to find how the values inside inTangent and outTangent works and how to display them. With this difficulty solved, the rest of the function is pretty straightforward: draw the tangent lines, call the handle center point, then handle for tangents and finally set keyframe values back.
That's everything for the KeyframeHandle, now let's see the CurveHandle class. In this class, nothing new except the system to add point to the curve when your mouse is near the edge of the curve, for this system i decided to not to use the standard Handle event management with AddControl but to implement my own (there is no reason for that but i found that it might be useful in some cases). So let's jump to the interesting function: DrawCurve which in addition to draw the curve, detect if the mouse is near the edge of the curve:
//draw curve GL.Begin(GL.QUADS); { for (int i = 0; i < curveSamples; i++) { float f0 = (float)i / (float)curveSamples; float f1 = (float)(i + 1) / (float)curveSamples; DrawCurveQuad(curve, f0, f1); } } GL.End();
In the first part of this function we sample the curve with curveSamples number of samples to draw a quad (inside DrawCurveQuad) from the time and values evaluated with the curve:
Vector3 topLeft = new Vector3(f0 * width, curve.Evaluate(f0) * height, 0); Vector3 topRight = new Vector3(f1 * width, curve.Evaluate(f1) * height, 0); //check if the mouse is near frmo the curve edge: float dst = HandleUtility.DistancePointToLineSegment(currentMouseWorld, topLeft, topRight); if (dst < mouseCurveEdgeDst) mouseCurveEdgeDst = dst; if (dst < mouseOverEdgeDstThreshold) mouseOverCurveEdge = true;
In this piece of code, we take the two values of the curve at time f0 and f1 and we use DistancePointToLineSegment to get the distance from the current mouse position to the top segment of the curve. Then we keep the smallest distance and if it's under a threshold we set a boolean to tell that the mouse is near the edge.
The rest of the code is pretty simple, there is a lot of legacy Opengl to draw the curve and it's edges plus some code to call other handles (like Keyframe Handles or labels). The usage of this Handle is also straightforward and have already been explained in the Undo/Redo section so i'll jump directly to the next example.

Another full example: Snap Handle tool for GameObjects

And once again, the source code of this example is available here: SnapHandleEditor.
In this example we'll see how to improve the default Move tool by adding a little cylinder on each axis which help to snap the position of the object by 0.25 if action is hold or 0.1 if action+shift.
To do that, first we need a script that run when a GameObject is selected. The first logic idea is to use a custom editor on the Transform component, from them we can draw Handles and access to the GameObject transform, but this is not what we are going to use, instead we'll use a simple script with the InitializeOnLoad attribute. This attribute will run our class in the editor without begin attached to anything and this is a real benefit because in contrast to the custom editor, we're not going to override any default inspectors (which would break the unity's custom editor for Transform).
[InitializeOnLoad] public class SnapHandleEditor { static SnapHandleEditor() { SceneView.onSceneGUIDelegate += OnSceneGUI; } static void OnSceneGUI(SceneView sv) { } }
To explain a little bit more, what does InitializeOnLoad is, when the unity context is ready, to call the static constructor of each class marked with this attribute, so inside our constructor we can access to every static unity fields like in the example onSceneGUIDelegate which allow our OnSceneGUI to be bound to the scene view. With this class as base we can now begin to code the core of our system:
if (Tools.current != Tool.Move) return ; if (Selection.activeGameObject == null) return ; transform = Selection.activeGameObject.transform;
We start with some checks: first we check if the current tool selected in the editor is the Move tool otherwise we wont display our handles, then we check if the current selected object is a GameObject and is not null, finally we assign our local transform to the selected object's transform. then i decided to create a function to draw one handle for an axis:
static void DrawAxisHandle(Quaternion rotation, Vector3 direction, Color color, int index) { var e = Event.current; float snapUnit = 0; float size = HandleUtility.GetHandleSize(transform.position) * snapHandleSize; float dist = size * snapHandleDistance; if (EditorGUI.actionKey) snapUnit = (e.shift) ? shiftSnapUnit : commandSnapUnit; currentRotation = rotation; currentColor = color; Vector3 addPos = direction * dist; Vector3 newPosition = Handles.Slider(transform.position + addPos, direction, size, SnapHandleCap, snapUnit) - addPos; transform.position = newPosition; }
This function takes a rotation for the cylinder to draw, a direction which is the axis and a color for the axis color. The first thing we do here is to calculate the size of the handle with GetHandleSize so the cylinders size is not dependent from the camera distance. Then we compute the snap value, unfortunately the Slider snapping is only active when EditorGUI.actionKey is hold so i only assign snapUnit when it is true, if we press shift and actionKey it changes the snapUnit to 0.1 (default is 0.25). The next step is to set our current rotation and color, i've used external variables to store the rotation and the color because to draw the cylinder i'm using a custom Cap function and i can't pass additional parameters to them. Then we call the Slider handle with our custom cap function and finally we set the new position of the GameObject.
For the custom cap function it looks like the one we code above:
static void SnapHandleCap(int controlId, Vector3 position, Quaternion rotation, float size, EventType eventType) { if (eventType == EventType.Repaint) { //we set the material color or preselected color if mouse is near from our handle Color color = (HandleUtility.nearestControl == controlId || GUIUtility.hotControl == controlId) ? Handles.preselectionColor : currentColor; HandlesMaterials.overlayColor.SetColor("_Color", color); //we draw the cylinder with overlay material HandlesMaterials.overlayColor.SetPass(0); Matrix4x4 trs = Matrix4x4.TRS(position, transform.rotation * currentRotation, * size); Graphics.DrawMeshNow(snapToolMesh, trs); } else if (eventType == EventType.Layout) { float distance = HandleUtility.DistanceToCircle(position, size); HandleUtility.AddControl(controlId, distance); } }
The only different thing here is the material used to draw the mesh, it's a custom material with ZTest disabled so our cylinder will always be in front of everything exactly like the move handle.
This conclude our last example, pretty short but enough to understand how to improve or add custom behaviors to the unity's default tool with handles. I hope this article will be useful for your future creations, keep creating awesome tools and see you in another post.
antoine lelievre
Procedural generation developer - Programmer
Extremely useful article! Very clean explanation, much better than an official documentation! Thank you, and keep going!
3 years ago
Nice article, was very helpful.