Custom component Inspectors

To get full control over how a component draws its presence in the Inspector, you can implement a custom PropertyDrawer script for it.

If you are unfamiliar with Property Drawers, check out the official Unity documentation for them.

To draw the component correctly, create a script and inherit from SceneComponentDrawer class. Then, override the DrawComponent() method, like this:

using SuperScenes.Editor.CustomEditors;
using UnityEditor;
using UnityEngine.UIElements;

namespace SuperScenes.Editor
{
    [CustomPropertyDrawer(typeof(EnemyData))]
    public class EnemyDataPropertyDrawer : SceneComponentDrawer
    {
        protected override VisualElement DrawComponent(SerializedProperty property)
        {
            VisualElement inspector = base.DrawComponent(property);

            inspector.Add(new HelpBox("This box has been added by a custom PropertyDrawer.",
                HelpBoxMessageType.Info));

            return inspector;
        }
    }
}

In the method, you can create a completely new editor from scratch, or you can leverage base.DrawComponent(property) to still draw the original Inspector and add elements to it (like in the example above).

To create a custom Property Drawer for Scene Components, you must use UI Toolkit.

However, you can still return an IMGUIContainer element from your override of DrawComponent() , and implement your UI in OnGUI. Check out Unity's own IMGUIContainer documentation.

Last updated