In this module, we will explore how to create custom editors and gizmos in Unity. Custom editors allow you to extend the Unity Editor to suit your needs, while gizmos help you visualize objects and components in the Scene view.

Key Concepts

  1. Custom Editors:

    • Extending the Unity Editor to create custom inspector windows.
    • Enhancing the workflow by providing more intuitive and powerful tools.
  2. Gizmos:

    • Visualizing objects and components in the Scene view.
    • Debugging and understanding the spatial relationships of objects.

Custom Editors

Creating a Custom Editor

To create a custom editor, you need to create a new script that extends Editor. This script will override the default inspector for a specific component.

Example: Custom Editor for a Simple Component

  1. Create a Simple Component:

    using UnityEngine;
    
    public class SimpleComponent : MonoBehaviour
    {
        public float speed;
        public Color color;
    }
    
  2. Create a Custom Editor Script:

    using UnityEditor;
    using UnityEngine;
    
    [CustomEditor(typeof(SimpleComponent))]
    public class SimpleComponentEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            SimpleComponent simpleComponent = (SimpleComponent)target;
    
            simpleComponent.speed = EditorGUILayout.FloatField("Speed", simpleComponent.speed);
            simpleComponent.color = EditorGUILayout.ColorField("Color", simpleComponent.color);
    
            if (GUILayout.Button("Reset Values"))
            {
                simpleComponent.speed = 0;
                simpleComponent.color = Color.white;
            }
    
            if (GUI.changed)
            {
                EditorUtility.SetDirty(simpleComponent);
            }
        }
    }
    

Explanation

  • [CustomEditor(typeof(SimpleComponent))]: This attribute tells Unity that this editor script is for the SimpleComponent class.
  • OnInspectorGUI(): This method is overridden to define the custom inspector GUI.
  • EditorGUILayout: Provides various controls for the custom inspector (e.g., FloatField, ColorField).
  • GUILayout.Button: Creates a button in the inspector.
  • EditorUtility.SetDirty: Marks the object as dirty to ensure changes are saved.

Gizmos

Drawing Gizmos

Gizmos are drawn using the OnDrawGizmos or OnDrawGizmosSelected methods in your component script.

Example: Drawing Gizmos for a Simple Component

  1. Update the Simple Component Script:
    using UnityEngine;
    
    public class SimpleComponent : MonoBehaviour
    {
        public float speed;
        public Color color;
    
        private void OnDrawGizmos()
        {
            Gizmos.color = color;
            Gizmos.DrawSphere(transform.position, 0.5f);
        }
    }
    

Explanation

  • OnDrawGizmos(): This method is called by Unity to draw gizmos in the Scene view.
  • Gizmos.color: Sets the color of the gizmo.
  • Gizmos.DrawSphere: Draws a sphere at the specified position with the given radius.

Practical Exercise

Exercise: Create a Custom Editor and Gizmo

  1. Create a new component script named CustomComponent:

    using UnityEngine;
    
    public class CustomComponent : MonoBehaviour
    {
        public float radius = 1.0f;
        public Color gizmoColor = Color.green;
    
        private void OnDrawGizmos()
        {
            Gizmos.color = gizmoColor;
            Gizmos.DrawWireSphere(transform.position, radius);
        }
    }
    
  2. Create a custom editor script for CustomComponent:

    using UnityEditor;
    using UnityEngine;
    
    [CustomEditor(typeof(CustomComponent))]
    public class CustomComponentEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            CustomComponent customComponent = (CustomComponent)target;
    
            customComponent.radius = EditorGUILayout.FloatField("Radius", customComponent.radius);
            customComponent.gizmoColor = EditorGUILayout.ColorField("Gizmo Color", customComponent.gizmoColor);
    
            if (GUILayout.Button("Reset Values"))
            {
                customComponent.radius = 1.0f;
                customComponent.gizmoColor = Color.green;
            }
    
            if (GUI.changed)
            {
                EditorUtility.SetDirty(customComponent);
            }
        }
    }
    

Solution Explanation

  • CustomComponent: Defines a component with a radius and gizmo color, and draws a wire sphere gizmo.
  • CustomComponentEditor: Creates a custom inspector for CustomComponent with fields for radius and gizmo color, and a reset button.

Common Mistakes and Tips

  • Forgetting to Mark Dirty: Always use EditorUtility.SetDirty to ensure changes are saved.
  • Performance: Be cautious with complex gizmos as they can impact performance in the Scene view.
  • Testing: Regularly test your custom editors and gizmos to ensure they work as expected.

Conclusion

In this module, you learned how to create custom editors and gizmos in Unity. Custom editors enhance your workflow by providing tailored inspector windows, while gizmos help visualize and debug objects in the Scene view. Practice creating custom editors and gizmos to become proficient in extending the Unity Editor to suit your needs.

© Copyright 2024. All rights reserved