How do I make fields in the inspector go bold when prefab value is overridden

How do I get custom inspectors to bold their fields when the target has an overridden value from its prefab.

This is the standard functionality of the default inspector and I want to replicate this for my own inspectors. Is there an easy way to do this?

This information can only be retrived from a SerializedProperty. The default inspector uses a PropertyField for every field. Inside PropertyField you can find this code which sets the bold state for this property according to the “prefabOverride” value of the serialized property:

    [...]
    bool isInstantiatedPrefab = property.isInstantiatedPrefab;
    if (isInstantiatedPrefab)
    {
        EditorGUIUtility.SetBoldDefaultFont(property.prefabOverride);
    }
    [...]

If you need to develop some advanced editor GUI stuff, i recommend to use ILSpy on the UnityEditor.dll (and UnityEngine.dll) to see how the GUI functions are implemented.

If you mark your object after altering it with EditorUtility.SetDirty(arg), it will be stored properly. We had the same problem in our project, causing custom components to replace their values with prefab values at random.

I don’t know yet, how to know that the value should be bold, so the change is not visible in my custom component editor, but in the debug editor, the value is written with a bold font.

Here is a little code snippet that calls EditorGUIUtility.SetBoldDefaultFont :

private MethodInfo boldFontMethodInfo = null;

private void SetBoldDefaultFont(bool value) {
	if(boldFontMethodInfo == null)
		boldFontMethodInfo = typeof(EditorGUIUtility).GetMethod("SetBoldDefaultFont", BindingFlags.Static | BindingFlags.NonPublic);
			boldFontMethodInfo.Invoke(null, new[] { value as object });
		}
}

You can easily use this to bold stuff if the prefab has changed.

//Example with a SerializedProperty property
if(property.isInstantiatedPrefab)
		SetBoldDefaultFont(property.prefabOverride);

Thanks Bunny83 for heading me into the right direction.

If someone wants to make a custom “boldable-on-override” property drawer (as I do), he can place his GUI code between BeginProperty/EndProperty pair. This way you will also get the standart “Revert Value to Prefab” menu when right-clicking.

EditorGUI.BeginProperty(position, label, property);
/// Your GUI code here...
EditorGUI.EndProperty();