61 lines
1.4 KiB
C#
61 lines
1.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class DragDrop : MonoBehaviour, IDragHandler, IPointerDownHandler, IBeginDragHandler, IEndDragHandler
|
|
{
|
|
[SerializeField] private Canvas canvas;
|
|
private RectTransform rectTransform;
|
|
private CanvasGroup canvasGroup;
|
|
private Vector2 originalPosition;
|
|
private bool droppedOnValidSlot = false; // Flag
|
|
|
|
private void Awake()
|
|
{
|
|
rectTransform = GetComponent<RectTransform>();
|
|
canvasGroup = GetComponent<CanvasGroup>();
|
|
originalPosition = rectTransform.anchoredPosition;
|
|
}
|
|
|
|
public void ResetPosition()
|
|
{
|
|
rectTransform.anchoredPosition = originalPosition;
|
|
}
|
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
Debug.Log("OnBeginDrag");
|
|
canvasGroup.alpha = .6f;
|
|
canvasGroup.blocksRaycasts = false;
|
|
droppedOnValidSlot = false;
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
Debug.Log("OnEndDrag");
|
|
if (!droppedOnValidSlot)
|
|
{
|
|
ResetPosition();
|
|
}
|
|
canvasGroup.alpha = 1f;
|
|
canvasGroup.blocksRaycasts = true;
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
Debug.Log("OnDrag");
|
|
rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
|
|
}
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
Debug.Log("OnPointerDown");
|
|
}
|
|
|
|
public void SetDroppedSuccessful()
|
|
{
|
|
droppedOnValidSlot = true;
|
|
}
|
|
}
|