Search Unity

Proper way of showing a Texture2D in the inspector?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Raseru, Mar 28, 2018.

  1. Raseru

    Raseru

    Joined:
    Oct 4, 2013
    Posts:
    87
    I have Texture2D for my ScriptableObject but I want to show a big image of it on the inspector.
    I've tried it with using drawers and editors.

    With drawers, I think I have to make a new class that inherits or contains the Texture2D if I wanted to show it. This feels more ideal but if I try and pull the Texture2D from the serialized object, the image constantly flickers, disappearing.

    If I use the editor, I can't just show the image, I got to make the whole window for each field I want to show right? Seems like overkill, but at least the image isn't flickering.

    What is the best way to approach this?
    Thanks.
     
  2. Raseru

    Raseru

    Joined:
    Oct 4, 2013
    Posts:
    87
    Apparently the Texture2D flickering has been a bug for at least 5 years. One solution is to just make it a background and the problem will go away.

    See this: https://answers.unity.com/questions/377207/drawing-a-texture-in-a-custom-propertydrawer.html

    With a drawer I managed to just do it like so:

    Code (CSharp):
    1. GUIStyle style = new GUIStyle();
    2. style.normal.background = item.itemImage.sprite;
    3. position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Keyboard), label);
    4. var rectImage = new Rect(position.x - 70, 85, 64, 64);
    5. EditorGUI.LabelField(rectImage, GUIContent.none, style);
    6. EditorGUI.PropertyField(position, property.FindPropertyRelative("sprite"), GUIContent.none);