- Home /
Question by
Sprakle · May 17, 2014 at 11:45 PM ·
guieditordraganddrop
OnGUI() not called during DragAndDrop
I have a custom editor window that needs some drag and drop functionality. For some reason, the window stops repainting once dragging starts. Since OnGUI stops, dragging can never complete.
What the test window looks like:
Test Code:
public class DragTest : EditorWindow
{
[MenuItem("Window/Drag Test")]
internal static void Init()
{
GetWindow(typeof(DragTest), false, "Drag Test");
}
private List<Draggable> draggables;
public DragTest()
{
draggables = new List<Draggable>();
for (int i = 0; i < 10; i++)
draggables.Add(new Draggable(i));
}
private void OnGUI()
{
Debug.Log("On GUI");
// update each draggable
foreach (Draggable draggable in draggables)
draggable.Draw();
}
}
public class Draggable
{
private int number;
public Draggable(int number)
{
this.number = number;
}
public void Draw()
{
EditorGUILayout.LabelField("Draggable Object");
// get area that can be clicked on
Rect dropArea = GUILayoutUtility.GetLastRect();
Event currentEvent = Event.current;
// ignore if mouse is outside the area
if (!dropArea.Contains(currentEvent.mousePosition))
return;
switch (Event.current.type)
{
case EventType.MouseDown:
Debug.Log("Clicked on " + number);
// no more OnGUI messages after this
DragAndDrop.PrepareStartDrag();
currentEvent.Use();
break;
case EventType.MouseDrag:
Debug.Log("Dragging" + number);
DragAndDrop.StartDrag("Title");
currentEvent.Use();
break;
case EventType.DragUpdated:
Debug.Log("Drag updated on " + number);
currentEvent.Use();
break;
}
}
}
Comment
I am basing this off of code found here: http://forum.unity3d.com/threads/223242-Drag-and-Drop-in-the-editor-Explanation
Very strange, the problem seems to go away if I have a second unrelated editor script updating at the same time. At least it works.
I wont mark this as answered until I'm sure it stays this way.
Your answer
![](https://koobas.hobune.stream/wayback/20220613145302im_/https://answers.unity.com/themes/thub/images/avi.jpg)