public IDragAndDropContainer GetContainer() { IDragAndDropContainer container = null; Transform par = transform.parent; while (container == null && par != null) { container = par.GetComponent <IDragAndDropContainer>(); par = par.parent; } if (container != null) { cachedContainer = container; } else { container = cachedContainer; } return(container); }
void Update() { // on mouse down, start dragging if we find a draggable if (Input.GetMouseButtonDown(0)) { objectToDrag = GetDraggableTransformUnderMouse(); // check with the container to see if we can drag objects if (objectToDrag != null) { IDragAndDropContainer containerDrag = objectToDrag.GetContainer(); if (containerDrag != null && containerDrag.CanDrag(objectToDrag) == false) { objectToDrag = null; } } if (objectToDrag != null) { dragging = true; // do this so the dragged object appears on top of other sibling containers //objectToDrag.transform.parent.SetAsLastSibling(); originalPosition = objectToDrag.transform.position; originalParent = objectToDrag.transform.parent; Transform parentCanvas = objectToDrag.transform.parent; while (parentCanvas != null && parentCanvas.GetComponent <Canvas>() == null) { parentCanvas = parentCanvas.parent; } objectToDrag.transform.parent = parentCanvas; parentCanvas.SetAsLastSibling(); // make the dragged object appear over all other objects in the container objectToDrag.transform.SetAsLastSibling(); // make the object being dragged transparent to raycasts objectToDragImage = objectToDrag.GetComponent <Image>(); objectToDragImage.raycastTarget = false; } } if (dragging) { objectToDrag.transform.position = Input.mousePosition; } // on mouse up, try to complete the drag and drop if (Input.GetMouseButtonUp(0)) { if (objectToDrag != null) { IDraggable objectToReplace = GetDraggableTransformUnderMouse(); // find the container we're dropping in to and from IDragAndDropContainer containerReplace = null; IDragAndDropContainer containerDrag = objectToDrag.GetContainer(); if (objectToReplace) { containerReplace = objectToReplace.GetContainer(); // if there is a container and we can't drop into it for game logic reasons, cancel the drag if (containerReplace != null) { if (containerReplace.CanDrop(objectToDrag, objectToReplace) == false) { objectToReplace = null; } } // check the other way too, since the drag and drop is a swap if (containerDrag != null) { if (containerDrag.CanDrop(objectToReplace, objectToDrag) == false) { objectToReplace = null; } } } if (objectToReplace != null) { // store the indexes of where these objects came from to pass through, they may get changed in Drop() calls int dragIndex = objectToDrag.index; int replaceIndex = objectToReplace.index; IDraggable replacement = Instantiate(objectToReplace); IDraggable current = Instantiate(objectToDrag); // game logic - let both containers know about the update if (containerReplace != null) { containerReplace.Drop(current, objectToReplace, replaceIndex, containerReplace != containerDrag); } if (containerDrag != null) { containerDrag.Drop(replacement, objectToDrag, dragIndex, true); } if (objectToDrag.GetContainer().DoesSwap()) { // swap positions objectToDrag.transform.position = objectToReplace.transform.position; objectToReplace.transform.position = originalPosition; // swap parents objectToDrag.transform.parent = objectToReplace.transform.parent; objectToReplace.transform.parent = originalParent; } else { // return to point of origin and nothing happens objectToDrag.transform.position = originalPosition; objectToDrag.transform.parent = originalParent; } } else { // return to point of origin and nothing happens objectToDrag.transform.position = originalPosition; objectToDrag.transform.parent = originalParent; } objectToDragImage.raycastTarget = true; objectToDrag = null; } dragging = false; } }