Gizmos and handles
- Game Development course
- Expert
- Lesson 35/47
- 4 min
Learn scene drawing with OnDrawGizmos and Handles.
Scene Visualization with OnDrawGizmos
To draw invisible trigger areas, collision bounds, or directional vectors directly in the Scene view, the OnDrawGizmos() method is used. Methods like Gizmos.DrawWireSphere or Gizmos.DrawLine render visual debugging guides.
void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, 5f);
}Interactive Scene Tools with Handles
While the Gizmos class draws static visual shapes, the UnityEditor.Handles class provides interactive handles (position arrows, sliders, rotation discs) draggable with the mouse in the Scene view. These controls are configured inside OnSceneGUI().
// Interactive draggable handle point with Handles Handles.PositionHandle(position, Quaternion.identity);
Practice Gizmo Sphere Draw Simulation
Inside the DrawGizmo(float yaricap) method, execute Debug.Log("Gizmo kure: " + yaricap);.
Starter code
using System;
public static class Debug
{
public static void Log(object message) => Console.WriteLine(message);
}
public class Program
{
public static void DrawGizmo(float yaricap)
{
// Yaricap degerini ekrana yazdir
}
public static void Main()
{
DrawGizmo(5f);
}
}Sample solution
using System;
public static class Debug
{
public static void Log(object message) => Console.WriteLine(message);
}
public class Program
{
public static void DrawGizmo(float yaricap)
{
Debug.Log("Gizmo kure: " + yaricap);
}
public static void Main()
{
DrawGizmo(5f);
}
}Expected output
Gizmo kure: 5
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which method in Unity ensures gizmos are drawn in the Scene view only when the object is actively selected?
- OnDrawGizmosSelected Correct answer
- OnRenderObject
- OnDrawGizmos
- OnSceneGUI
Explanation: The OnDrawGizmosSelected method executes only while the GameObject hosting the component is actively selected.
2 Gizmo Draw Method
void ____() {}
void OnDrawGizmos() {}
Explanation: To draw visual debugging aids in the Scene view, define the OnDrawGizmos method.
3 Gizmo and Handle Terminology
| Term | Meaning |
|---|---|
OnDrawGizmos |
Draws visual guides in Scene view |
Gizmos.color |
Color of visual guide lines to draw |
Handles |
Interactive 3D handles in Scene view |
OnSceneGUI |
Method where custom handles are drawn |