Inspector

We can change the appeareance of a MonoBehaviour or a ScriptableObject in the inspector by implementing our own Editor for a given type.

A custom inspector

Lets say we have the following simple behaviour.

using UnityEngine;

public class IntroPlayer: MonoBehaviour
{
    [SerializeField] private string introURL;

    void Start()
    {
        // Play the intro
    }
}

We can make a custom editor that could for example give us a button to enable or disable the intro movie, as well as making the text input field larger to make editing longer paths more comfortable.

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(IntroPlayer))]
public class IntroPlayerEditor: Editor
{
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        var property = serializedObject.FindProperty("introURL");
        EditorGUILayout.LabelField("Intro URL");
        property.stringValue = EditorGUILayout.TextField(property.stringValue, EditorStyles.helpBox);
        serializedObject.ApplyModifiedProperties();

        EditorGUILayout.Space();

        var introPlayer = target as IntroPlayer;
        var buttonText = introPlayer.enabled ? "Disable" : "Enable";
        if (GUILayout.Button(buttonText))
        {
            introPlayer.enabled = !introPlayer.enabled;
        }
    }
}

Put inspector extentions in a Editor folder to avoid problems when building.

If you only want to add additional UI widgets to the default inspector, you can draw the default UI by calling base.OnInspectorGUI().

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(IntroPlayer))]
public class IntroPlayerEditor: Editor
{
    public override void OnInspectorGUI()
    {
        base.OnIspectorGUI();
    
        var intro = target as IntroPlayer;
        var buttonText = introPlayer.enabled ? "Disable" : "Enable";
        if (GUILayout.Button(buttonText))
        {
            intro.enabled = !intro.enabled;
        }
                
        
    }
}

Constant redrawing

If your editor needs to be repainted constantly, override RequiresConstantRepaint in your editor:

public override bool RequiresConstantRepaint()
{
    return true;
}

Inspector width

Sometimes you may need to know the width of the inspector view in order to calculate the positions of your widgets. Don't use Screen.width for that. You can often find that online, but using Screen.width won't work well if Unity is running in HiDPI mode.

Instead use EditorGUIUtility.currentViewWidth