UGUI图片拖拽+缩放功能

转载自:关于Unity中UGUI 图片实现拖拽功能 - 魔卡先生 - 博客园 (cnblogs.com)

自己写也不难,懒得弄就找了个,然后加上了缩放功能。

记录一下这个工具,以后备用。

应用方法:将下面脚本挂载在需要拖拽功能的UI图片上即可

两种拖拽选择:A.中心拖拽(图片中心跟随鼠标位置)m_isPrecision为false;

       B.精准拖拽(图片被鼠标点击的位置跟随鼠标位置)m_isPrecision为true;

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

//UI图片拖拽功能类 脚本功能:UI图片拖拽功能(将脚本挂载在需要拖放的图片上)
public class UIImageDrag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    [Header("是否精准拖拽")]
    public bool m_isPrecision;

    [Header("是否开启缩放")]
    public bool isCanZoom;//是否开启缩放

    [Tooltip("最大放大倍数")]
    public float m_maxScale = 4;

    [Tooltip("最小缩小倍数")]
    public float m_minScale = 0.4f;

    //存储图片中心点与鼠标点击点的偏移量
    private Vector3 m_offset;

    //存储当前拖拽图片的RectTransform组件
    private RectTransform m_rt;

    

    void Start()
    {
        //初始化
        m_rt = gameObject.GetComponent<RectTransform>();
    }

    private void Update()
    {
        if (isCanZoom)
        {
            float s = Input.GetAxis("Mouse ScrollWheel");
            if (s > 0)
            {
                Magnify();
            }
            else if (s < 0)
            {
                Shrink();
            }

        }

    }

    /// <summary>
    /// 放大
    /// </summary>
    void Magnify()
    {
        if (m_rt.localScale.x < m_maxScale)
        {
            m_rt.localScale += new Vector3(0.1f, 0.1f, 0.1f);
        }
    }
    /// <summary>
    /// 缩小
    /// </summary>
    void Shrink()
    {
        if (m_rt.localScale.x > m_minScale)
        {
            m_rt.localScale -= new Vector3(0.1f, 0.1f, 0.1f);
        }
    }
    /// <summary>
    /// 重置图片大小位置
    /// </summary>
    public void ImageReset()
    {
        m_rt.localScale = Vector3.one;
        m_rt.localPosition = Vector3.zero;
    }

    //开始拖拽触发
    public void OnBeginDrag(PointerEventData eventData)
    {
        //如果精确拖拽则进行计算偏移量操作
        if (m_isPrecision)
        {
            // 存储点击时的鼠标坐标
            Vector3 tWorldPos;
            //UI屏幕坐标转换为世界坐标
            RectTransformUtility.ScreenPointToWorldPointInRectangle(m_rt, eventData.position, eventData.pressEventCamera, out tWorldPos);
            //计算偏移量
            m_offset = transform.position - tWorldPos;
        }
        //否则,默认偏移量为0
        else
        {
            m_offset = Vector3.zero;
        }

        SetDraggedPosition(eventData);
    }

    //拖拽过程中触发
    public void OnDrag(PointerEventData eventData)
    {
        SetDraggedPosition(eventData);
    }

    //结束拖拽触发
    public void OnEndDrag(PointerEventData eventData)
    {
        SetDraggedPosition(eventData);
    }

    /// <summary>
    /// 设置图片位置方法
    /// </summary>
    /// <param name="eventData"></param>
    private void SetDraggedPosition(PointerEventData eventData)
    {
        //存储当前鼠标所在位置
        Vector3 globalMousePos;
        //UI屏幕坐标转换为世界坐标
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_rt, eventData.position, eventData.pressEventCamera, out globalMousePos))
        {
            //设置位置及偏移量
            m_rt.position = globalMousePos + m_offset;
        }
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值