UI的拖拽

限制UI的拖拽范围

思路:使用ScreenPointToLocalPointInRectangle函数获取相对于父物体的本地坐标系,
利用父物体的大小限制UI活动范围

ScreenPointToLocalPointInRectangle参数

  1. 参数1:父对象
  2. 参数2:屏幕坐标
  3. 参数3:渲染UI的相机
  4. 参数4:out修饰的二维向量
using UnityEngine;
using UnityEngine.EventSystems;
public class DragObj : MonoBehaviour, IDragHandler
{
    public Vector2 movePos;
    public void OnDrag(PointerEventData eventData)
    {
        RectTransformUtility.ScreenPointToLocalPointInRectangle(
            transform.parent as RectTransform,
            eventData.position,
            eventData.pressEventCamera,
            out movePos);
        movePos.x = Mathf.Clamp(movePos.x, -(transform.parent as RectTransform).sizeDelta.x / 2 + (transform as RectTransform).sizeDelta.x / 2,
            (transform.parent as RectTransform).sizeDelta.x / 2 - (transform as RectTransform).sizeDelta.x / 2);
        movePos.y = Mathf.Clamp(movePos.y, -(transform.parent as RectTransform).sizeDelta.y / 2 + (transform as RectTransform).sizeDelta.y / 2,
             (transform.parent as RectTransform).sizeDelta.y / 2 - (transform as RectTransform).sizeDelta.y / 2);
        transform.localPosition = movePos;
    }
}

优化

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>
/// UI拖拽
/// </summary>
public class DragPageList : MonoBehaviour, IBeginDragHandler, IDragHandler
{
    Vector2 beginPos;
    Vector2 dragPos;
    Vector3 direction;
    Vector3 currentPos;
    RectTransform parent;
    RectTransform self;
    float horizontalMax;
    float verticalMax;

    private void Awake()
    {
        parent = transform.parent as RectTransform;
        self = transform as RectTransform;
        horizontalMax = parent.sizeDelta.x / 2 - self.sizeDelta.x / 2;
        verticalMax = parent.sizeDelta.y / 2 - self.sizeDelta.y / 2;
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
        //点击位置
        RectTransformUtility.ScreenPointToLocalPointInRectangle(parent,
             eventData.position, eventData.pressEventCamera, out beginPos);
        //当前位置
        currentPos = transform.localPosition;
    }

    public void OnDrag(PointerEventData eventData)
    {
        //拖拽位置
        RectTransformUtility.ScreenPointToLocalPointInRectangle(parent,
            eventData.position, eventData.pressEventCamera, out dragPos);
        //拖拽方向
        direction = dragPos - beginPos;
        //目标位置
        currentPos += direction;
        //限制范围
        currentPos.x = Mathf.Clamp(currentPos.x, -horizontalMax, horizontalMax);
        currentPos.y = Mathf.Clamp(currentPos.y, -verticalMax, verticalMax);
        //到达目标位置
        transform.localPosition = currentPos;
        //更新开始位置
        beginPos = dragPos;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值