Unity 基础 之 IDragHanlder 简单实现 UGUI 元素随着鼠标移动,拖动的效果
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 元素随着鼠标移动,拖动的效果相关推荐
- Unity 屏幕特效 之 简单地使用 Shader 获取深度,实现景深效果
Unity 屏幕特效 之 简单地使用 Shader 获取深度,实现景深效果 目录
- Unity基础知识学习五,UGUI优化相关
1.什么是UGUI优化,UGUI优化的理论基础 1.1理论基础 Canvas, 是Unity渲染系统给层状几何体( layered geometry )提供的可以被画入.被放在上面或者放在世界空间的 ...
- Unity 基础 之 在 UGUI 上简单实现VideoPlayer视频播放的功能,简单暂停播放/显示视频名称/显示时长/拖拽播放等
Unity 基础 之 在 UGUI 上简单实现VideoPlayer视频播放的功能,简单暂停播放/显示视频名称/显示时长/拖拽播放等 目录 Unity 基础 之 在 UGUI 上简单实现VideoPl ...
- Unity UGUI基础 之 Scroll View/Scroll Rect 的简单使用,并取消拖拽(滑动内容)效果,拖拽只在Scrollbar 上起作用
Unity UGUI基础 之 Scroll View/Scroll Rect 的简单使用,并取消拖拽(滑动内容)效果,拖拽只在Scrollbar 上起作用 目录 Unity UGUI基础 之 Scro ...
- Unity 基础 之 一个点(物体)绕另一个点(物体)旋转的简单封装
Unity 基础 之 一个点(物体)绕另一个点(物体)旋转的简单封装 目录 Unity 基础 之 一个点(物体)绕另一个点(物体)旋转的简单封装 一.简单介绍 二.实现原理 三.注意事项 四.效果预览 ...
- Unity 基础 之 鼠标控制 相机(摄像机Camera)的旋转,移动和 fov 的简单使用整理
Unity 基础 之 鼠标控制 相机(摄像机Camera)的旋转,移动和 fov 的简单使用整理 目录 Unity 基础 之 鼠标控制 相机(摄像机Camera)的旋转,移动和 fov 的简单使用整理 ...
- Unity 基础 之 实现枚举(enum/Enum)遍历的三种简单方法(foreach/for)
Unity 基础 之 实现枚举(enum/Enum)遍历的三种简单方法 目录 Unity 基础 之 实现枚举(enum/Enum)遍历的三种简单方法 一.简单介绍 二.实现原理 三.效果预览 四.实现 ...
- unity图片拖不进去_Unity UGUI实现简单拖拽图片功能
这一篇博客我们来使用 UGUI 实现图片的拖拽功能. 说到拖拽,那必然离不开坐标,UGUI 的坐标有点不一样,它有两种坐标,一种是屏幕坐标,还有一种就是 UI 在Canvas内的坐标(暂时叫做ugui ...
- Unity 面试题汇总(三)Unity 基础相关
Unity 面试题汇总(三)Unity 基础相关 目录 Unity 面试题汇总(三)Unity 基础相关 0.FSM(状态机).HFSM(分层状态机).BT(行为树)的区别 1.什么是协同程序? 2. ...
- 第一阶段unity基础
第一阶段unity基础 引言 注意: 1.重点:1.适用性(什么时候用,怎么用)<记脑子里> 2.作用(能够解决什么问题) 3.定义 4.操作/语法 2.每天练习必须会独立完成 3.形 ...
最新文章
- 索尼 android l,家庭影院级音质 索尼Xperia i1参数全曝光
- ofbiz mysql_ofbiz+mysql安装求教
- 红罐王老吉品牌定位战略制定过程详解
- python的核心理念_python核心基础 - 草稿
- 什么是元宇宙?如果你想参与进Metaverse中去,应该采用什么策略?
- 基于scikit-learn的朴素贝叶斯实战
- 阶段1 语言基础+高级_1-3-Java语言高级_09-基础加强_第2节 反射_6_反射_获取字节码Class对象的三种方式...
- ubuntu安装后要做什么
- 视频教程-大数据编程语言scala讲座-其他
- 基于频域的数字图像水印算法设计
- matlab 相机焦距,matlab – 给定焦距和摄像机位置/旋转的正确透视图像
- ERP与MES集成技术在服装行业中的应用
- switchport mode access
- 二维中的OBB相交测试
- 移动端项目功能点及实现方案 (图片居多)
- shell——正则表达式
- 为什么有时ping不通www.baidu.com但可以访问www.baidu.com网页?
- 虚拟服务器安装ibm mq,IBM MQ 客户端查看服务端消息的工具 WMQTool
- ioctl(sock, SIOCGIFHWADDR, ifr)获取网卡mac地址
- coreldraw梯形校正_CorelDRAWX6图形图像设计章节复习试题(大学期末复习资料).docx