using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class Shirt : MonoBehaviour, IDropHandler { public const string DRAGGABLE_TAG = "UIDraggable"; private bool dragging = false; private Vector2 originalPosition; private Transform objectToDrag; private Object objectToDragImage; List hitObjects = new List(); private void Update() { if (Input.GetMouseButton(0)) { objectToDrag = GetDraggableTransformUnderMouse(); if (objectToDrag != null) { dragging = true; objectToDrag.SetAsLastSibling(); originalPosition = objectToDrag.position; objectToDrag.GetComponent().enabled = false; } if (dragging) { objectToDrag.position = Input.mousePosition; } if (Input.GetMouseButtonUp(0)) { if (objectToDrag != null) { Transform objectToReplace = GetDraggableTransformUnderMouse(); if (objectToReplace != null) { objectToDrag.position = objectToReplace.position; objectToReplace.position = originalPosition; } else { objectToDrag.position = originalPosition; } objectToDrag.GetComponent().enabled = true; objectToDrag = null; } dragging = false; } } } private GameObject GetObjectUnderMouse() { var pointer = new PointerEventData(EventSystem.current); pointer.position = Input.mousePosition; EventSystem.current.RaycastAll(pointer, hitObjects); if (hitObjects.Count <= 0) { return null; } return hitObjects.First().gameObject; } private Transform GetDraggableTransformUnderMouse() { GameObject clickedObject = GetObjectUnderMouse(); if (clickedObject != null && clickedObject.tag == DRAGGABLE_TAG) { return clickedObject.transform; } return null; } }