UGUI實現UI拖拽並限制UI在屏幕內的拖拽範圍

/*  
 * ......................我佛慈悲...................... 
 *                       _oo0oo_ 
 *                      o8888888o 
 *                      88"C.J"88 
 *                      (| -_- |) 
 *                      0\  =  /0 
 *                    ___/`---'\___ 
 *                  .' \\|     |// '. 
 *                 / \\|||  :  |||// \ 
 *                / _||||| -卍-|||||- \ 
 *               |   | \\\  -  /// |   | 
 *               | \_|  ''\---/''  |_/ | 
 *               \  .-\__  '-'  ___/-. / 
 *             ___'. .'  /--.--\  `. .'___ 
 *          ."" '<  `.___\_<|>_/___.' >' "". 
 *         | | :  `- \`.;`\ _ /`;.`/ - ` : | | 
 *         \  \ `_.   \_ __\ /__ _/   .-` /  / 
 *     =====`-.____`.___ \_____/___.-`___.-'===== 
 *                       `=---=' 
 *                        
 *..................佛祖開光 ,永無BUG................... 
 *  
 */

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

public class DragWindow : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{

    public float limitLeftRange = 0;      //UI到左邊界的距離
    public float limitRightRange = 0;     //UI到右邊邊界的距離
    public float limitTopRange = 0;       //UI到頂部邊界的距離
    public float limitDownRange = 0;      //UI到底部邊界的距離


    private Vector3 offset;              //UI和指針的位置偏移量
    private RectTransform rt;
    private Vector3 pos;
    private float minWidth;             //水平最小拖拽範圍
    private float maxWidth;            //水平最大拖拽範圍
    private float minHeight;            //垂直最小拖拽範圍  
    private float maxHeight;            //垂直最大拖拽範圍
    private float rangeX;               //拖拽範圍
    private float rangeY;               //拖拽範圍


    void Update()
    {
        DragRangeLimit();
    }

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

        minWidth = rt.rect.width / 2+limitLeftRange;
        maxWidth = Screen.width - (rt.rect.width / 2)-limitRightRange;
        minHeight = rt.rect.height / 2+limitDownRange;
        maxHeight = Screen.height - (rt.rect.height / 2)-limitTopRange;
    }

    /// <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;
        }
    }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章