1.新建场景,在场景中创建一个UI的Panel。
2.创建脚本Drag.cs,将脚本附加到Panel上。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Drag : MonoBehaviour, IDragHandler,IBeginDragHandler
{
//记录初始鼠标与元素的偏移量
private Vector3 offset;
public void OnBeginDrag(PointerEventData eventData)
{
offset = transform.position - new Vector3(eventData.position.x, eventData.position.y, transform.position.z);
}
public void OnDrag(PointerEventData eventData)
{
transform.position = offset + new Vector3(eventData.position.x, eventData.position.y, transform.position.z);
}
}