How to change custom editor - Texture2D - field look?

(From: …so messy, what’s the difference between Answers and Forums?)


I’m using this:

(Texture2D)EditorGUILayout.ObjectField(_propertyfield.Name, _value_old, typeof(Texture2D), false, _empty_guilayoutoptions);

…to create a custom editor Texture2D field.

I wonder how to make it look like Unity’s “default” Texture2D field, like a long line, not the “ugly box”.

Any idea?

Thank you.

I want it to look like the green one but I get it a box, like the red one. :frowning:

One would think, something this simple should be fairly easy but no one seems to know the answer.

For futureself and me:

EditorGUILayout.PrefixLabel("Texture: ");
texture = (Texture2D)EditorGUILayout.ObjectField(texture, typeof(Texture2D), false);

Unity’s Layout has a weird Algorithm for determining how things are drawn, Especially in Object Field LAyout Function.
First, somehow Unity can pickup when we cast Object into Texture2D, so it will draw it in your red version, A second note is to remove GUILayoutOptions from that line for code. Another thing is adding a name / GUIContent to that line will cause it to trip and show the red texture layout. Like I said it’s Weird.

The line Below will show the Green version but without a name in Unity 5.1.1f1 (just use a Layout Label instead)

 EditorGUILayout.ObjectField(dia.portrait, typeof(Texture2D), true);

So, yeah, you may find that difficult to work with, if you can try to tell Unity that your really working with an Object, then cast it into a textur2D outside of that Object Field Line. playing around with it, you’ll see how screwy it really is.

Hope it helps