Search code examples
c#unity-game-engineunity-editor

unity inspector editor dropdown menu with images


I want make dropdown menu to choose image from given. The problem is that image doesn't show in dropdown, only in already chosen. So I can see image only after I choose it from dropdown.

here's editor screenshot

I need every option in dropdown to have it's own little image so I can see which one I wanna edit.

here's editor code (in runeMaker.runeStorage.runes[i].preview we get simply Texture2D)

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(RuneMaker))]
public class RuneMakerEditor : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        RuneMaker runeMaker = (RuneMaker)target;

        GUIContent[] imagesToChooseFrom = new GUIContent[runeMaker.runeStorage.runes.Count];
        for (int i = 0; i < runeMaker.runeStorage.runes.Count; i++)
        {
            imagesToChooseFrom[i] = new GUIContent("rune " + i.ToString(), runeMaker.runeStorage.runes[i].preview);
        }

        runeMaker.runeToEditIndex = EditorGUILayout.Popup
        (
            label:new GUIContent("rune to edit"),
            selectedIndex:runeMaker.runeToEditIndex,
            displayedOptions:imagesToChooseFrom
        );
        
        if (GUILayout.Button("New rune")) runeMaker.NewRune();
    }
}

is it even possible to insert images to inspector dropdown?


Solution

  • Appears that it's not possible to make inspector dropdown menu show images and text altogether. So I get another approach. I make custom Editor Window, that has scrollview and bunch of images each with it's own button. Following script have to be placed inside Assets/Editor folder.

    using UnityEngine;
    using UnityEditor;
    
    public class CustomEditorWindow1 : EditorWindow
    {    
        private Vector2 scrollPosition;
    
        [MenuItem("CustomEditors/CustomEditorWindow1")]
        public static void ShowWindow()
        {
            CustomEditorWindow1 window = CustomEditorWindow1)GetWindow(typeof(CustomEditorWindow1 ), true, "CustomEditorWindowHeader");
            window.Show();
        }
    
        void OnGUI()
        {
            scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, true); 
       
            int runeIndex = 0;
            foreach (image in images)
            {
                EditorGUILayout.BeginHorizontal();
    
                GUILayout.Box(image);
    
                EditorGUILayout.BeginVertical();
                if (GUILayout.Button("Do something")) DoSometheingWithImage(image);
                if (GUILayout.Button("Do another stuff")) DoEnotherStuffWithImage(image);
                EditorGUILayout.EndVertical();
    
                EditorGUILayout.EndHorizontal();
            }
    
            GUILayout.EndScrollView();  
        }
    }