Hello !
I’m Using the GraphView blackboard tool and I’d like to implement drag and drop operation for blackboard variables. However there seems to be a bug in the UnityEditor.DragAndDrop as this bit of code doesn’t work :
blackboardField.RegisterCallback<MouseDownEvent>(evt =>
{
if (evt.button == (int)MouseButton.LeftMouse)
{
DragAndDrop.PrepareStartDrag();
DragAndDrop.SetGenericData("variableGUID", variable.GUID);
Debug.Log(Event.current.type == EventType.MouseDown); // Prints "True"
DragAndDrop.StartDrag("Drag");
}
evt.StopPropagation();
});
It gives me an error saying : “Drags can only be started from MouseDown or MouseDrag events”
However as you can see with the Debug.Log check, the current event is indeed of type MouseDown.
Does somebody knows how to fix this bug ?
Thank you very much and good day !
This looks like IMGUI code, isn’t it? GraphView is using UI Toolkit.
How to drag & drop with UI Toolkit
This is UITK code. The DragAndDrop class is not IMGUI exclusive; it’s needed to drag things between windows. There’s an example in the manual for that.
This does seem to me like a Unity bug. I think the OP could report it.
Interestingly, the example uses OnPointerDown rather than OnMouseDown. Can’t hurt to try the other although I don’t expect it to differ. Also OP should try the example script in the project to see if that has the same issue in which case => report.
Okay thank you for your replies,
I tried using OnPointerDown but it still gives me the same error.
I’ll report this bug.
Thank you very much and good day.
Again - this is about UI toolkit, it doesn’t make much sense, to copy and paste an Unity GUI example.
Anybody has figured this out?
Because a simple button down should never initiate a drag operation and in the move event, the OPs message is thrown. As this is still the case, I guess that has some correctness, but what is the correct way? The examples all take the short cut via button down and a drag needs to react on initial movement AFTER the button down. To me, it makes no sense to limit this to button down and a mouse drag event simply doesn’t exist in UI toolkit.
Handlers can’t be the answer, because they do it the very same way, but simply externalizing this into an additional class. There is no extra functionality for this.
So the question remains:
Where and when do we call StartDrag actually?
Ok, solved it. In the end, it is quite easy, you CAN trigger StartDrag within the MouseMoveEvent, IF the left button is pressed. Seems, both events are called from the same spot, so this works:
sprites.RegisterCallback<MouseMoveEvent>(evt => {
if ((evt.pressedButtons & 1) == 0) {
return;
}
DragAndDrop.PrepareStartDrag();
DragAndDrop.objectReferences = new Object[]{ objectToBeDragged };
DragAndDrop.paths = new [] { AssetDatabase.GetAssetPath(objectToBeDragged) };
DragAndDrop.StartDrag("");
});
Hope, this may help one or the other seeker in the future!