public void OnDrop(PointerEventData eventData) { if (!UIDrag.Dragging[eventData.pointerId].OnEndDrag) { UIDrag.Dragging[eventData.pointerId].Object.GetComponent <UIDrag>() .OnEndDrag(eventData); //Ensure execution order OnBeginDrag -> OnDrag -> OnEndDrag -> OnDrop } if (UIDrag.Dragging[eventData.pointerId].OnDrop) { return; } //Debug.Log("OnDrop " + eventData.pointerId); UIDrag.Dragging[eventData.pointerId].Object.SnapTo(this); if (Stored != null) { Destroy(Stored.gameObject); //remove old to replace to new object Stored = null; } Stored = UIDrag.Dragging[eventData.pointerId].Object; UIDrag.Dragging[eventData.pointerId].Object.CurrentContainer = this; OnDropFinishHandler.Invoke(UIDrag.Dragging[eventData.pointerId].Object); //alias to OnDropFinish OnDropFinish(UIDrag.Dragging[eventData.pointerId].Object); //alias to OnDropFinishHandler UIDrag.Dragging[eventData.pointerId].OnDrop = true; //UIDrag.Dragging[eventData.pointerId] = null; }
public void DestroyItemStored() { if (Stored != null) { Destroy(Stored.gameObject); Stored = null; } }
//Override this if you want a custom drop logic public virtual bool CanDrop(PointerEventData eventData, UIDrag draggingObject) { if (RectTransformUtility.RectangleContainsScreenPoint(transform as RectTransform, eventData.position, eventData.enterEventCamera)) //to make sure that the object will be inside a slot { if (SwapItems) { return(true); } if (Exclusive && Stored != null) { return(false); } return(true); } Debug.Log("Refused by cursor location."); return(false); }
public void OnEndDrag(PointerEventData eventData) { if (Dragging[eventData.pointerId].OnEndDrag) { return; } //Debug.Log("OnEndDrag " + eventData.pointerId); UIDrop droppedTarget = null; foreach (UIDrop targetSlot in hovered) { //Debug.Log("Hovering " + targetSlot.transform.name); if (targetSlot != CurrentContainer && targetSlot.CanDrop(eventData, this) ) //prevent self-container swap and drop check { UIDrag other = targetSlot.Stored; UIDrop destinationContainer = targetSlot; UIDrop originContainer = CurrentContainer; if (targetSlot.SwapItems) { if (targetSlot.Stored != null) { other.SnapTo(originContainer, true); //move other UIDrag to origin UIDrop other.CurrentContainer = originContainer; other.CanvasGroup.blocksRaycasts = true; originContainer.Stored = other; destinationContainer.Stored = null; CurrentContainer = null; Debug.Log("Swapped"); Debug.Log("Other[" + other.name + "] container: " + other.CurrentContainer.name + " | Origin Container[" + originContainer.name + "]: " + originContainer.Stored.name); other.OnSlotChanged(destinationContainer, originContainer); } else { if (originContainer != null) { originContainer.Stored = null; } CurrentContainer = null; } } else { if (originContainer != null) { originContainer.Stored = null; //remove reference from container } CurrentContainer = null; } droppedTarget = destinationContainer; OnSlotChanged(originContainer, destinationContainer); break; } Debug.Log("Valid container[" + targetSlot.name + "] = " + (targetSlot != CurrentContainer) + " | Can be dropped = " + targetSlot.CanDrop(eventData, this)); } if (droppedTarget == null) { Dragging[eventData.pointerId].OnDrop = true; //prevent OnDrop execution when cant drop if (DestroyOnDropInvalid) { if (CurrentContainer != null) { CurrentContainer.Stored = null; //remove reference from container } Destroy(gameObject); return; } if (ReturnoToStart) { RestoreAll(ReturnWillBeAnimated); //back to slot position } } forceCleanAllHover(); CanvasGroup.blocksRaycasts = true; OnDragEnd(eventData); Dragging[eventData.pointerId].OnEndDrag = true; if (droppedTarget != null) { droppedTarget.OnDrop(eventData); } }
public DraggingObject(int id, UIDrag theObject, RectTransform plane = null) { ID = id; Object = theObject; Plane = plane; }
protected virtual void OnDropFinish(UIDrag draggedObject) { }