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

Unity 适用Canvas 为任一渲染模式的UI 拖拽

RectTransformUtility-ScreenPointToWorldPointInRectangle - Unity 脚本 API

将一个屏幕空间点转换为世界空间中位于给定RectTransform 平面上的一个位置。 

实现 

 获取平面位置。

parentRT = transform.parent as RectTransform;

继承IPointerDownHandler 和IDragHandler 接口。

鼠标点击时记录鼠标与UI 位置的偏差。

RectTransformUtility.ScreenPointToWorldPointInRectangle(parentRT, eventData.position, eventData.pressEventCamera, out Vector3 worldPoint);
offset = transform.position - worldPoint;

拖拽时记录鼠标位置并设定UI 位置。

RectTransformUtility.ScreenPointToWorldPointInRectangle(parentRT, eventData.position, eventData.pressEventCamera, out Vector3 worldPoint);
transform.position = worldPoint + offset;

 效果


http://www.kler.cn/a/572211.html

相关文章:

  • Excel-to-JSON v2.0.0发布,可以在Excel内部,把Excel表格转换成JSON,嵌套的JSON也能转
  • 数据结构(初阶)(四)----双向链表
  • 网络安全需要学多久才能入门?
  • 决策树(Decision Tree)案例分析
  • Halcon 车牌识别-超精细教程
  • Dolphinscheduler调度部署
  • 鸿蒙NEXT开发-应用/元服务签名
  • 算法基础 -- 字符串哈希的基本概念和数学原理分析
  • Windows系统安装python2025最新安装包,包括环境配置,以及安装python编程软件PyCharm2024.3.3免费社区版本,详细全流程
  • 面试八股文--数据库基础知识总结(3)MySQL优化
  • fisco-bcosV3使用go-sdk使用教程
  • 说一下redis事务底层原理
  • element-plus中添加全局样式
  • 大模型应用开发需要的知识和工具
  • 如何判断https使用了哪个版本的TLS?
  • MySQL-简介与基本命令
  • 从 Milvus 中导出数据到 JSON 文件的实践
  • 【第12节】C++设计模式(结构型模式)-Proxy(代理)模式
  • stm32移植LCD2002驱动
  • 数据结构理论