Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Resolved GUI.DrawTexture / EditorGUI.DrawPreviewTexture not working

Discussion in 'Immediate Mode GUI (IMGUI)' started by Heptagram064, Jan 3, 2023.

  1. Heptagram064

    Heptagram064

    Joined:
    Feb 22, 2022
    Posts:
    70
    Hello

    So im trying to display a simple texture inside a custom editor window.
    My needs are simple, its just a texture, and it needs to draw in OnGui().

    So i tried both;
    GUI.DrawTexture()
    and
    EditorGUI.DrawPreviewTexture()

    neither seems to work.
    I looked in the forums and other sources, and what i could find is that im doing nothing wrong, yet i keep getting either a black box, or nothing.

    Here is my code;
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System.IO;
    4.  
    5. public class MyEditorWindow : EditorWindow {
    6.     [MenuItem("Window/Custom Windows/My Editor Window")]
    7.     private static void ShowWindow() {
    8.         var window = GetWindow<MyEditorWindow>();
    9.         window.titleContent = new GUIContent("My Editor Window");
    10.         window.Show();
    11.     }
    12.     // The texture in question
    13.     private Texture2D texture;
    14.     // Load the texture in OnEnable()
    15.     private void OnEnable() {
    16.         texture = LoadImage("Assets/texture.bmp");
    17.     }
    18.     // Function to load a texture
    19.     private static Texture2D LoadImage(string path) {
    20.         // Load the referenced image into memory as bytes
    21.         byte[] ImageBytes = File.ReadAllBytes(path);
    22.         // Create a new Texture2D
    23.         var returner = new Texture2D(2, 2);
    24.         // Load the bytes into it
    25.         returner.LoadImage(ImageBytes, false);
    26.         // return the Texture2D.
    27.         return returner;
    28.     }
    29.     // Where the magic is supposed to happen
    30.     private void OnGUI() {
    31.         GUI.DrawTexture(new Rect(0, 0, 50,50), texture);
    32.         EditorGUI.DrawPreviewTexture(new Rect(0, 50, 50,50), texture);
    33.     }
    34.     // Just to make shure we are refreshing
    35.     private void OnInspectorUpdate() {
    36.         Repaint();
    37.     }
    38. }
     
  2. Heptagram064

    Heptagram064

    Joined:
    Feb 22, 2022
    Posts:
    70
    Oof, i found the problem. Apparently unity simply hates bitmaps... Swapping file format to png solved it...