Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- public class DragDrop : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
- {
- private RectTransform rectTransform;
- private CanvasGroup canvasGroup;
- public static GameObject itemBeingDragged;
- Vector3 startPosition;
- Transform startParent;
- private void Awake()
- {
- rectTransform = GetComponent<RectTransform>();
- canvasGroup = GetComponent<CanvasGroup>();
- }
- public void OnBeginDrag(PointerEventData eventData)
- {
- canvasGroup.alpha = .6f;
- //So the ray cast will ignore the item itself.
- canvasGroup.blocksRaycasts = false;
- startPosition = transform.position;
- startParent = transform.parent;
- transform.SetParent(transform.root);
- itemBeingDragged = gameObject;
- }
- public void OnDrag(PointerEventData eventData)
- {
- //So the item will move with our mouse (at same speed) and so it will be consistant if the canvas has a different scale (other then 1);
- rectTransform.anchoredPosition += eventData.delta;
- }
- public void OnEndDrag(PointerEventData eventData)
- {
- itemBeingDragged = null;
- if (transform.parent == startParent || transform.parent == transform.root)
- {
- transform.position = startPosition;
- transform.SetParent(startParent);
- }
- canvasGroup.alpha = 1f;
- canvasGroup.blocksRaycasts = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement