- Home /
Drag.Exited editor event confusion
I am trying to drag some game objects from the hierarchy window and drop them inside a rectangle field and automatically add them to a list. So my problem lies when trying to resize the SerializedProperty List which resides inside the inspected object, within the editor events. When trying to do a simple resize over the SerializedProperty "m_objectsList" like so:
serializedObject.Update();
if (current.type == EventType.DragExited)
{
Debug.Log("Drag Exited"); // Does get called.
m_objectsList.arraySize++; // Does not resize.
current.Use();
}
serializedObject.ApplyModifiedProperties();
the list does not expand. However when i switch the event for instance with MouseUp the array expands as expected.
serializedObject.Update();
if (current.type == EventType.MouseUp)
{
m_objectsList.arraySize++; // Resizes as expected
current.Use();
}
serializedObject.ApplyModifiedProperties();
So even that the DragExited event get's performed it does not want to apply the modified properties? What is that i am missing and not understanding correctly.
Try a sanity check:
if (current.type == EventType.DragExited)
{
Debug.Log("Drag Exited, arraySize: " + m_objectsList.arraySize);
m_objectsList.arraySize++;
current.Use();
Debug.Log("arraySize now: " + m_objectsList.arraySize);
}
Did that already at the first place. It is increasing the array size by 1 but never shows that new element in the inspector and never resizes it more than 1 comparing to the $$anonymous$$ouseUp event which normally increases the array.
The array size get's 0 as soon as the OnInspectorGUI() code finishes execution and starts again it's next frame.
6 years later and I'm getting screwed by this now, almost at the point of pulling my hair. One thing I found is that after DragExited is called, shortly after an Undo event very precisely will be called by God knows what to specifically nuke any changes you have made during that DragExited call, I swear to GOD it's like someone at Unity is just trolling us at this point.
Answer by sysameca · Jan 27, 2015 at 06:52 AM
So i will write what i did if somebody else ever needs this. It seems that for some reason when trying to perform some action over a SerializedProperty inside the if (current.type == EventType.DragExited) brackets, when dragging from the hierachy window to some inspector item, does not apply the modified properties of the serialized object. As i though what can i simply do, i came up with a bool flag to check when the drag exits and just do my logic inside the EventType.Repaint:
private bool m_dragExited;
public override void OnInspectorGUI()
{
serializedObject.Update();
if (current.type == EventType.DragExited)
{
if (position.Contains(m_mousePosition))
{
m_dragExited = true;
}
}
if (current.type == EventType.Repaint && m_dragExited == true)
{
// Perform whatever action needs to be done when the drag exits
m_dragExited = false;
}
serializedObject.ApplyModifiedProperties();
}
Answer by Moe_Baker · Jun 08, 2021 at 08:23 PM
Very old thread but I wanted to answer it since I have been pulling my hair with this problem for a good day.
DragExited is meant to be used for when a drag is canceled (like when the user presses escape when dragging) so in a sense, it really should've been called DragCancelled instead, and to add insult to injury, if you were to use the DragExited event type then shortly afterward a very mean & silent undo operation will be invoked to undo all of the changes you made, so it will make it seem as if the changes are applied but then they disappear three frames later ....
DragPerform is what gets called when the drag is "performed" which any reasonable person would assume to be when an object is first dragged, but you see it's actually called when the drag operation is completed, so you'll need to use that instead.
Thank you for wasting a day of my life Unity, hopefully, this information saves someone else from wasting their time as well.
Your answer
Follow this Question
Related Questions
DragAndDrop class 1 Answer
Can't drag and drop assets to editor. 6 Answers
Possible to drag items across tabs onto the same frame? 1 Answer
Drag & Drop Components in the Editor 1 Answer
drag and drop script 1 Answer