Unity 基础 之 IDragHanlder 多种方法简单实现  UGUI 元素随着鼠标移动,拖动的效果

目录

Unity 基础 之 IDragHanlder 多种方法简单实现  UGUI 元素随着鼠标移动,拖动的效果

一、简单介绍

二、实现原理

三、注意实现

四、效果预览

五、实现步骤

六、多种方法实现拖拽 UI

方法一:RectTransformUtility.ScreenPointToWorldPointInRectangle

方法二 :RectTransformUtility.ScreenPointToWorldPointInRectangle 并带位移 offset

方法三:RectTransformUtility.ScreenPointToLocalPointInRectangle 并带位移 Offset

方法四:Camera.WorldToScreenPoint 和  Camera.WorldToScreenPoint  并带位移 Offset


一、简单介绍

Unity中的一些基础知识点。

本节介绍,使用 IDraHandler ,简单的就实现 UGUI 元素,随着鼠标的移动而移动的效果。

二、实现原理

1、IBeginDragHandler, IDragHandler, IEndDragHandler 三个接口,进行实现拖拽的功能

2、RectTransformUtility.ScreenPointToWorldPointInRectangle 或者  RectTransformUtility.ScreenPointToLocalPointInRectangle

进行坐标转化,实现拖拽

3、必要的再结合 Camera.WorldToScreenPoint 和 Camera.ScreenToWorldPoint 一起实现拖拽移动效果

三、注意实现

1、根据 Canvas 的 Render Mode 不同的拖拽方法,移动的效果可能会略有不同,择需使用即可

四、效果预览

五、实现步骤

1、打开 Unity,新建一个空工程

2、在场景中搭建UI,布局如下

3、新建脚本,编辑方法移动 UI,把脚本对应赋值

六、多种方法实现拖拽 UI

方法一:RectTransformUtility.ScreenPointToWorldPointInRectangle

1、代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;public class DragHandlerUGUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{private RectTransform rectTransform;// Start is called before the first frame updatevoid Start(){rectTransform = GetComponent<RectTransform>();}public void OnBeginDrag(PointerEventData eventData){Debug.Log("开始拖拽");}public void OnDrag(PointerEventData eventData){Vector3 pos;RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, eventData.position, eventData.enterEventCamera, out pos);rectTransform.position = pos;}public void OnEndDrag(PointerEventData eventData){Debug.Log("结束拖拽");}}

2、效果如下

方法二 :RectTransformUtility.ScreenPointToWorldPointInRectangle 并带位移 offset

1、代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;public class DragHandlerUGUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{private RectTransform rectTransform;private Vector3 pos;                            //控件初始位置private Vector3 mousePos;                       //鼠标初始位置private void Start(){rectTransform = GetComponent<RectTransform>();}public void OnBeginDrag(PointerEventData eventData){Debug.Log("开始拖拽");       pos = this.GetComponent<RectTransform>().position;    RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out mousePos);}public void OnDrag(PointerEventData eventData){Vector3 newVec;          RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out newVec);Vector3 offset = new Vector3(newVec.x - mousePos.x, newVec.y - mousePos.y, 0);rectTransform.position = pos + offset;}public void OnEndDrag(PointerEventData eventData){Debug.Log("结束拖拽");}}

2、效果如下

方法三:RectTransformUtility.ScreenPointToLocalPointInRectangle 并带位移 Offset

1、代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;public class DragHandlerUGUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{private Vector3 pos;                            //控件初始位置private Vector2 mousePos;                       //鼠标初始位置private RectTransform canvasRec;                //控件所在画布private void Start(){canvasRec = this.GetComponentInParent<Canvas>().transform as RectTransform;}public void OnBeginDrag(PointerEventData eventData){Debug.Log("开始拖拽");//控件所在画布空间的初始位置pos = this.GetComponent<RectTransform>().anchoredPosition;    //将屏幕空间鼠标位置eventData.position转换为鼠标在画布空间的鼠标位置RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRec, eventData.position, eventData.pressEventCamera, out mousePos);}public void OnDrag(PointerEventData eventData){Vector2 newVec;      RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRec, eventData.position, eventData.pressEventCamera, out newVec);//鼠标移动在画布空间的位置增量Vector3 offset = new Vector3(newVec.x - mousePos.x, newVec.y - mousePos.y, 0);//原始位置增加位置增量即为现在位置(this.transform as RectTransform).anchoredPosition = pos + offset;}public void OnEndDrag(PointerEventData eventData){Debug.Log("结束拖拽");}
}

2、效果如下

方法四:Camera.WorldToScreenPoint 和  Camera.WorldToScreenPoint  并带位移 Offset

1、代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;public class DragHandlerUGUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{Vector3 uiScreenPosition;Vector3 mouseScreenPosition;Vector3 mScreenPosition;private Vector3 offset;Camera mCamera;// Start is called before the first frame updatevoid Start(){mCamera = Camera.main;}public void OnBeginDrag(PointerEventData eventData){Debug.Log("开始拖拽");//转换对象到当前屏幕位置uiScreenPosition = mCamera.WorldToScreenPoint(transform.position);mouseScreenPosition = mCamera.WorldToScreenPoint(eventData.position);//鼠标屏幕坐标mScreenPosition = new Vector3(mouseScreenPosition.x, mouseScreenPosition.y, uiScreenPosition.z);//获得鼠标和对象之间的偏移量,拖拽时相机应该保持不动offset = transform.position - mCamera.ScreenToWorldPoint(mScreenPosition);}public void OnDrag(PointerEventData eventData){mouseScreenPosition = mCamera.WorldToScreenPoint(eventData.position);//鼠标屏幕上新位置mScreenPosition = new Vector3(mouseScreenPosition.x, mouseScreenPosition.y, uiScreenPosition.z);// 对象新坐标transform.position = offset + mCamera.ScreenToWorldPoint(mScreenPosition);}public void OnEndDrag(PointerEventData eventData){Debug.Log("结束拖拽");}
}

2、效果如下

Unity 基础 之 IDragHanlder 简单实现 UGUI 元素随着鼠标移动,拖动的效果相关推荐

  1. Unity 屏幕特效 之 简单地使用 Shader 获取深度,实现景深效果

    Unity 屏幕特效 之 简单地使用 Shader 获取深度,实现景深效果 目录

  2. Unity基础知识学习五,UGUI优化相关

    1.什么是UGUI优化,UGUI优化的理论基础 1.1理论基础 Canvas, 是Unity渲染系统给层状几何体( layered geometry  )提供的可以被画入.被放在上面或者放在世界空间的 ...

  3. Unity 基础 之 在 UGUI 上简单实现VideoPlayer视频播放的功能,简单暂停播放/显示视频名称/显示时长/拖拽播放等

    Unity 基础 之 在 UGUI 上简单实现VideoPlayer视频播放的功能,简单暂停播放/显示视频名称/显示时长/拖拽播放等 目录 Unity 基础 之 在 UGUI 上简单实现VideoPl ...

  4. Unity UGUI基础 之 Scroll View/Scroll Rect 的简单使用,并取消拖拽(滑动内容)效果,拖拽只在Scrollbar 上起作用

    Unity UGUI基础 之 Scroll View/Scroll Rect 的简单使用,并取消拖拽(滑动内容)效果,拖拽只在Scrollbar 上起作用 目录 Unity UGUI基础 之 Scro ...

  5. Unity 基础 之 一个点(物体)绕另一个点(物体)旋转的简单封装

    Unity 基础 之 一个点(物体)绕另一个点(物体)旋转的简单封装 目录 Unity 基础 之 一个点(物体)绕另一个点(物体)旋转的简单封装 一.简单介绍 二.实现原理 三.注意事项 四.效果预览 ...

  6. Unity 基础 之 鼠标控制 相机(摄像机Camera)的旋转,移动和 fov 的简单使用整理

    Unity 基础 之 鼠标控制 相机(摄像机Camera)的旋转,移动和 fov 的简单使用整理 目录 Unity 基础 之 鼠标控制 相机(摄像机Camera)的旋转,移动和 fov 的简单使用整理 ...

  7. Unity 基础 之 实现枚举(enum/Enum)遍历的三种简单方法(foreach/for)

    Unity 基础 之 实现枚举(enum/Enum)遍历的三种简单方法 目录 Unity 基础 之 实现枚举(enum/Enum)遍历的三种简单方法 一.简单介绍 二.实现原理 三.效果预览 四.实现 ...

  8. unity图片拖不进去_Unity UGUI实现简单拖拽图片功能

    这一篇博客我们来使用 UGUI 实现图片的拖拽功能. 说到拖拽,那必然离不开坐标,UGUI 的坐标有点不一样,它有两种坐标,一种是屏幕坐标,还有一种就是 UI 在Canvas内的坐标(暂时叫做ugui ...

  9. Unity 面试题汇总(三)Unity 基础相关

    Unity 面试题汇总(三)Unity 基础相关 目录 Unity 面试题汇总(三)Unity 基础相关 0.FSM(状态机).HFSM(分层状态机).BT(行为树)的区别 1.什么是协同程序? 2. ...

  10. 第一阶段unity基础

    第一阶段unity基础 引言 注意: 1.重点:1.适用性(什么时候用,怎么用)<记脑子里>  2.作用(能够解决什么问题) 3.定义  4.操作/语法 2.每天练习必须会独立完成 3.形 ...

最新文章

  1. 索尼 android l,家庭影院级音质 索尼Xperia i1参数全曝光
  2. ofbiz mysql_ofbiz+mysql安装求教
  3. 红罐王老吉品牌定位战略制定过程详解
  4. python的核心理念_python核心基础 - 草稿
  5. 什么是元宇宙?如果你想参与进Metaverse中去,应该采用什么策略?
  6. 基于scikit-learn的朴素贝叶斯实战
  7. 阶段1 语言基础+高级_1-3-Java语言高级_09-基础加强_第2节 反射_6_反射_获取字节码Class对象的三种方式...
  8. ubuntu安装后要做什么
  9. 视频教程-大数据编程语言scala讲座-其他
  10. 基于频域的数字图像水印算法设计
  11. matlab 相机焦距,matlab – 给定焦距和摄像机位置/旋转的正确透视图像
  12. ERP与MES集成技术在服装行业中的应用
  13. switchport mode access
  14. 二维中的OBB相交测试
  15. 移动端项目功能点及实现方案 (图片居多)
  16. shell——正则表达式
  17. 为什么有时ping不通www.baidu.com但可以访问www.baidu.com网页?
  18. 虚拟服务器安装ibm mq,IBM MQ 客户端查看服务端消息的工具 WMQTool
  19. ioctl(sock, SIOCGIFHWADDR, ifr)获取网卡mac地址
  20. coreldraw梯形校正_CorelDRAWX6图形图像设计章节复习试题(大学期末复习资料).docx

热门文章

  1. 高速公路坐标高程计算软件3.6版本发布
  2. 使用eclipse开发可视化界面windowsbuilder
  3. 【原创】基于Qt5.14的一站式安卓开发环境搭建
  4. 在线教学生计算机,洪恩老兔轻松教你学电脑
  5. 动易2007后台模板上传任意文件漏洞
  6. android中获取 bitmap 像素的颜色 之吸管取色功能
  7. CSND如何转载别人的文章
  8. TrueCrypt的原理
  9. 系泊系统 matlab 代码,系泊系统的设计
  10. 我的世界服务器怎么修改标语,怎么用资源包修改我的世界闪烁标语