Search Unity

Custom Editor DragAndDrop only works after dragging unrelated things in Hierachy

Discussion in 'Editor & General Support' started by Sprakle, May 18, 2014.

  1. Sprakle

    Sprakle

    Joined:
    Jun 8, 2013
    Posts:
    31
    I have a custom editor window that needs some drag and drop functionality for a hierarchical tree. (like the Scene Hierarchy window)

    I've been encountering a bug where OnGUI() would not be called during a drag process, breaking everything. However, I recently noticed that this was completely fixed after dragging something in the Scene Hierarchy window. In fact, using any custom editor (I tried the RAIN behaviour tree) that involves dragging seemed to fix my bug.

    So it seems there is some hidden state change (That's bad OOP design Unity!) that I am unaware of.

    Here is the code I am testing with. (open "Window>Drag Test" after importing)
    If it works fine for you, try restarting Unity to reset the mystery state.

    Code (csharp):
    1.  
    2.  
    3.     public class DragTest : EditorWindow
    4.     {
    5.         [MenuItem("Window/Drag Test")]
    6.         internal static void Init()
    7.         {
    8.             GetWindow(typeof(DragTest), false, "Drag Test");
    9.         }
    10.  
    11.         private void OnGUI()
    12.         {
    13.             // draw 10 draggable objects
    14.             for (int i = 0; i < 10; i++)
    15.                 DrawDraggable();
    16.         }
    17.  
    18.         private void DrawDraggable()
    19.         {
    20.             EditorGUILayout.LabelField("Draggable Object");
    21.  
    22.             // get area that can be clicked on
    23.             Rect dropArea = GUILayoutUtility.GetLastRect();
    24.  
    25.             // ignore if mouse is outside the area
    26.             if (!dropArea.Contains(Event.current.mousePosition))
    27.                 return;
    28.  
    29.             switch (Event.current.type)
    30.             {
    31.                 case EventType.MouseDown:
    32.                     DragAndDrop.PrepareStartDrag();
    33.                     break;
    34.  
    35.                 case EventType.MouseDrag:
    36.                     DragAndDrop.StartDrag("drag-test");
    37.                     break;
    38.  
    39.                 case EventType.DragUpdated:
    40.                     DragAndDrop.visualMode = DragAndDropVisualMode.Link;
    41.                     break;
    42.  
    43.                 case EventType.Repaint:
    44.                     if (DragAndDrop.visualMode == DragAndDropVisualMode.None ||
    45.                         DragAndDrop.visualMode == DragAndDropVisualMode.Rejected)
    46.                         break;
    47.  
    48.                     // highlight
    49.                     EditorGUI.DrawRect(dropArea, new Color(0.2f, 0.2f, 0.2f, 0.5f));
    50.                     break;
    51.             }
    52.         }
    53.     }
    54.  
    55.  
    I tried using reflection to understand what state was being changed, but nothing useful came of that.
     
    Last edited: Sep 3, 2014
  2. Sprakle

    Sprakle

    Joined:
    Jun 8, 2013
    Posts:
    31
    I left the project alone for a while, but I'm back now and the problem remains. Bump!

    I must be doing something wrong, since I've seen many other custom editors with working hierarchy trees .
     
  3. thelackey3326

    thelackey3326

    Joined:
    Sep 17, 2010
    Posts:
    34
    I'm having a DragAndDrop issue of my own (Mac only, though), and ran across your thread. Found another thread earlier that might explain your problem.

    http://forum.unity3d.com/threads/ed...stem-needs-to-be-initialized-by-unity.219342/

    In short: make sure that DragAndDrop.objectReferences isn't null. If you aren't using it, just assign an empty object array to it just before the call to StartDrag().

    Code (CSharp):
    1. DragAndDrop.objectReferences = new Object[] { null };
    Also worth noting that in the referenced thread, as well as my own code, PrepareStartDrag() is being called within the mouseDrag event instead of on mouseDown. Hard to tell if this is the "right" way since the DragAndDrop docs are currently...uh...unhelpful.