Loading lessons...
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):
- Place your editor script in a folder named "Editor".
- Inherit from the
Editorclass. - Use the
[CustomEditor(typeof(TargetClass))]attribute. - 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):
- Inherit from
EditorWindow. - Implement an
OnGUI()orCreateGUI()method. - 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
OnInspectorGUIto customize component fields. - Standalone windows inherit from
EditorWindow. - Use
[MenuItem]to add custom command entries to Unity's main top-bar menus.