Hello, I made a property drawer and used a PopupField to be able to quickly assign a property field :
Here’s my code.
public override VisualElement CreatePropertyGUI(SerializedProperty property) {
var container = new VisualElement();
LoadIngredientDatabase();
var field = new PopupField<UnityEngine.Object>(property.displayName, items, 0) {
formatListItemCallback= item=>(item==null)?"None":((IngredientData)item).displayName,
formatSelectedValueCallback = item => (item == null) ? "None" : ((IngredientData)item).displayName
};
field.BindProperty(property);
container.Add(field);
return container;
}
Result :
It’s working but ideally, I would like to add icons to the left, and search functionnality like the “Add Component” dropdown menu :
so I may need to use a custom VisualElement instead of a generic PopupField… I know there’s a variety of examples scattered through the Unity editor UI (Shader Graph, Add Component, etc…) but I need some guidance with the best approach for this kind of thing, and maybe some examples !
Thank you !
If you want something only for the Editor, why not use the same class behind the AddComponent Window: AdvancedDropdown. You could make a button, put a label inside of it, bind the label to the property. Then when you click the button, you open your own implementation of AdvancedDropdown.
It’s relatively easy to make for your use case. Like in the doc’s example you override BuildRoot to return an AdvancedDropdownItem that itsef contains an AdvancedDropDownItem for each of the items in your Popup. It’s mostly about doing the same thing that the docs example does. The AdvancedDropDownItems have an icon property you can use to assign the icons. Then you override ItemSelected to do something with the item that’s selected by the user.
If you do this and get stuck, give me a shout and maybe I can help you.
Hello! Unfortunately with PopupField, you’re stuck with the OS implementation of a dropdown menu, but we have the GenericDropdownMenu class that you can use to implement your own dropdown with images and texts. Unfortunately we don’t have an official example of that yet.
The example of what you want to do that you shared is not made with UI Toolkit, it’s IMGUI 
Hope this helps!
Hey @JuliaP_Unity and @oscarAbraham and thank your for your amazing input !
I made a custom dropdown from scratch using EditorWindow.ShowAsDropdown like seen below
public static void Open<K>(VisualElement parent, System.Action<T> callback) where K : GenericDropdown<T,C> {
var p = ScriptableObject.CreateInstance<K>();
float size = 120f;
p.callback = callback;
//Calculate Position
var rect = parent.worldBound;
var v2 = GUIUtility.GUIToScreenRect(parent.worldBound);
rect.x = v2.x;
rect.y = v2.y;
p.ShowAsDropDown(rect, new Vector2(parent.contentRect.width, size));
}
but I now have some troubles correctly registering undo changes and make it “bindable”, so I will try the GenericDropdownMenu class, thank you !
1 Like