using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>
/// UI拖拽
/// </summary>
public class DragUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
/// <summary>
/// 拖拽对象
/// </summary>
public Transform _Target;//可拖拽区域
/// <summary>
/// 拖拽对象RectTransform
/// </summary>
public RectTransform _TargetRect;//整个窗口
private bool isDrag;
private Vector3 offset;
public void OnBeginDrag(PointerEventData eventData)
{
isDrag = false;
SetDragObjPostion(eventData);
_TargetRect.transform.SetSiblingIndex(1);
}
public void OnDrag(PointerEventData eventData)
{
isDrag = true;
SetDragObjPostion(eventData);
}
public void OnEndDrag(PointerEventData eventData)
{
SetDragObjPostion(eventData);
}
void SetDragObjPostion(PointerEventData eventData)
{
Vector3 mouseWorldPosition;
//判断是否点到UI图片上的时候
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(_TargetRect, eventData.position, eventData.pressEventCamera, out mouseWorldPosition))
{
if (isDrag)
{
_TargetRect.position = mouseWorldPosition + offset;
}
else
{
//计算偏移量
offset = _TargetRect.position - mouseWorldPosition;
}
}
}
}
Unity 拖拽UGUI窗口
原文作者:Dore__
原文地址: https://blog.csdn.net/Dore__/article/details/123351695
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/Dore__/article/details/123351695
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。