How do I display a texture with label on top like in the “Add Terrain Texture” window?
I’ve tried various combinations of EditorGUILayout.ObjectField
in vertical and horizontal layout groups but the result is either the small texture picture (like in the material editor) or the large picker but with the label to the left rather than on top.
Here’s what I want:
Based on the answer and comments from @dan_wipf, I was able to get the look I wanted. The TextureField function below renders an individual texture field that has a label on top.
private static Texture2D TextureField(string name, Texture2D texture)
{
GUILayout.BeginVertical();
var style = new GUIStyle(GUI.skin.label);
style.alignment = TextAnchor.UpperCenter;
style.fixedWidth = 70;
GUILayout.Label(name, style);
var result = (Texture2D)EditorGUILayout.ObjectField(texture, typeof(Texture2D), false, GUILayout.Width(70), GUILayout.Height(70));
GUILayout.EndVertical();
return result;
}
To display a set of 4 textures horizontally:
EditorGUILayout.BeginHorizontal();
TextureField("Texture 1", texture1);
TextureField("Texture 2", texture2);
TextureField("Texture 3", texture3);
TextureField("Texture 4", texture4);
EditorGUILayout.EndHorizontal();
One of the problems I was running into is that I was previously using EditorGUILayout.LabelField, but that caused too much space between each of the textures. The reason for this is that EditorGUILayout.LabelField actually renders 2 labels (that’s it’s purpose in life). I didn’t realize this and what I actually needed with GUILayout.Label, which only renders 1 label.
public Texture2D backgroundImage;
OnGUI()
{
backgroundImage = (Texture2D) EditorGUILayout.ObjectField("Image", backgroundImage, typeof (Texture2D), false);
}
maybe this works?