How to display an image/logo in a custom editor?

Hi everyone,

I’m pulling my hair out with this and I’ve not got much left, so I thought I’d ask.

I’ve started experimenting and learning about custom editors - I’ve created a couple for a project I’m working on, but I’d like to be able to display a small .PNG file at the of the script’s custom editor, a logo.

I’ve hunted around found references to DrawTexturePreview etc, but I can’t get them to work. I’m loading a small PNG file from the Resources folder into a Texture2D variable and trying to draw that within OnInspectorGUI() using the aforementioned EditorGUIUtility.DrawTexturePreview, but I’m just getting errors.

Can anybody throw a short bit of code at me to demonstrate how I should be doing this please?

Thanks in advance!! :slight_smile:

2 Likes
1 Like

The “trick” here is to realise that you can use normal GUI API calls in the Editor as well. You’re not restricted to just the EditorGUI stuff.

3 Likes

After a bit of struggling I found out that the easiest to use is GUILayout.Box(image); as this handles the layout for you.

16 Likes

Note, the image here must be a Texture. You can retrieve the photo in your project via

Texture banner = (Texture)AssetDatabase.LoadAssetAtPath("Assets/(insert file path)/file.png", typeof(Texture));
GUILayout.Box(banner);
3 Likes

GUILayout.Box() is great except that it can’t resize the texture to fit the box (as far as I know). Is anyone aware of a good solution for that?

Use this overload Parameter (GUILayout.MaxHeight(256),GUILayout.MaxWidth(256))

GUILayout.Box(Resources.Load<Texture>("template"),GUILayout.MaxHeight(256),GUILayout.MaxWidth(256));
1 Like

Tried that with several different paths trying to get it to see it. Nope.

Just use this.

Texture banner = (Texture)AssetDatabase.LoadAssetAtPath(“Assets/Resources/banner.png”, typeof(Texture));
GUILayout.Box(banner, GUILayout.Width(120), GUILayout.Height(50));

Of course experimenting with a banner image already there and any sizes but works perfect.

The box did show as expected just no image.

I was trying to do it in a method attached to a selection grid method inside a GUI Method and was really shocked to find my answer so fast so thanks. Still got it to work. I am assuming your path was supposed to be just inside the resources folder. Mine was also and I tried several different ways to make the path work to no avail even using the actual path shown which works in my example.

2 Likes

You have to choose between size of image and parameters of box. What size does the box need to be? Make the image that size. You can play with the height and width of the box some to make it appear correctly with the image. Just make sure the image size is real close or exactly as needed. I recently dealt with that and as long as the size of the box is not more than a few pixels off from image size, it can be done.

In case someone need a better solution, when the image fits the size of the inspector window:

Texture2D cover = Resources.Load<Texture2D>("YourImageInResourcesFolder.png");
float imageWidth = EditorGUIUtility.currentViewWidth - 40;
float imageHeight = imageWidth * cover.height / cover.width;
Rect rect = GUILayoutUtility.GetRect(imageWidth, imageHeight);
GUI.DrawTexture(rect, cover, ScaleMode.ScaleToFit);
1 Like