How To Drag and Drop Multiple Elements into a PropertyField of List<Material>?

When viewing a gameObject in the inspector view that has a MeshRenderer, you’re able to lock the inspector, select multiple Materials from the project view, and drag them all into the materials slot and they will all populate the list instead of having to populate them one at a time.

I’m wondering if it’s possible to do this in a custom editor window?

I’m able to show and populate a list of materials one-at-a-time currently with the following code:

public class MultiMaterialSelectorExample : EditorWindow
{
    [SerializeField]
    List<Material> materials;

    [MenuItem("Window/UI Toolkit/MultiMaterialSelectorExample")]
    public static void ShowExample()
    {
        MultiMaterialSelectorExample wnd = GetWindow<MultiMaterialSelectorExample>();
        wnd.materials = new();
        wnd.titleContent = new GUIContent("MultiMaterialSelectorExample");
    }

    public void CreateGUI()
    {
        // Each editor window contains a root VisualElement object
        VisualElement root = rootVisualElement;

        // Import UXML
        var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/_Internal/Tools/Editor/MultiMaterialSelectorExample.uxml");
        VisualElement uxmlTree = visualTree.Instantiate();
        root.Add(uxmlTree);
        
        var  materialPropField = uxmlTree.Q(name: "materials-prop") as PropertyField;
        // Where I'm binding the property list:
        SerializedObject serializedObj = new(this);
        SerializedProperty prop = serializedObj.FindProperty("materials");
        materialPropField.BindProperty(prop);
        materialPropField.Bind(serializedObj);
    }
}

I’ve used a semi-hacky approach to avoid having to write a ListView from scratch, but is there any native functionality that would allow me to get this working with a PropertyField/ListView, or will I need to do some trickery where I hide an ObjectField over the list label and then populate the list with the dragged elements?

We are currently working on this missing feature. It should make it in 2022.2 and up.
Otherwise, I would say to look at DragUpdatedEvent and DragPerformEvent events with a combination of
DragAndDrop.objectReferences. It is basically how it will be implemented, based on the IMGUI behavior in ReorderableList.

2 Likes

This still doesn’t seem to work in 2022.2.0b2 (beta) for me. @griendeau_unity do you have any updates maybe? Thanks!

We are still waiting for the branch to land in 2023.1. The backport to 2022.2 should come not so long after that, as it is a high priority issue.