public void OnDrop(PointerEventData data) { if (DragAndDropItem_Training.icon != null) { if (DragAndDropItem_Training.icon.activeSelf == true) // If icon inactive do not need to drop item in cell { DragAndDropItem_Training item = DragAndDropItem_Training.draggedItem; DragAndDropCell_Training sourceCell = DragAndDropItem_Training.sourceCell; if ((item != null) && (sourceCell != this)) { switch (sourceCell.cellType) // Check source cell's type { case CellType.UnlimitedSource: string itemName = item.name; int itemnum = item.IndexNum; item = Instantiate(item); // Clone item from source cell item.name = itemName; item.IndexNum = itemnum; break; default: // Nothing to do break; } switch (cellType) // Check this cell's type { case CellType.Swap: DragAndDropItem_Training currentItem = GetComponentInChildren <DragAndDropItem_Training>(); switch (sourceCell.cellType) { case CellType.Swap: SwapItems(sourceCell, this); // Swap items between cells break; default: PlaceItem(item.gameObject); // Place dropped item in this cell break; } break; case CellType.DropOnly: PlaceItem(item.gameObject); // Place dropped item in this cell break; default: //nothing to do. break; } } if (item.GetComponentInParent <DragAndDropCell_Training>() == null) // If item have no cell after drop { Destroy(item.gameObject); // Destroy it } TrainingManager.TMInstance.DragEndAction(sourceCell.GetCellNumber(), this.GetCellNumber(), item.IndexNum); } } }
public void OnBeginDrag(PointerEventData eventData) { if (IsItemCanDrag) { sourceCell = GetComponentInParent <DragAndDropCell_Training>(); // Remember source cell draggedItem = this; // Set as dragged item icon = new GameObject("Icon"); // Create object for item's icon Image image = icon.AddComponent <Image>(); image.sprite = GetComponent <Image>().sprite; image.raycastTarget = false; // Disable icon's raycast for correct drop handling RectTransform iconRect = icon.GetComponent <RectTransform>(); // Set icon's dimensions iconRect.sizeDelta = new Vector2(GetComponent <RectTransform>().sizeDelta.x, GetComponent <RectTransform>().sizeDelta.y); Canvas canvas = GetComponentInParent <Canvas>(); // Get parent canvas if (canvas != null) { // Display on top of all GUI (in parent canvas) icon.transform.SetParent(canvas.transform, true); // Set canvas as parent icon.transform.SetAsLastSibling(); // Set as last child in canvas transform } if (OnItemDragStartEvent != null) { OnItemDragStartEvent(this); // Notify all about item drag start } } }
public void SwapItems(DragAndDropCell_Training firstCell, DragAndDropCell_Training secondCell) { if ((firstCell != null) && (secondCell != null)) { DragAndDropItem_Training firstItem = firstCell.GetItem(); // Get item from first cell DragAndDropItem_Training secondItem = secondCell.GetItem(); // Get item from second cell if (firstItem != null) { // Place first item into second cell firstItem.transform.SetParent(secondCell.transform, false); firstItem.transform.localPosition = Vector3.zero; } if (secondItem != null) { // Place second item into first cell secondItem.transform.SetParent(firstCell.transform, false); secondItem.transform.localPosition = Vector3.zero; } } }
public void OnEndDrag(PointerEventData eventData) { eventData.Reset(); if (icon != null) { Destroy(icon); // Destroy icon on item drop } MakeVisible(true); // Make item visible in cell if (OnItemDragEndEvent != null) { OnItemDragEndEvent(this); // Notify all cells about item drag end } //빈 공간에 드래그 앤 드롭 했을시 if (eventData.pointerCurrentRaycast.gameObject.tag != "SkillPanel" && sourceCell.cellType == DragAndDropCell_Training.CellType.Swap) { sourceCell.RemoveItem(); TrainingManager.TMInstance.DragEndAction(sourceCell.GetCellNumber(), null, this.IndexNum); } draggedItem = null; icon = null; sourceCell = null; }