EditorWindow not getting MouseDrag event at startup

Hi,

It seems that when I create my own EditorWindow class and monitor the mouse and drag events the ‘MouseDrag’ event doesn’t act correctly when I first startup Unity. The first time I launch unity and attempt a drag and drop in my editor window I do not get a MouseDrag event until I let go of the mouse button (Instead of getting it when I first click and move the mouse). If, after that, I go in the ‘Hierarchy’ window and drag and drop anything, go back to my editor window all the events seem to work normally again until I restart Unity.

To test this I wrote the following script that, after creating an empty project, I put in Assets/Editor/MyEditorWindow.cs :

using UnityEngine;
using UnityEditor;

class MyEditorWindow : EditorWindow
{
    bool m_dragging = false;

    [MenuItem("Window/MyEditorWindow")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof(MyEditorWindow));
    }

    void OnGUI()
    {
        Event e = Event.current;

        if (m_dragging)
        {
            DragAndDrop.visualMode = DragAndDropVisualMode.Link;
        }

        if ((e.type == EventType.DragUpdated) ||
            (e.type == EventType.MouseDown) ||
            (e.type == EventType.MouseUp))
        {
            Debug.Log(e.type);
        }
        else if((e.type == EventType.DragExited) ||
                (e.type == EventType.DragPerform))
        {
            m_dragging = false;
            Debug.Log(e.type);
        }
        else if (e.type == EventType.mouseDrag)
        {
            DragAndDrop.PrepareStartDrag();
            DragAndDrop.StartDrag("MyEditorWindow_drag");
            Debug.Log(e.type);
            m_dragging = true;
        }
    }
}

When I do a drag and drop in my window after starting Unity I get the following log :

  • mouseDown
  • mouseDrag (happens when I let go of the mouse button)
  • DragExited

If I do a drag and drop in the ‘hierarchy’ window and try a drag and drop in my editor after that I get the expected :

  • mouseDown
  • mouseDrag
  • DragUpdated (Many times)
  • DragPerform
  • DragExited

Anybody got any clues on how to solve this ?

Thanks

Sorry for zombie post, but I just ran into this with Unity 5.6.2. Anyone halp?

OK, I found an answer elsewhere, so I’ll repost here. The DragAndDrop system needs to be initialized and isn’t done so until you drag something within a built-in editor window (I consider this a bug in Unity).

To work around…

[InitializeOnLoad]
public class EditorStartup
{
    static EditorStartup()
    {
        DragAndDrop.objectReferences = new Object[0];
    }
}