/// <summary> /// Draws a Drag and Drop Area and allows you to send in a method which receives an array of the objects that were dragged into the area. /// <para></para> /// The caller method needs to receive a generic type "T" and then cast it to its desired type itself OR use a Lambda Expression for handling the OnDragged-event. /// <para></para> /// Example implementation in OnGUI: '<see cref="Paalo.UnityMiscTools.Examples.DragAndDropAreaExample"/>' /// </summary> /// <seealso cref="HowToDrawDragAndDropArea"/> /// <typeparam name="T">The object type you want the '<paramref name="OnPerformedDragCallback"/>'-method to handle.</typeparam> /// <param name="dragAreaInfo">How the DragArea should look like and what text it should display.</param> /// <returns></returns> public static void DrawDragAndDropArea <T>(DragAndDropAreaInfo dragAreaInfo, System.Action <T[]> OnPerformedDragCallback = null) where T : UnityEngine.Object { //Change color and create Drag Area Color originalGUIColor = GUI.color; GUI.color = dragAreaInfo.outlineColor; EditorGUILayout.BeginVertical(GUI.skin.box); GUI.color = dragAreaInfo.backgroundColor; var dragArea = GUILayoutUtility.GetRect(dragAreaInfo.dragAreaWidth, dragAreaInfo.dragAreaHeight, GUILayout.ExpandWidth(true)); GUI.Box(dragArea, dragAreaInfo.DragAreaText); //See if the current Editor Event is a DragAndDrop event. var anEvent = Event.current; switch (anEvent.type) { case EventType.DragUpdated: case EventType.DragPerform: if (!dragArea.Contains(anEvent.mousePosition)) { //Early Out in case the drop is made outside the drag area. break; } //Change mouse cursor icon to the "Copy" icon DragAndDrop.visualMode = DragAndDropVisualMode.Copy; //If mouse is released if (anEvent.type == EventType.DragPerform) { DragAndDrop.AcceptDrag(); var draggedTypeObjectsArray = GetDraggedObjects <T>(); OnPerformedDragCallback?.Invoke(draggedTypeObjectsArray); } Event.current.Use(); break; } EditorGUILayout.EndVertical(); GUI.color = originalGUIColor; }
private void GUISection_GetAudioClips() { Color oldGuiColor = GUI.color; EditorGUILayout.BeginVertical(GUI.skin.box); EditorGUILayout.Space(); var dragAndDropInfo = new DragAndDropAreaInfo("Audio Clips", Color.black, Color.cyan); PaaloEditorHelper.DrawDragAndDropArea <AudioClip>(dragAndDropInfo, UpdateAudioClipsOnDragAndDrop); EditorGUILayout.Space(); GUI.color = Color.red; if (GUILayout.Button("Clear selected AudioClips")) { //audioClips = null; audioClips = new AudioClip[0]; } EditorGUILayout.Space(); GUI.color = oldGuiColor; EditorGUILayout.EndVertical(); }