【Unity】UGUI实现UI拖拽及限制拖拽范围

【Unity】UGUI实现UI拖拽及限制拖拽范围
实现效果

 

 实现代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class DragUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    /// <summary>
    /// UI和指针的位置偏移量
    /// </summary>
    Vector3 offset;

    RectTransform rt;
    Vector3 pos;                
    float minWidth;             //水平最小拖拽范围
    float maxWidth ;            //水平最大拖拽范围
    float minHeight;            //垂直最小拖拽范围  
    float maxHeight;            //垂直最大拖拽范围
    float rangeX;               //拖拽范围
    float rangeY;               //拖拽范围


    void Update()
    {
        DragRangeLimit();
    }

    void Start()
    {
        rt = GetComponent<RectTransform>();
        pos = rt.position;

        minWidth = rt.rect.width / 2;                           
        maxWidth = Screen.width - (rt.rect.width / 2);
        minHeight = rt.rect.height / 2;
        maxHeight = Screen.height - (rt.rect.height / 2);
    }

    /// <summary>
    /// 拖拽范围限制
    /// </summary>
    void DragRangeLimit()
    {
        //限制水平/垂直拖拽范围在最小/最大值内
        rangeX = Mathf.Clamp(rt.position.x, minWidth, maxWidth);
        rangeY = Mathf.Clamp(rt.position.y, minHeight, maxHeight);
        //更新位置
        rt.position = new Vector3(rangeX, rangeY, 0);
    }

    /// <summary>
    /// 开始拖拽
    /// </summary>
    public void OnBeginDrag(PointerEventData eventData)
    {
        Vector3 globalMousePos;

        //将屏幕坐标转换成世界坐标
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, null, out globalMousePos))
        {
            //计算UI和指针之间的位置偏移量
            offset = rt.position - globalMousePos;
        }
    }

    /// <summary>
    /// 拖拽中
    /// </summary>
    public void OnDrag(PointerEventData eventData)
    {
        SetDraggedPosition(eventData);
    }

    /// <summary>
    /// 结束拖拽
    /// </summary>
    public void OnEndDrag(PointerEventData eventData)
    {

    }

    /// <summary>
    /// 更新UI的位置
    /// </summary>
    private void SetDraggedPosition(PointerEventData eventData)
    {
        Vector3 globalMousePos;

        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, null, out globalMousePos))
        {
            rt.position = offset + globalMousePos;
        }
    }
}

 

拖拽功能的实现:

 UGUI实现UI拖拽功能需要继承IBeginDragHandler, IDragHandler, IEndDragHandler三个接口,并实现接口的三个方法:OnBeginDrag()、OnDrag()、OnEndDrag(),这三个方法分别在开始拖拽、拖拽中、结束拖拽时调用。需要注意的是,即使其中一个方法不执行任何代码,也必须实现该方法,除非不继承该方法的接口,例如上述代码中的OnEndDrag()。

SetDraggedPosition()方法用于更新UI的位置,首先在OnDrag()中计算UI和指针的偏移量,然后将偏移量和转换成世界坐标后的指针位置相加,得到UI的位置。

 

 限制拖拽范围的实现:

 根据UI的宽高和摄像机的可视范围(Screen.width/height)计算出UI的水平/垂直移动范围,用Clamp()方法将UI位置的x和y值锁定在水平/垂直移动范围内。

 

脚本使用方法,直接把脚本添加到需要拖拽的UI上即可。