using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class DragChip : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public GameObject chipPrefab; public Image rouletteBoardImage; private RectTransform canvasRect; private RectTransform chipRect; private Vector2 originalPosition; private GameObject currentChip; private Canvas canvas; void Start() { // Get the RectTransform component of the canvas canvasRect = GetComponentInParent().GetComponent(); // Get the Canvas component of the parent object canvas = GetComponentInParent(); } public void OnBeginDrag(PointerEventData eventData) { // Create a clone of the chip image at the current position currentChip = Instantiate(chipPrefab, transform.position, Quaternion.identity, canvas.transform); // Get the RectTransform component of the clone chipRect = currentChip.GetComponent(); // Store the original position of the clone before dragging originalPosition = chipRect.anchoredPosition; // Bring the clone to the front chipRect.SetAsLastSibling(); } public void OnDrag(PointerEventData eventData) { // Move the clone with the mouse drag movement chipRect.anchoredPosition += eventData.delta / canvas.scaleFactor; } public void OnEndDrag(PointerEventData eventData) { // If the clone exists, check if it's dropped onto the roulette board image if (currentChip != null) { RectTransform boardRect = rouletteBoardImage.GetComponent(); Vector2 localMousePos; // Check if the mouse position is within the bounds of the roulette board image if (RectTransformUtility.ScreenPointToLocalPointInRectangle(boardRect, Input.mousePosition, canvas.worldCamera, out localMousePos)) { if (boardRect.rect.Contains(localMousePos)) { // If the chip is dropped onto the roulette board image, set its parent to the image and position it at the mouse cursor chipRect.SetParent(rouletteBoardImage.transform); chipRect.anchoredPosition = localMousePos; } else { // If the chip is dropped outside of the roulette board image, destroy it Destroy(currentChip); } } else { // If the mouse position is not within the bounds of the canvas, destroy the clone Destroy(currentChip); } // Set the clone to null to indicate that it no longer exists currentChip = null; } } }