I’m using EditorGUILayout.ObjectField with the filter set to Texture2D. is there a way to force it to show a standard object reference field instead of the texture preview field?
If anyone has the same problem but with EditorGUI.ObjectField, I found that if you set the height of its rect to 16 or less it will render as the standard object reference field.
It’s old post but…
Instead of writing:
sprite = (Sprite)EditorGUILayout.ObjectField("Sprite", sprite, typeof(Sprite), allowSceneObjects: true);
We just need to make it like it:
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Source Image");
sprite = (Sprite)EditorGUILayout.ObjectField(sprite, typeof(Sprite), allowSceneObjects: true);
EditorGUILayout.EndHorizontal();
And we have Field for Sprites like in Image Component.
I think code is simple enough to understand.
I hope it helped.
I found with a sprite at least if you use the overload function that does not have a label it will present as an inline object field, if i use the overload with a title (even if i set it to “”) i get the image display.
sprite = (Sprite)EditorGUILayout.ObjectField("Sprite", sprite, typeof(Sprite), allowSceneObjects: true);
Will present itself as an image view with the select button within the bottom right corner
sprite = (Sprite)EditorGUILayout.ObjectField(sprite, typeof(Sprite), allowSceneObjects: true);
This will present itself as a standard inline object field. Might not be what some are looking for as a label is usually desired but you could add a label in a EditorGUILayout.BeginHorizontal(); call or something , just something I stumbled onto, It would be more helpful if there was a parameter in the EditorGUILayout.ObjectField to display as Text or Object, but anyway there it is.
Old post, but I ran into this recently. The type parameter is a signal to ObjectField() for which type of GUI layout control to present, as well as a hint for what to use as a filter for the ObjectPicker. The fields meaning seems a bit overloaded. If you use typeof(Sprite) or typeof(Texture2D), for example, it will present the corresponding ‘advanced’ control types. If you want the usual one-line compact ObjectField control, pass typeof(Object). This does have the side-effect of generating an ObjectPicker filter for Objects, and not one more specific for Sprite/Textures. And, unfortunately, passing a filter string to ObjectField doesn’t seem possible, so if you need that functionality, yes you build out a custom line per previous comments and other web posts.
- cheers
sorry for oppening an old thread. for anyone else looking at getting a sprite to render with a label on a single line without it being so bulky you can use a Horizontal group
EditorGUILayout.BeginHorizontal();
GUILayout.Label("Override Icon");
overrideIcon.objectReferenceValue =
(Sprite)EditorGUILayout.ObjectField(overrideIcon.objectReferenceValue,
typeof(Sprite), allowSceneObjects: false);
EditorGUILayout.EndHorizontal();
@kitsee the issue that you discussed in this question has still not been solved even after 5 years in 2021. But I got a quick “hack” though. Here’s the code :-
Rect rect = EditorGUILayout.BeginVertical(GUILayout.MaxHeight(EditorGUIUtility.singleLineHeight));
sprite = (Sprite)EditorGUI.ObjectField(rect, "Sprite", sprite, typeof(Sprite), false);
EditorGUILayout.Space();
EditorGUILayout.EndVertical();
First of all, begin a group with a function called GUILayout.MaxHeight
and pass it the EditorGUIUtility.singleLineHeight
float. It will give a Rect
which you can use to create an ObjectField
. Make sure to add EditorGUILayout.Space
after the field as the next SerializedProperties
would overlap this one if not written. Also don’t forget to add the EditorGUILayout.EndVertical
line at the end to end the group.
I hope the answer helps if anyone is facing the same issue. Hope Unity fixes it soon.