当前位置: 首页 > news >正文

Unity 拖动物体技术文档

📌 前提条件

  • 场景中必须有 EventSystem(除非使用 OnMouseDrag)。
  • 拖拽目标必须能被事件系统或物理系统检测到:
    • UI 元素:Canvas + GraphicRaycaster。
    • 3D 物体:Collider + Camera 上的 PhysicsRaycaster。

🖼 拖拽 UI 元素(Canvas 内的 Image/Button)

核心代码

using UnityEngine;
using UnityEngine.EventSystems;public class UIDrag : MonoBehaviour, IBeginDragHandler, IDragHandler
{private Vector2 offset;public void OnBeginDrag(PointerEventData eventData){offset = new Vector2(transform.position.x, transform.position.y) - eventData.position;}public void OnDrag(PointerEventData eventData){transform.position = eventData.position + offset;}
}

特点

  • 无需 Collider
  • 无需 PhysicsRaycaster,只要 Canvas 上有 GraphicRaycaster
  • 坐标直接使用 eventData.position,不需要考虑 z 深度。

🎲 拖拽 3D 世界物体(使用 IDragHandler)

核心代码

using UnityEngine;
using UnityEngine.EventSystems;public class Drag3DObject : MonoBehaviour, IBeginDragHandler, IDragHandler
{private Vector3 offset;public void OnBeginDrag(PointerEventData eventData){Vector3 worldPos = Camera.main.ScreenToWorldPoint(new Vector3(eventData.position.x, eventData.position.y,Camera.main.WorldToScreenPoint(transform.position).z));offset = transform.position - worldPos;}public void OnDrag(PointerEventData eventData){Vector3 pos = new Vector3(eventData.position.x, eventData.position.y,Camera.main.WorldToScreenPoint(transform.position).z);transform.position = Camera.main.ScreenToWorldPoint(pos) + offset;}
}

特点

  • 必须有 Collider
  • Camera 上需要挂 PhysicsRaycaster
  • 通过 ScreenToWorldPoint 将屏幕坐标转换为世界坐标。
  • 使用 offset 避免物体在拖拽时“跳到鼠标中心”。

🎯 射线检测拖拽 3D 物体

核心代码

using UnityEngine;public class RaycastDrag : MonoBehaviour
{private Transform selectedObject;private Vector3 offset;void Update(){if (Input.GetMouseButtonDown(0)){Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;if (Physics.Raycast(ray, out hit, 1000, 1 << LayerMask.NameToLayer("Draggable"))){selectedObject = hit.transform;Vector3 worldPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,Camera.main.WorldToScreenPoint(selectedObject.position).z));offset = selectedObject.position - worldPos;}}if (Input.GetMouseButton(0) && selectedObject != null){Vector3 pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y,Camera.main.WorldToScreenPoint(selectedObject.position).z);selectedObject.position = Camera.main.ScreenToWorldPoint(pos) + offset;}if (Input.GetMouseButtonUp(0)){selectedObject = null;}}
}

特点

  • 灵活,可选择任意 3D 物体。
  • 不依赖 EventSystem,直接用物理系统。
  • 适合 FPS 射击、RTS 单位选择、场景交互。

🖱️ OnMouseDrag 拖拽 3D 物体

核心代码

using UnityEngine;public class MouseDragExample : MonoBehaviour
{private Vector3 offset;void OnMouseDown(){Vector3 worldPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,Camera.main.WorldToScreenPoint(transform.position).z));offset = transform.position - worldPos;}void OnMouseDrag(){Vector3 pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y,Camera.main.WorldToScreenPoint(transform.position).z);transform.position = Camera.main.ScreenToWorldPoint(pos) + offset;}
}

特点

  • 物体必须有 Collider
  • 不需要 EventSystem。
  • 简单快速,但只能响应鼠标,无法处理触摸/多点交互。

⚖️ 四种方式对比

方法 优点 缺点 常见用途
UI 拖拽 (IDragHandler) 简单,直接用事件系统;无需考虑 z 仅限 UI 元素 拖拽 UI 面板、图片
3D 拖拽 (IDragHandler) 与 UI 拖拽统一;支持触摸 依赖 EventSystem + PhysicsRaycaster 少量 3D 拖拽场景
射线检测 灵活,可选择任意物体;适合复杂交互 逻辑稍复杂,需要写 Update FPS 射击、RTS 单位选择
OnMouseDrag 简单,不需要 EventSystem 只能响应鼠标,移动端不适用 快速实现 3D 拖拽

🎯 总结

  • UI 元素拖拽 → 用 IDragHandler
  • 简单 3D 拖拽 → 用 OnMouseDrag
  • 复杂 3D 场景交互 → 用 射线检测
  • 跨平台(鼠标+触摸)3D 拖拽 → 用 IDragHandler + PhysicsRaycaster。

http://www.jsqmd.com/news/113226/

相关文章:

  • 12.19每日总结
  • 研究生必备:7款免费AI论文生成器,效率飙升200%,告别拖延 - 麟书学长
  • OOP-实验六
  • 在 Windows 11 中,以管理员权限打开 CMD(命令提示符)的几种常用方法
  • Git大文件管理与版本回退 - 详解
  • 看三泽纱千香负能量发言有感
  • 完整教程:Live2D形象展示与文本语音播报:打造生动交互体验的完整实现
  • SSM基于信息安全的无锡旅游服务系统5l83d(脚本+源码+数据库+调试部署+研发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
  • 12.19 程序员修炼之道:从小工到专家 - GENGAR
  • 【赵渝强老师】国产金仓数据库的数据库集群
  • RAG的系列文章,有空可以看看
  • Day65-F:\硕士阶段\Java\课程资料\1、黑马程序员Java项目《苍穹外卖》企业级开发实战\sky-take-out-Git-苍穹外卖-swagger-接口文档
  • 【赵渝强老师】MongoDB的数据类型
  • 06.cloundflare的使用
  • 计算机图形学|三维变换与变换矩阵
  • 完整教程:Flutter 布局入门
  • 《程序员修炼之道》阅读笔记8
  • CVE-2025-14910:Edimax BR-6208AC路由器路径遍历漏洞深度解析
  • 数据安全新选择:访答本地知识库的隐私守护之道
  • 详细介绍:ThinkPHP 5.1 程序在 Nginx 和 Apache 下的性能对比
  • 实实在在不夸大值得推荐的银川AI搜索优化公司——智美天创
  • 完整教程:CentOS快速安装DockerCE指南
  • 12月19日
  • 英语_阅读_a plan for cancer prevention_待读
  • 【题解】Luogu P3398 仓鼠找 sugar
  • 个人经验记录
  • 女装店铺数据分析系统:从数据预处理到智能推荐的全链路技术实现与深度解析
  • 第二阶段:Android音视频基础 - 教程
  • 聊天软件项目系统设计总结
  • 2025.12.19