一、拖拽UI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class DragDrop : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
{
private RectTransform thisrectTransform;
public Canvas _Canvas;
private CanvasGroup thisCanvasGroup;
void Start()
{
thisrectTransform = GetComponent<RectTransform>();
thisCanvasGroup = GetComponent<CanvasGroup>();
}
public void OnBeginDrag(PointerEventData eventData)
{
thisCanvasGroup.alpha = 0.6f;
thisCanvasGroup.blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
thisrectTransform.anchoredPosition += eventData.delta/ _Canvas.scaleFactor;
}
public void OnEndDrag(PointerEventData eventData)
{
thisCanvasGroup.alpha = 1;
thisCanvasGroup.blocksRaycasts = true;
}
public void OnPointerDown(PointerEventData eventData)
{
}
}
二、放置
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ItemSlot : MonoBehaviour, IDropHandler
{
public void OnDrop(PointerEventData eventData)
{
if (eventData.pointerDrag != null)
{
eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition=GetComponent<RectTransform>().anchoredPosition;
}
}
}
三、
|