Lesson 62 +10 XP

Custom Inspectors & Editor Windows

Custom Inspectors & Editor Windows

You can extend the Unity Editor by creating custom inspectors, custom drawers, and dedicated utility windows.

Custom Inspectors

To customize how a component looks in the Inspector (e.g. adding custom buttons or conditional fields):

  1. Place your editor script in a folder named "Editor".
  2. Inherit from the Editor class.
  3. Use the [CustomEditor(typeof(TargetClass))] attribute.
  4. Override the OnInspectorGUI() method.
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(MapGenerator))]
public class MapGeneratorEditor : Editor
{
    public override void OnInspectorGUI()
    {
        // Draw the default inspector fields
        DrawDefaultInspector();

        MapGenerator generator = (MapGenerator)target;
        if (GUILayout.Button("Generate Map"))
        {
            generator.Generate();
        }
    }
}

Editor Windows

To create a standalone utility window inside the editor (like a localization tool or asset checker):

  1. Inherit from EditorWindow.
  2. Implement an OnGUI() or CreateGUI() method.
  3. Show the window using GetWindow.
public class AssetRenamer : EditorWindow
{
    [MenuItem("Tools/Asset Renamer")]
    public static void ShowWindow()
    {
        GetWindow<AssetRenamer>("Renamer");
    }

    void OnGUI()
    {
        GUILayout.Label("Asset Renamer Utility", EditorStyles.boldLabel);
        // GUI components go here
    }
}

TL;DR

  • Place editor scripting tools in a folder named 'Editor'.
  • Custom Inspectors override OnInspectorGUI to customize component fields.
  • Standalone windows inherit from EditorWindow.
  • Use [MenuItem] to add custom command entries to Unity's main top-bar menus.