Custom inspector
- Game Development course
- Expert
- Lesson 34/47
- 4 min
Learn the Editor class to write a custom Inspector.
The CustomEditor Attribute and Editor Class
To customize how a component looks in the Inspector panel, author a class inheriting from Editor and decorated with [CustomEditor(typeof(TargetComponent))]. This enables highlighting fields, adding custom buttons, or organizing workflows.
[CustomEditor(typeof(Character))]
public class CharacterEditor : Editor
{
// Custom inspector for the Character component
}Rendering the Inspector via OnInspectorGUI
The custom Inspector interface is rendered inside the OnInspectorGUI() method. To retain default serialized fields while appending custom buttons or banners below them, call DrawDefaultInspector().
public override void OnInspectorGUI()
{
DrawDefaultInspector();
if (GUILayout.Button("Heal")) { /* Action */ }
}Practice Trigger Inspector Button Action
If butonBasildi is true, print Debug.Log("Inspector butonu tiklandi");.
Starter code
using System;
public static class Debug
{
public static void Log(object message) => Console.WriteLine(message);
}
public class Program
{
public static void OnInspectorGUI(bool butonBasildi)
{
// Buton basildiysa mesaj yazdir
}
public static void Main()
{
OnInspectorGUI(true);
}
}Sample solution
using System;
public static class Debug
{
public static void Log(object message) => Console.WriteLine(message);
}
public class Program
{
public static void OnInspectorGUI(bool butonBasildi)
{
if (butonBasildi)
{
Debug.Log("Inspector butonu tiklandi");
}
}
public static void Main()
{
OnInspectorGUI(true);
}
}Expected output
Inspector butonu tiklandi
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which attribute is placed above an editor class to indicate which component it customizes?
- [RequireComponent]
- [InspectorTool]
- [CustomEditor] Correct answer
- [AddComponentMenu]
Explanation: The [CustomEditor(typeof(Component))] attribute links the editor class to the target component.
2 Override OnInspectorGUI Method
public override void ____() {}
public override void OnInspectorGUI() {}
Explanation: To customize the Inspector interface, override the Editor class's OnInspectorGUI method.
3 Inspector Customization Terminology
| Term | Meaning |
|---|---|
CustomEditor |
Attribute linking editor to a component |
OnInspectorGUI |
Main method for Inspector drawing |
DrawDefaultInspector |
Draws component's default fields |
target |
Reference to target component edited |