Drag&Drop Area
Published 5 months ago
A simple drag&drop area to get path of element(s)
Because of the lack of drag & drop area in EditorGUILayout, I created one.

You will need a array of string to stock the path of the element in your drag and drop.
private string[] elementsDropped;
If you want to use textures as visual inside your area, you will need to define them and set them one time at the start.
// To be sure the textures are loaded once only. bool elementsLoaded; // The texture to invite user to drag and drop inside; Texture2D TextureDragDrop; // The texture to indicate that the aera is not empty; Texture2D TextureNotEmpty; void OnGUI() { // Load elements once if (elementsLoaded == false) { LoadElements(); } //... } void LoadElements() { // Load your textures here by setting their paths TextureDragDrop = (Texture2D) AssetDatabase.LoadAssetAtPath("Assets/.../TextureDragDrop.png", typeof(Texture2D)); TextureNotEmpty = (Texture2D) AssetDatabase.LoadAssetAtPath("Assets/.../TextureNotEmpty.png", typeof(Texture2D)); }
You can also use simple texts like:
... or other elements.
After that you need to call this in your OnGUI()
public void DropAreaGUI() { GUILayout.Space(10); // Visual Part Rect drop_area = GUILayoutUtility.GetRect(0.0f, 100.0f, GUILayout.ExpandWidth(true)); var TextStyle = new GUIStyle(); TextStyle.normal.textColor = Color.white; if (elementsDropped == null) { //Change here for text or other. dragDropZoneTexture = TextureDragDrop; } GUI.Box(drop_area, dragDropZoneTexture); // Event Part Event evt = Event.current; switch (evt.type) { case EventType.DragUpdated: case EventType.DragPerform: if (!drop_area.Contains(evt.mousePosition)) return; DragAndDrop.visualMode = DragAndDropVisualMode.Copy; if (evt.type == EventType.DragPerform) { elementsDropped = null; DragAndDrop.AcceptDrag(); elementsDropped = new string[DragAndDrop.objectReferences.Length]; for (int i = 0; i < DragAndDrop.objectReferences.Length; i++) { elementsDropped[i] = AssetDatabase.GetAssetPath(DragAndDrop.objectReferences[i]); } // Change the dragDropZoneTexture here to update the image on the visual. Change here for text or other. dragDropZoneTexture = TextureNotEmpty; Repaint(); } break; } }

Tool Programmer and Developer - Programmer