initial commit
This commit is contained in:
63
Assets/Scripts/DraggableCard.cs
Normal file
63
Assets/Scripts/DraggableCard.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
|
||||
|
||||
public class DraggableCard : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
|
||||
{
|
||||
public string layerName;
|
||||
|
||||
private RectTransform rectTransform;
|
||||
private CanvasGroup canvasGroup;
|
||||
private Vector2 startPosition;
|
||||
private Transform startParent;
|
||||
private Canvas canvas;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
rectTransform = GetComponent<RectTransform>();
|
||||
canvasGroup = GetComponent<CanvasGroup>();
|
||||
if (canvasGroup == null)
|
||||
canvasGroup = gameObject.AddComponent<CanvasGroup>();
|
||||
canvas = GetComponentInParent<Canvas>();
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
startPosition = rectTransform.anchoredPosition;
|
||||
startParent = transform.parent;
|
||||
}
|
||||
|
||||
public void OnBeginDrag(PointerEventData eventData)
|
||||
{
|
||||
if (transform.parent != startParent)
|
||||
{
|
||||
DropZone currentZone = transform.parent.GetComponent<DropZone>();
|
||||
if (currentZone != null)
|
||||
currentZone.currentCard = null;
|
||||
}
|
||||
|
||||
transform.SetParent(canvas.transform);
|
||||
transform.SetAsLastSibling();
|
||||
canvasGroup.blocksRaycasts = false;
|
||||
}
|
||||
|
||||
public void OnDrag(PointerEventData eventData)
|
||||
{
|
||||
rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
|
||||
}
|
||||
|
||||
public void OnEndDrag(PointerEventData eventData)
|
||||
{
|
||||
canvasGroup.blocksRaycasts = true;
|
||||
if (transform.parent == canvas.transform)
|
||||
ReturnToOriginal();
|
||||
}
|
||||
|
||||
public void ReturnToOriginal()
|
||||
{
|
||||
transform.SetParent(startParent);
|
||||
rectTransform.anchoredPosition = startPosition;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user