Unity UGUI拖拽移动

复制代码
 UGUI精准拖拽
复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

[RequireComponent(typeof(RectTransform))]
public class TuoDongImage : MonoBehaviour,IBeginDragHandler,IDragHandler ,IEndDragHandler
{
    private bool isDrag = false;
    //偏移量
    private Vector3 offset = Vector3.zero;

    public void OnBeginDrag(PointerEventData eventData)
    {
        isDrag = false;
        SetDragObjPostion(eventData);
    }

    public void OnDrag(PointerEventData eventData)
    {
        isDrag = true;
        SetDragObjPostion(eventData);
    }

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



    void SetDragObjPostion(PointerEventData eventData)
    {
        RectTransform rect = this.GetComponent<RectTransform>();
        Vector3 mouseWorldPosition;

        //判断是否点到UI图片上的时候
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rect, eventData.position, eventData.pressEventCamera, out mouseWorldPosition))
        {
            if (isDrag)
            {
                rect.position = mouseWorldPosition + offset;
            }
            else
            {
                //计算偏移量
                offset = rect.position - mouseWorldPosition;
            }

            //直接赋予position点到的时候回跳动
            //rect.position = mouseWorldPosition;
        }
    }

}
复制代码
复制代码

 

UI精准拖拽移动

复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>
/// 挂在要拖拽的ui上即可
/// </summary>
public class TuoDongImage : MonoBehaviour,IBeginDragHandler ,IDragHandler,IEndDragHandler 
{
    public bool isPrecision=true;  //精准拖拽为true ,鼠标一直在UI中心可以为false
    //存储图片中心点与鼠标点击点的偏移量
    private Vector3 offect;
    //存储当前拖拽图片的RectTransform组件
    private RectTransform m_rt;
    void Start()
    {
        m_rt = this.transform.GetComponent<RectTransform>();
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
        //如果是精确拖拽则进行计算偏移量操作
        if (isPrecision)
        {
            // 存储点击时的鼠标坐标
            Vector3 tWorldPos;
            //UI屏幕坐标转换为世界坐标
            RectTransformUtility.ScreenPointToWorldPointInRectangle(m_rt, eventData.position, eventData.pressEventCamera, out tWorldPos);
            //计算偏移量   
            offect = transform.position - tWorldPos;
           
        }
        //否则,默认偏移量为0
        else
        {
            offect = Vector3.zero;
        }
        //m_rt.position = Input.mousePosition + offect;
        SetDraggedPosition(eventData);

    }

    //拖拽过程中触发
    public void OnDrag(PointerEventData eventData)
    {
        //m_rt.position = Input.mousePosition + offect;
        SetDraggedPosition(eventData);
    }

    //结束拖拽触发
    public void OnEndDrag(PointerEventData eventData)
    {
        //m_rt.position = Input.mousePosition + offect;
        SetDraggedPosition(eventData);
    }
    private void SetDraggedPosition(PointerEventData eventData)
    {
        //存储当前鼠标所在位置
        Vector3 globalMousePos;
        //UI屏幕坐标转换为世界坐标
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_rt, eventData.position, eventData.pressEventCamera, out globalMousePos))
        {
            //设置位置及偏移量
            m_rt.position = globalMousePos + offect;
        }
    }

}
复制代码

 

第二种 克隆拖拽    把需要克隆的图片 锚点设置一下  (如下图)

 

                

 

 

 

 在把脚本挂在需要克隆的图片上

 

复制代码
/******************************************
 * 项目名称:UGUI通用
 * 脚本功能:UI图片拖拽功能(将脚本挂载在需要拖放的图片上)
*******************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Clonephoto : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    private GameObject objj;
    private RectTransform m_rt;//canvas下面的一个ui
    private Vector3 offect;
    private RectTransform rect;
    void Start()
    {
        //查找这个ui(前提是必须得有这个UI)
        m_rt = GameObject.Find("Canvas").GetComponent<RectTransform>();
        rect = this.transform.GetComponent<RectTransform>();
    }
    //开始拖拽
    public void OnBeginDrag(PointerEventData eventData)
    {
        //ui屏幕坐标转换为世界坐标
        RectTransformUtility.ScreenPointToWorldPointInRectangle(rect, eventData.position, eventData.
            pressEventCamera, out offect);
        //克隆自己在m_rt上面显示
        objj = Instantiate(transform.gameObject,m_rt);
        //objj.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(80, 80); //设置ui大小
        //objj.transform.localScale = new Vector3(0.7f, 0.7f, 0.7f);                  //设置一下拖的时候大小,显得好看一点点,可要可不要
        objj.transform.GetComponent<RectTransform>().anchoredPosition = Input.mousePosition -offect; //计算偏移量
    }
    //拖拽中
    public void OnDrag(PointerEventData eventData)
    {
        if (objj !=null )
        {
            objj.transform.GetComponent <RectTransform>().anchoredPosition = Input.mousePosition-offect;
        }
    }
    //结束拖拽
    public void OnEndDrag(PointerEventData eventData)
    {
        if (objj!=null)
        {
            Accuracy_testing.name = objj.transform.name;
             objj.SetActive(false);
             Invoke("ShanChu", 0.1f);            
        }
    }
    void ShanChu()
    {
        Destroy(objj.gameObject);
    }

}
复制代码

 

posted @   剑起苍穹  阅读(2953)  评论(2编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
/*鼠标点击特效*/
点击右上角即可分享
微信分享提示