using UnityEngine;
using UnityEngine.EventSystems;
public class text : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
private Vector3 originalPosition;
private Transform 父对象;
public Transform 临时父对象;
public void 点击控件()
{
// 记录开始拖拽时的原始位置和父对象
originalPosition = transform.position;
父对象 = transform.parent;
transform.SetParent(临时父对象);//为了显示在最上层
transform.SetParent(父对象);//为了显示在最上层
}
private RectTransform rectTransform;
private Vector3 pos; //控件初始位置
private Vector3 mousePos; //鼠标初始位置
private void Start()
{
rectTransform = GetComponent<RectTransform>();
}
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("开始拖拽");
pos = this.GetComponent<RectTransform>().position;
RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out mousePos);
}
public void OnDrag(PointerEventData eventData)
{
Vector3 newVec;
RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out newVec);
Vector3 offset = new Vector3(newVec.x - mousePos.x, newVec.y - mousePos.y, 0);
rectTransform.position = pos + offset;
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("结束拖拽");
}
}