Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

EditorWindow not getting MouseDrag event at startup

Discussion in 'Scripting' started by Wheresmind, Aug 10, 2015.

  1. Wheresmind

    Wheresmind

    Joined:
    Oct 31, 2013
    Posts:
    5
    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 :
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. class MyEditorWindow : EditorWindow
    5. {
    6.     bool m_dragging = false;
    7.  
    8.     [MenuItem("Window/MyEditorWindow")]
    9.     public static void ShowWindow()
    10.     {
    11.         EditorWindow.GetWindow(typeof(MyEditorWindow));
    12.     }
    13.  
    14.     void OnGUI()
    15.     {
    16.         Event e = Event.current;
    17.  
    18.         if (m_dragging)
    19.         {
    20.             DragAndDrop.visualMode = DragAndDropVisualMode.Link;
    21.         }
    22.  
    23.         if ((e.type == EventType.DragUpdated) ||
    24.             (e.type == EventType.MouseDown) ||
    25.             (e.type == EventType.MouseUp))
    26.         {
    27.             Debug.Log(e.type);
    28.         }
    29.         else if((e.type == EventType.DragExited) ||
    30.                 (e.type == EventType.DragPerform))
    31.         {
    32.             m_dragging = false;
    33.             Debug.Log(e.type);
    34.         }
    35.         else if (e.type == EventType.mouseDrag)
    36.         {
    37.             DragAndDrop.PrepareStartDrag();
    38.             DragAndDrop.StartDrag("MyEditorWindow_drag");
    39.             Debug.Log(e.type);
    40.             m_dragging = true;
    41.         }
    42.     }
    43. }
    44.  
    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
     
  2. junglemason

    junglemason

    Joined:
    Dec 30, 2010
    Posts:
    69
    Sorry for zombie post, but I just ran into this with Unity 5.6.2. Anyone halp?
     
  3. junglemason

    junglemason

    Joined:
    Dec 30, 2010
    Posts:
    69
    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...

    Code (csharp):
    1. [InitializeOnLoad]
    2. public class EditorStartup
    3. {
    4.     static EditorStartup()
    5.     {
    6.         DragAndDrop.objectReferences = new Object[0];
    7.     }
    8. }