Unity - UGUI精准拖拽UI(手指控制UI放大缩小)

最近在做的一个项目里面需要鼠标拖动一张图片移动,起初就使用了Drag的三个接口去实现这个功能,当完成后去查看发现拖拽是实现了,但是每次去拖拽的时候,图片的中心点就会自动到鼠标点的位置。所以为了解决这个问题就需要使用偏移量去解决它。Just do it.

update里的方法是放大缩小的功能,请忽略。

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

public class TouchDesgin : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    public bool isPrecision = true; //是否是精准拖拽

    private RectTransform m_rect;
    private Vector3 m_offset;
    private Touch oldTouch1;  //上次触摸点1(手指1)  
    private Touch oldTouch2;  //上次触摸点2(手指2) 

    void Start()
    {
        m_rect = transform.GetComponent<RectTransform>();
    }

    void Update()
    {
        //没有触摸  
        if (Input.touchCount <= 0)
        {
            return;
        }

        //单点触摸, 水平上下旋转  
        if (1 == Input.touchCount)
        {
            //Touch touch = Input.GetTouch(0);
            //Vector2 deltaPos = touch.deltaPosition;
            //transform.Rotate(Vector3.down * deltaPos.x, Space.World);
            //transform.Rotate(Vector3.right * deltaPos.y, Space.World);
        }

        if (2 == Input.touchCount)
        {
            JinchuanDataInfo.Instance.idleTimer = 0;
            //多点触摸, 放大缩小  
            Touch newTouch1 = Input.GetTouch(0);
            Touch newTouch2 = Input.GetTouch(1);

            //第2点刚开始接触屏幕, 只记录,不做处理  
            if (newTouch2.phase == TouchPhase.Began)
            {
                oldTouch2 = newTouch2;
                oldTouch1 = newTouch1;
                return;
            }

            //计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型  
            float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
            float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);

            //两个距离之差,为正表示放大手势, 为负表示缩小手势  
            float offset = newDistance - oldDistance;

            //放大因子, 一个像素按 0.01倍来算(100可调整)  
            float scaleFactor = offset / 100f;
            Vector3 localScale = transform.localScale;
            Vector3 scale = new Vector3(localScale.x + scaleFactor,
                                        localScale.y + scaleFactor,
                                        localScale.z + scaleFactor);

            //最小缩放到 0.3 倍  
            if (scale.x > 0.3f && scale.y > 0.3f && scale.z > 0.3f)
            {
                transform.localScale = scale;
                if (scale.x > 3 && scale.y > 3 && scale.z > 3)
                {
                    transform.localScale = new Vector3(3f, 3f, 3f);
                }
            }

            //记住最新的触摸点,下次使用  
            oldTouch1 = newTouch1;
            oldTouch2 = newTouch2;
        }
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        Vector3 tWorldPos;
        if (isPrecision)
        {
            RectTransformUtility.ScreenPointToWorldPointInRectangle(m_rect, eventData.position, eventData.pressEventCamera, out tWorldPos);
            m_offset = transform.position - tWorldPos;
        }
        else
        {
            m_offset = Vector3.zero;
        }
        SetDraggedPosition(eventData);
    }

    public void OnDrag(PointerEventData eventData)
    {
        SetDraggedPosition(eventData);
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        SetDraggedPosition(eventData);
    }

    /// 
    /// 设置位置
    /// 
    /// 
    private void SetDraggedPosition(PointerEventData eventData)
    {
        JinchuanDataInfo.Instance.idleTimer = 0;

        //存储当前鼠标所在位置
        Vector3 globalMousePos;
        //UI屏幕坐标转换为世界坐标
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_rect, eventData.position, eventData.pressEventCamera, out globalMousePos))
        {
            //设置位置及偏移量
            m_rect.position = globalMousePos + m_offset;
        }
    }
}

你可能感兴趣的:(Unity引擎)