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

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.dtcms.com/a/48544.html

相关文章:

  • 2.css简介
  • pytorch 模型测试
  • 刷题记录10
  • 下载谷歌浏览器(Chrome)
  • HttpServletRequest 和 HttpServletResponse 不同JDK版本的引入
  • 23种设计模式之单例模式(Singleton Pattern)【设计模式】
  • 【三.大模型实战应用篇】【4.智能学员辅导系统:docx转PDF的自动化流程】
  • 基于springboot的丢失儿童的基因比对系统(源码+lw+部署文档+讲解),源码可白嫖!
  • SFP28(25 Gigabit Small Form-factor Pluggable)详解
  • STM32-FOC-SDK包含以下关键知识点
  • 算法基础 -- 字符串哈希的基本概念和数学原理分析
  • Linux常用指令学习笔记
  • 以1.7K深圳小区房价为例,浙大GIS实验室使用注意力机制挖掘地理情景特征,提升空间非平稳回归精度
  • 蓝桥与力扣刷题(蓝桥 k倍区间)
  • JavaScript 系列之:事件
  • 使用Docker搭建Oracle Database 23ai Free并扩展MAX_STRING_SIZE的完整指南
  • C++基础算法:模拟
  • Redis 哨兵模式
  • 本地部署大数据集群前置准备
  • Java中常见的设计模式
  • Qt信号与槽机制
  • 调用的子组件中使用v-model绑定数据以及使用@调用方法
  • 硅基流动前端如何设置tool工具
  • Collab-Overcooked:专注于多智能体协作的语言模型基准测试平台
  • go语言逆向-符号恢复
  • VUE集成Live2d
  • python3使用selenium打开火狐并全屏
  • DeepSeek掘金——DeepSeek-R1图形界面Agent指南
  • LeetCode(必刷75题)151. 反转字符串中的单词——字符串处理
  • 双碳战略下的智慧能源实践:安科瑞储能管理系统助力企业绿色转型