Unity 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); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器