- Home /
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:
But I want it to appear like in default inspector (with type written in parentheses):
Is there a way to do it with custom inspector code?
Answer by Immanuel-Scholz · Jan 14, 2015 at 01:43 PM
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.. ;)
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 :D
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;
}
Changing the name won't be a solution for me, but thank you anyway. If you can give me some other hints about rewriting EditorGUI.ObjectField that will be much appreciated :)
Yea another hint: Download JetBrain DotPeek, drag UnityEditor.dll from your Unity installation folder, Press F3 and enter "EditorGUI.ObjectField". Then copy'n'paste the stuff to your project folder and start tweaking. ;)