- Home /
Letting go of a dragged object
I have this code:
if( new Rect(0, 0+ (cnt * BUTTONSIZE), BUTTONSIZE,BUTTONSIZE).Contains(e.mousePosition)) {
if(e.clickCount == 2) {
//DO something
}
if(e.button == 0 && e.type == EventType.mouseDrag && !draggingItem){
draggingItem = true;
draggedItemIndex = cnt;
draggedItem = item;
Debug.Log ("Test drag");
}
if(e.type == EventType.mouseUp && draggingItem )
{
Debug.Log ("Dropped item");
}
}
The "Debug.Log("Dropped item");" is never printed to the console. So why is the "mouseUp" event never recognized? The dragging and the double click works.
Dragging the item and then doubleclicking prints "Dropped item" to the console but that is not how it should work.
Answer by Kashrlyyk · Aug 22, 2014 at 11:03 AM
It only detects the mouseUp event when I let the mouse button go while I am hovering over the Rect defined at the top.
EDIT: I want to drag the item from the inventory inside a "GUI.BeginScrollView" and drop it into a "GUI.SelectionGrid" outside the scrollview.
So I have added this to my code which is completely inside the "GUI.BeginScrollView" section:
for(int cnt=0; cnt < _inventPlayer1W.Count; cnt++) {
EquipShortcut item = _inventPlayer1W[cnt] as EquipShortcut;
if( _inventPlayer1W[cnt] != null ) {
GUI.DrawTexture(new Rect(0, 0+ (cnt * BUTTONSIZE), BUTTONSIZE,BUTTONSIZE), _inventPlayer1W[cnt].GetIcon());
GUI.Box (new Rect(0+2+BUTTONSIZE, 0+ (cnt * BUTTONSIZE), 320,BUTTONSIZE), _inventPlayer1W[cnt].GetName(),GUI.skin.GetStyle("ToolTipHeading" ));
GUI.Label (new Rect(0+2+BUTTONSIZE, 0+ (cnt * BUTTONSIZE), 30,30), c1.Inventory.GetWeaponCount(item.ID).ToString());
}
if(new Rect(0, 0+(cnt * BUTTONSIZE), BUTTONSIZE, BUTTONSIZE).Contains(e.mousePosition)) {
Debug.Log ("Slots in inventory"+ " " + slotRect.x.ToString()+ " "+ slotRect.min.ToString() +" " + slotRect.max.ToString() + " " + slotRect.y.ToString());
slotRect = new Rect(0, 0+ (cnt * BUTTONSIZE), BUTTONSIZE,BUTTONSIZE);
if( slotRect.Contains(e.mousePosition)) {
if(e.type == EventType.mouseDown && e.button == 1){
c1.Inventory.Remove(c1.Inventory.GetEquipment(item.ID, ItemDropType.Weapon),1, true, true);
if(c1.Inventory.GetWeaponCount(item.ID) == 0){
_inventPlayer1W.Remove(_inventPlayer1W[cnt]);
//_inventPlayer1W = c1.Inventory.GetContent(c1, false, false, true, false);
}
}
if(e.clickCount == 2) { //e.type == EventType.mouseDown && e.button == 0 &&
if(c1.Inventory.GetEquipment(item.ID, ItemDropType.Weapon).CanEquip(c1)) {
c1.Equipment.Equip(-1, item, c1.Inventory, false);
if(c1.Inventory.GetWeaponCount(item.ID) == 0){
_inventPlayer1W.Remove(_inventPlayer1W[cnt]);
}
}
}
if(e.button == 0 && e.type == EventType.mouseDrag && !draggingItem){
draggingItem = true;
draggedItemIndex = cnt;
draggedItem = item;
Debug.Log ("Test drag" + draggedItemIndex.ToString() + " " + draggedItem.GetName());
}
if(e.button == 0 && e.type == EventType.MouseUp && draggingItem )// && player2GridPos.Contains(e.mousePosition))
{
Debug.Log ("Dropped item");
//_inventPlayer2W.Add(_inventPlayer1W[cnt]);
//_inventPlayer1W.Remove(_inventPlayer1W[cnt]);
draggingItem = false;
draggedItem = null;
}
}
} else if(new Rect(25-WIDTHOFFSET, 25-HEIGHTOFFSET-200, 192, 270).Contains(e.mousePosition)){
Debug.Log ("This should work"+ draggingItem.ToString() + " " + slotRect.x.ToString()+ " "+ slotRect.min.ToString() +" " + slotRect.max.ToString() + " " + slotRect.y.ToString());
for(int y1 = 0; y1 < 3; y1++) {
for(int x1 = 0; x1 < 2; x1++) {
slotRect = new Rect(25+ (x1 * 95)-WIDTHOFFSET,25 + (y1 * 89)-HEIGHTOFFSET-200, 97, 87);
/*if(e.button == 0 && e.type == EventType.mouseDrag && !draggingItem){
draggingItem = true;
draggedItemIndex = cnt;
draggedItem = item;
Debug.Log ("Test drag" + draggedItemIndex.ToString() + " " + draggedItem.GetName());
}*/
if(e.button == 0 && e.type == EventType.MouseUp && draggingItem )// && player2GridPos.Contains(e.mousePosition))
{
Debug.Log ("Dropped item");
draggingItem = false;
draggedItem = null;
}
}
}
}
}
"new Rect(25-WIDTHOFFSET, 25-HEIGHTOFFSET-200, 192, 270)" that is the position and size of the selectionGrid seen from inside the scrollview and when I hover over it the "This should work...." line is printed to the console but I still can't drop the item on that rect.
Your answer
Follow this Question
Related Questions
How To Make GUI/Dragable Object - Att: Video 3 Answers
Mouse drag get angle 1 Answer
Mouse drag gameobject 0 Answers
Input.GetMouseButtonUp not detected after drag 1 Answer
Camera and Mouse Movement 0 Answers