How to show type text in EditorGUI.ObjectField?

I am writhing a custom inspector and I have an ObjectField (something like this):

[CustomEditor(typeof(Test))]
public class InteractiveObjectEditor : Editor {
	public override void OnInspectorGUI () {
		(target as Test).collider = (Collider2D) EditorGUILayout.ObjectField("Some Collider", (target as Test).collider, typeof(Collider2D), true);
	}
}

It appears in inspector like this:
38799-custom.png

But I want it to appear like in default inspector (with type written in parentheses):
38801-default.png

Is there a way to do it with custom inspector code?

Not without rewriting the whole ObjectField logic by yourself or unless you are up to serious DLL decompile/compile hacking.

The text that is displayed is hardcoded in EditorGUIUtility.ObjectContent and can not be modified by user code.

However, rewriting EditorGUI.ObjectField yield in some more benefits, e.g. you can replace the object picker dialog with something better. (That’s what I did for our project here, to support additional Filter-things like "the selected object must also have component X with property Y set to Z…)

And it might turn out easier than you think. Look into EditorStyles.objectField and EditorStyles.objectFieldThumb for some starters… :wink:


Edit: Actually… if I think about it. You could just add the " (xxx)" to the gameObject’s name before calling ObjectField. Kinda hackish but could work :smiley:

public override void OnInspectorGUI () {
    var col = (target as Test).collider;
    string name = col != null ? col.name : null;
    if (col != null)
        col.name += " (" + col.GetType().Name + ")";
    (target as Test).collider = (Collider2D) EditorGUILayout.ObjectField("Some Collider", col, typeof(Collider2D), true);
    if (col != null)
        col.name = name;
}