Search Unity

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

Discussion in 'Scripting' started by nockieboy, Feb 10, 2014.

  1. nockieboy

    nockieboy

    Joined:
    Jul 7, 2012
    Posts:
    48
    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!! :)
     
    Cleo_willo876 and makaka-org like this.
  2. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    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.
     
    Psyco92, frekons_unity and CrandellWS like this.
  4. IQuick143

    IQuick143

    Joined:
    Feb 18, 2017
    Posts:
    1
    After a bit of struggling I found out that the easiest to use is
    Code (CSharp):
    1. GUILayout.Box(image);
    as this handles the layout for you.
     
  5. CraftedGaming

    CraftedGaming

    Joined:
    Jun 9, 2017
    Posts:
    37
    Note, the image here must be a Texture. You can retrieve the photo in your project via
    Code (CSharp):
    1. Texture banner = (Texture)AssetDatabase.LoadAssetAtPath("Assets/(insert file path)/file.png", typeof(Texture));
    2. GUILayout.Box(banner);
     
    BitGamey, Beetruth and reddycat like this.
  6. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    229
    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?
     
  7. umarhyatt

    umarhyatt

    Joined:
    Nov 20, 2020
    Posts:
    6
    Use this overload Parameter (GUILayout.MaxHeight(256),GUILayout.MaxWidth(256))
    Code (CSharp):
    1. GUILayout.Box(Resources.Load<Texture>("template"),GUILayout.MaxHeight(256),GUILayout.MaxWidth(256));
     
    FungusMonkey likes this.
  8. FungusMonkey

    FungusMonkey

    Joined:
    Jun 30, 2016
    Posts:
    41
    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.
     
    Last edited: Sep 14, 2022
  9. FungusMonkey

    FungusMonkey

    Joined:
    Jun 30, 2016
    Posts:
    41
    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.
     
    Last edited: Sep 15, 2022
  10. NOT_Lonely

    NOT_Lonely

    Joined:
    Feb 2, 2013
    Posts:
    538
    In case someone need a better solution, when the image fits the size of the inspector window:

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