Just looking for a simple EditorGUI.DropdownButton with 3 option printing something different depending on what was selected.
You can just use
EditorGUILayout.Popup(string label, int selectedIndex, string[ ] displayedOptions);
or
EditorGUI.Popup(Rect position, int selectedIndex, GUIContent[ ] displayedOptions, GUIStyle style = EditorStyles.popup);
//Example
int _selected = 0;
string[] _options = new string[3] { "Item1", "Item2", "Item3" };
public override void OnInspectorGUI()
{
EditorGUI.BeginChangeCheck();
this._selected = EditorGUILayout.Popup("My Simple Dropdown", _selected, _options);
if (EditorGUI.EndChangeCheck())
{
Debug.Log(_options[_selected]);
}
}
For more info:
Perfect thank you
Was wondering the same thing, but found out how to use it, so leaving it here in case someone need that in the future:
public static void DrawDropdown(Rect position, GUIContent label)
{
if (!EditorGUI.DropdownButton(position, label, FocusType.Passive))
{
return;
}
void handleItemClicked(object parameter)
{
Debug.Log(paramater);
}
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent("Item 1"), false, handleItemClicked, "Item 1");
menu.AddItem(new GUIContent("Item 2"), false, handleItemClicked, "Item 2");
menu.AddItem(new GUIContent("Item 3"), false, handleItemClicked, "Item 3");
menu.DropDown(position);
}
Cross posting for google results. The EditorGUI.Popup is what one wants for situations like this. I have a selection from my database that works in a propertyDrawer:
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(ArmorType))]
public class ArmorTypeDrawerUGUI : PropertyDrawer
{
int armorTypeIndex = 0;
SerializedProperty targetProperty;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
DamageTable.Init();
var armorTypes = DamageTable.ArmorNames();
targetProperty = property.FindPropertyRelative("reference");
armorTypeIndex = targetProperty.intValue;
armorTypeIndex = EditorGUI.Popup(position, armorTypeIndex, armorTypes);
targetProperty.intValue = armorTypeIndex;
}
}
Posting for google results, as this killed an afternoon for me. EditorGUI.popup is extremely limited as it has no events and only triggers on a change. This makes it completely useless if you’re programatically changing the menu. I’d suggest any fellow newbs out there get to know how to use the dropdown menu’s as brunocoimbra suggested.
Very similar to @brunocoimbra 's solution above, tho I used menu.ShowAsContextI();
to avoid having to pass in the position Rect:
...
var enumValues = Enum.GetValues(typeof(OptionTypes));
menu = new GenericMenu();
menu.AddItem(new GUIContent($"{enumValues}"), false, AddNewDropdownCallback, enumValues);
...
if (EditorGUILayout.DropdownButton(new GUIContent("Add New..."), FocusType.Keyboard))
{
menu.ShowAsContext();
}
This is how it looks like with an Enum :
public void OnGUI()
{
GenericMenu menu = new GenericMenu();
var enumValues = Enum.GetValues(typeof(CellLayout));
foreach (var value in enumValues)
{
Debug.Log(value);
menu.AddItem(new GUIContent($"{value}"), false, handleItemClicked, value);
}
if (EditorGUILayout.DropdownButton(new GUIContent("Add New..."), FocusType.Keyboard))
{
menu.ShowAsContext();
}
}
private void handleItemClicked(object userData)
{
}