using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UIEvents : MonoBehaviour,IDragHandler,IDropHandler
{
public RectTransform ParentPos;
private RectTransform rt;
bool isFirst = true;
Vector2 offset = new Vector2();
void Start()
{
rt = transform as RectTransform;
}
public void OnDrag(PointerEventData eventData1)
{
Vector2 mousePos = eventData1.position;
Vector2 CanvasPos = new Vector2 ();
bool componentIN = RectTransformUtility.ScreenPointToLocalPointInRectangle(ParentPos,mousePos,eventData1.enterEventCamera,out CanvasPos);
if(isFirst)
{
RectTransformUtility.ScreenPointToLocalPointInRectangle(rt,mousePos,eventData1.enterEventCamera,out offset);
isFirst = false;
}
else{
if(componentIN)
{
rt.anchoredPosition = CanvasPos - offset;
}
}
}
public void OnDrop(PointerEventData eventData)
{
isFirst = true;
Debug.Log("拖拽结束!");
}
}