public void OnDrag(PointerEventData eventData) { draggingSprite.transform.position = eventData.position; // Check if hovering List <RaycastResult> results = new List <RaycastResult>(); EventSystem.current.RaycastAll(eventData, results); // Find what we might be hovering over UISlotRef hover = FindSlotAtPointer(eventData); // If they are different, we end an old hover and start a new one if (prevHover.container != hover.container || prevHover.slot != hover.slot) { if (prevHover.slot && prevHoverSlot.container != null) { prevHover.slot.Highlighted = false; } else if (prevHover.container && uiInventory.CanMoveItem(holding.container, holding.index, prevHover.container)) { prevHover.container.Highlighted = false; } prevHover = hover; if (hover.slot) { var hoverSlot = hover.container.GetSlotLink(hover.slot); if (hoverSlot.container && holding.container.GetItem(holding.index) != null && uiInventory.CanMoveItem(holding.container, holding.index, hoverSlot.container, hoverSlot.index)) { prevHoverSlot = hoverSlot; hover.slot.Highlighted = true; } } else if (hover.container && uiInventory.CanMoveItem(holding.container, holding.index, hover.container)) { hover.container.Highlighted = true; } } }
public void OnEndDrag(PointerEventData eventData) { // Stop any hovering if (prevHover.slot) { prevHover.slot.Highlighted = false; } else if (prevHover.container) { prevHover.container.Highlighted = false; } holding.uiSlot.Transparent = false; // Revert the dragging object if (draggingSprite != null) { Destroy(draggingSprite); draggingSprite = null; } prevHover = new UISlotRef(); prevHoverSlot = new SlotInfo(); // Now attempt the actual item movement var destination = FindSlotAtPointer(eventData); bool moveOccured = false; if (destination.slot && destination.slot.enabled) { // Move from holding to destination var destinationSlot = destination.container.GetSlotLink(destination.slot); if (holding.container == destinationSlot.container && holding.index == destinationSlot.index) { moveOccured = true; } else if (uiInventory.CanMoveItem(holding.container, holding.index, destinationSlot.container, destinationSlot.index)) { uiInventory.MoveItem(holding.container, holding.index, destinationSlot.container, destinationSlot.index); moveOccured = true; } } else if (destination.container) { if (uiInventory.FindHandler(holding.container) == destination.container) { moveOccured = true; } else if (uiInventory.CanMoveItem(holding.container, holding.index, destination.container)) { uiInventory.MoveItem(holding.container, holding.index, destination.container); moveOccured = true; } } // If we could not move it to the intended place, move the item back to the original slot. if (moveOccured == false && uiInventory.CanMoveItem(holding.container, holding.index, origin.container, origin.index)) { uiInventory.MoveItem(holding.container, holding.index, origin.container, origin.index); } origin = new SlotInfo(); holding = new SlotInfo(); Destroy(this); }